What’s the best alternative to BOOST_FOREACH when you want to iterate over std::set?
When I try and do what seems to be a reasonable method for iterating over a set I get compile errors. This code:
set<string> nameSet;
string aName;
BOOST_FOREACH(nameSet, aName) {
cout << aName << endl;
}
generates this compile error with gcc 4.4.3:
error: no match for ‘operator=’ in ‘nameSet = boost::foreach_detail_::deref [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, C = mpl_::bool_<false>](((const boost::foreach_detail_::auto_any_base&)((const boost::foreach_detail_::auto_any_base*)_foreach_cur70)), 0u)’
According to this link the two aren’t compatible because std::set::iterator must allow mutation of the value it refers to.
I can think of a few ham fisted methods, but am wondering if there is a cleaner way to iterate over the set.
Works for me. Maybe you have the parameters to BOOST_FOREACH reversed.
That’s not really what that link says. What you cannot do, and what that link says won’t work, is this:
Because you cannot form a non-const reference from a const expression. (Namely, each the const members of a
std::set.)