【问题标题】:map, lambda, remove_if地图,拉姆达,remove_if
【发布时间】:2012-03-01 11:29:41
【问题描述】:

所以,我对 std::map、lambda 和 stl 算法(remove_if)有疑问。实际上,与 std::list 或 std::vector 相同的代码效果很好。

我的测试示例:

#include <map>
#include <iostream>
#include <algorithm>

struct Foo
{
    Foo() : _id(0) {}
    Foo(int id) : _id(id)
    {

    }

    int _id;    
};
typedef std::map<int, Foo> FooMap;


int main()
{
    FooMap m;
    for (int i = 0; i < 10; ++i)
        m[i + 100] = Foo(i);

    int removeId = 6;
    // <<< Error here >>>
    std::remove_if(m.begin(), m.end(), [=](const FooMap::value_type & item) { return item.second._id == removeId ;} ); 

    for (auto & item : m )
        std::cout << item.first << " = " << item.second._id << "\n";    

    return 0;
}

错误信息:

In file included from /usr/include/c++/4.6/utility:71:0,
                 from /usr/include/c++/4.6/algorithm:61,
                 from main.cxx:1:
/usr/include/c++/4.6/bits/stl_pair.h: In member function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1, _T2>&&) [with _T1 = const int, _T2 = Foo, std::pair<_T1, _T2> = std::pair<const int, Foo>]’:
/usr/include/c++/4.6/bits/stl_algo.h:1149:13:   instantiated from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::_Rb_tree_iterator<std::pair<const int, Foo> >, _Predicate = main()::<lambda(const value_type&)>]’
main.cxx:33:114:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: assignment of read-only member ‘std::pair<const int, Foo>::first’

我不明白这里有什么问题。所以,我很高兴阅读一些关于它的建议/指导。我的目标 - 使用带有 std::map 和算法的新 lambda 样式,例如 remove_if。

g++ 4.6,-std=c++0x。

【问题讨论】:

  • remove_if 接受一对迭代器,并返回一个迭代器。您认为它从哪里删除了 元素?

标签: c++ lambda c++11 stdmap stl-algorithm


【解决方案1】:

问题是std::map&lt;K,V&gt;::value_typestd::pair&lt;const K, V&gt;,又名.firstconst 并且不可分配。 Lambda 与这里的问题无关。

std::remove_if 通过移动容器的元素来“删除”项目,以便所有不符合谓词的内容都在前面,在返回的迭代器之前。该迭代器之后的所有内容都未指定。它通过简单的赋值来做到这一点,因为你不能赋值给 const 变量,你会得到那个错误。

remove 的名称可能有点误导,在这种情况下,您确实需要 erase_if,但可惜,这并不存在。您将不得不通过 map.erase(iterator) 手动遍历所有项目并擦除它们:

for(auto it = map.begin(), ite = map.end(); it != ite;)
{
  if(it->second._id == remove_id)
    it = map.erase(it);
  else
    ++it;
}

这是安全的,因为您可以擦除树中的单个节点,而不会使其他迭代器失效。请注意,我没有在 for 循环头本身中增加迭代器,因为在删除节点的情况下会跳过一个元素。


† 到目前为止,您应该已经注意到这会对std::map 的排序造成严重破坏,这就是密钥为const 的原因 - 所以在项目已插入。

【讨论】:

  • 感谢您的回答。因此,有任何优雅的方法可以从 std::map 中删除项目而无需丑陋的代码:void removeFromMap(FooMap &amp; m, int id) { for (auto it = m.begin(), end = m.end(); it != end; ++it) { if (it-&gt;second._id == id) { m.erase(it); break; } } }
  • @Reddy:不,我不知道其他方式。顺便说一句,如果您的 ID 不唯一,您只会删除地图中的第一个元素。如果是,那么该循环就可以了。
【解决方案2】:

您可以对地图使用查找和擦除。它不如 remove_if 方便,但它可能是你所拥有的最好的。

int removeId = 6;
auto foundIter = m.find(removeId);

// if removeId is not found you will get an error when you try to erase m.end()
if(foundIter != m.end())
{
    m.erase(foundIter);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2011-08-28
    • 2022-10-04
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多