如何使用字符串匹配从向量中删除整个元组? [英] How to remove a whole tuple from vector with a string match?

查看:28
本文介绍了如何使用字符串匹配从向量中删除整个元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从与输入字符串匹配的向量中删除一个元组.

vector<元组>目标;

因此,如果字符串与输入字符串 x 匹配,我希望将其删除.

<预><代码>void remove_goal(const string& x){目标.擦除(删除(目标.开始(),目标.结束(),x));}

但我收到此错误

error: 二进制表达式的无效操作数('std::__1::tuple, std::__1::basic_string, int>' and 'const std::__1::basic_string')如果 (!(*__i == __value_))~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

解决方案

下面的示例演示了从 std::vector<中删除 std::tuple 的相当有效的方法/code> 匹配条件.

它使用恰当命名的擦除/删除习语.

#include <算法>#include #include <字符串>#include <元组>#include <向量>int main() {std::vector>v;v.push_back(std::make_tuple(你好", 1, 1));v.push_back(std::make_tuple("Cruel", 2, 2));v.push_back(std::make_tuple("World.\n", 3, 3));v.erase(std::remove_if(v.begin(), v.end(),[](auto s) { return std::get<0>(s) == "Cruel";}),鬻());for (auto i : v) {std::cout <<std::get<0>(i)<<' ';}}

输出:

你好世界.

如您所见,您走在正确的轨道上.但是你不能直接比较 std::stringstd::tuple 仅仅因为 std::tuple 包含一个 std::字符串.这种逻辑没有任何意义,也没有扩展到其他示例.

这解决了您在函数中发生擦除的

#include <算法>#include #include <字符串>#include <元组>#include <向量>void remove_from(std::vector<std::tuple<std::string, int, int>>& v,const std::string&val) {v.erase(std::remove_if(v.begin(), v.end(),[&val](auto s) { return std::get<0>(s) == val;}),鬻());}int main() {std::vector>v;v.push_back(std::make_tuple(你好", 1, 1));v.push_back(std::make_tuple("Cruel", 2, 2));v.push_back(std::make_tuple("World.\n", 3, 3));remove_from(v, 残酷");for (auto i : v) {std::cout <<std::get<0>(i)<<' ';}}

之所以有效,是因为我只是对持有的值进行比较,而根本没有尝试修改 val.

I am trying to remove a tuple from a vector that matches the input string.

vector< tuple<string, int, int> > goals;

So, if the string matches with the input string x, I want it to be removed.


void remove_goal(const string& x){
     goals.erase(remove(goals.begin(), goals.end(), x));
}

But I am getting this error

error: invalid operands to binary expression ('std::__1::tuple<std::__1::basic_string<char>, std::__1::basic_string<char>, int>' and 'const std::__1::basic_string<char>')
            if (!(*__i == __value_))
                  ~~~~ ^  ~~~~~~~~

解决方案

Here's an example that demonstrates a fairly efficient way to delete std::tuples from a std::vector that match a criteria.

It uses the aptly named erase/remove idiom.

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

int main() {
  std::vector<std::tuple<std::string, int, int>> v;

  v.push_back(std::make_tuple("Hello", 1, 1));
  v.push_back(std::make_tuple("Cruel", 2, 2));
  v.push_back(std::make_tuple("World.\n", 3, 3));

  v.erase(std::remove_if(v.begin(), v.end(),
                         [](auto s) { return std::get<0>(s) == "Cruel"; }),
          v.end());

  for (auto i : v) {
    std::cout << std::get<0>(i) << ' ';
  }
}

Output:

Hello World.

As you can see, you were on the right track. But you cannot directly compare a std::string to a std::tuple just because the std::tuple contains a std::string. That logic doesn't really make sense nor does it extend to other examples.

This addresses your edit where the erase occurs in a function:

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

void remove_from(std::vector<std::tuple<std::string, int, int>>& v,
                 const std::string& val) {
  v.erase(std::remove_if(v.begin(), v.end(),
                         [&val](auto s) { return std::get<0>(s) == val; }),
          v.end());
}

int main() {
  std::vector<std::tuple<std::string, int, int>> v;

  v.push_back(std::make_tuple("Hello", 1, 1));
  v.push_back(std::make_tuple("Cruel", 2, 2));
  v.push_back(std::make_tuple("World.\n", 3, 3));

  remove_from(v, "Cruel");

  for (auto i : v) {
    std::cout << std::get<0>(i) << ' ';
  }
}

It works because I'm just making a comparison of values held, and not attempting to modify val at all.

这篇关于如何使用字符串匹配从向量中删除整个元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆