从地图向量中删除元素
我有一个地图向量:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)首先:请提供一个可编译的示例。
上面发布的代码是有问题的,因为 rVec 和 rowsVector 已互换(如果您发布了真实的代码,您自己就会看到这一点)。
2)您使用了错误的删除方法。它应该是remove_if
3)函子是const是正常的
4)operator()应该获取aMap类型的对象(因为这就是你的向量中的内容)而不是返回向量的引用。
5) 不要偷懒,在标准命名空间中的对象前面添加 std::。
而不是使用
using namespace std;
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;
remove
需要一个值。您正在尝试使用仿函数,您需要使用remove_if
为此。此外,您的仿函数需要接受 aMap 类型的对象,而不是 rVec。
remove
expects a value. You are trying to use a functor, you need to useremove_if
for that.Also, your functor needs to accept an object of type aMap, not rVec.
remove_if
是你想要的,而不是删除
。remove_if
is what you want, notremove
.