तो, मुझे std :: map, lambda और stl algorithm (remove_if) के साथ समस्या है। असल में, std :: list या std :: vector के साथ एक ही कोड अच्छी तरह से काम करता है।मानचित्र, लैम्ब्डा, remove_if
मेरे परीक्षण उदाहरण:
#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 और एल्गोरिदम, जैसे remove_if के साथ नई लैम्ब्डा-स्टाइल का उपयोग करें।
जी ++ 4.6, -स्टडी = सी ++ 0 एक्स।
'remove_if' iterators की एक जोड़ी स्वीकार करता है, और एक पुनरावर्तक देता है। आपको लगता है कि यह ** ** से तत्वों को हटा देता है? –