从地图向量中删除元素

发布于 2024-10-08 17:05:38 字数 738 浏览 0 评论 0原文

我有一个地图向量:

typedef map<string, string> aMap;
typedef vector<aMap> rVec;
rVec rows;

如何从行中删除一些元素?

下面的代码不起作用。

struct remove_it
{
  bool operator() (rVec& rows)
  {
    // Validation code here!

  }
};

rVec::iterator it = remove(rows.begin(), rows.end(), remove_it());
rows.erase(it, rows.end());

我收到以下错误。

error: no matching function for call to 'remove(std::vector<s
td::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::vec
tor<std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, mai
n(int, char**)::remove_it)'

谢谢。

I have a vector of maps:

typedef map<string, string> aMap;
typedef vector<aMap> rVec;
rVec rows;

How can I remove some elements from rows?

The following code does not work.

struct remove_it
{
  bool operator() (rVec& rows)
  {
    // Validation code here!

  }
};

rVec::iterator it = remove(rows.begin(), rows.end(), remove_it());
rows.erase(it, rows.end());

I got the following error.

error: no matching function for call to 'remove(std::vector<s
td::map<std::basic_string<char>, std::basic_string<char> > >::iterator, std::vec
tor<std::map<std::basic_string<char>, std::basic_string<char> > >::iterator, mai
n(int, char**)::remove_it)'

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-10-15 17:05:38

1)首先:请提供一个可编译的示例。
上面发布的代码是有问题的,因为 rVec 和 rowsVector 已互换(如果您发布了真实的代码,您自己就会看到这一点)。

2)您使用了错误的删除方法。它应该是remove_if

3)函子是const是正常的

4)operator()应该获取aMap类型的对象(因为这就是你的向量中的内容)而不是返回向量的引用。

5) 不要偷懒,在标准命名空间中的对象前面添加 std::。
而不是使用 using namespace std;

#include <map>
#include <vector>
#include <string>
#include <algorithm>

typedef std::map<std::string, std::string> aMap;
typedef std::vector<aMap>        rVec;

rVec rows;

struct remove_it
{
                 // Corrected type here
  bool operator() (aMap const& row) const  // const here
  {
    // Validation code here!
    return true;
  }
};

int main()
{
                                 // _if herer
    rVec::iterator it = std::remove_if(rows.begin(), rows.end(), remove_it());
    rows.erase(it, rows.end());
}

1) First off: please provide a single compilable example.
Your code posted above is problematic as rVec and rowsVector have been interchanged (you would have seen that yourself if you had posted real code).

2) You are using the wrong remove. It should be remove_if

3) It is normal for the functor to be const

4) The operator() should get object of type aMap (as that is what is in your vector) not a reference back to the vector.

5) Don't be lazy add std:: in-front of objects in the standard namespace.
rather than using using namespace std;

#include <map>
#include <vector>
#include <string>
#include <algorithm>

typedef std::map<std::string, std::string> aMap;
typedef std::vector<aMap>        rVec;

rVec rows;

struct remove_it
{
                 // Corrected type here
  bool operator() (aMap const& row) const  // const here
  {
    // Validation code here!
    return true;
  }
};

int main()
{
                                 // _if herer
    rVec::iterator it = std::remove_if(rows.begin(), rows.end(), remove_it());
    rows.erase(it, rows.end());
}
桜花祭 2024-10-15 17:05:38

remove 需要一个值。您正在尝试使用仿函数,您需要使用 remove_if为此。

此外,您的仿函数需要接受 aMap 类型的对象,而不是 rVec。

remove expects a value. You are trying to use a functor, you need to use remove_if for that.

Also, your functor needs to accept an object of type aMap, not rVec.

絕版丫頭 2024-10-15 17:05:38

remove_if 是你想要的,而不是 删除

remove_if is what you want, not remove.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文