I'd like to iterate over a std::map using BOOST_FOREACH and edit the values. I can't quite get it.
我想使用BOOST_FOREACH迭代std::map并编辑值。我不太明白。
typedef std::pair<int, int> IdSizePair_t;
std::map<int,int> mmap;
mmap[1] = 1;
mmap[2] = 2;
mmap[3] = 3;
BOOST_FOREACH( IdSizePair_t i, mmap )
i.second++;
// mmap should contain {2,3,4} here
Of course this doesn't change anything because I'm not iterating by reference. So I substitute this line instead (as per the example in the Boost docs):
当然,这不会改变任何东西,因为我没有按引用迭代。所以我替换了这一行(如Boost文档中的示例):
BOOST_FOREACH( IdSizePair_t &i, mmap )
and I get the compiler error:
我得到了编译错误:
error C2440: 'initializing' :
cannot convert from 'std::pair<_Ty1,_Ty2>' to 'IdSizePair_t &'
with
[
_Ty1=const int,
_Ty2=int
]
Any suggestions?
有什么建议吗?
4 个解决方案
#1
67
The problem is with the first member of the pair, which should be const. Try this:
问题是这一对中的第一个元素,应该是const。试试这个:
typedef std::map<int, int> map_t;
map_t mmap;
BOOST_FOREACH( map_t::value_type &i, mmap )
i.second++;
#2
21
This is an old thread, but there is a more convenient solution.
这是一个旧的线程,但是有一个更方便的解决方案。
boost has the notion of 'range adapters' that perform a transformation on iterator ranges. There are specific range adapters for this exact use case (iterating over map keys or values): boost::adaptors::map_values
and boost::adaptors::map_keys
.
boost具有在迭代器范围上执行转换的“范围适配器”的概念。这个用例有特定的范围适配器(遍历映射键或值):boost::::adaptors:::map_values和boost:::adaptors:::map_keys。
So you could iterate over map values like this:
你可以像这样迭代映射值:
BOOST_FOREACH(int& size, mmap | boost::adaptors::map_values)
{
++size;
}
More information here.
更多的信息在这里。
#3
4
Another option is to use BOOST_FOREACH_PAIR, see my answer here:
另一个选项是使用BOOST_FOREACH_PAIR,请参阅我的答案:
BOOST_FOREACH & templates without typedef
没有类型定义的BOOST_FOREACH和模板
#4
0
As of C++11 consider using auto keyword:
在c++ 11中考虑使用auto关键字:
std::map<int,int> mmap;
mmap[1] = 1;
mmap[2] = 2;
mmap[3] = 3;
BOOST_FOREACH(auto& mpair, mmap)
mpair.second++;
//mmap will contain {2,3,4} here
#1
67
The problem is with the first member of the pair, which should be const. Try this:
问题是这一对中的第一个元素,应该是const。试试这个:
typedef std::map<int, int> map_t;
map_t mmap;
BOOST_FOREACH( map_t::value_type &i, mmap )
i.second++;
#2
21
This is an old thread, but there is a more convenient solution.
这是一个旧的线程,但是有一个更方便的解决方案。
boost has the notion of 'range adapters' that perform a transformation on iterator ranges. There are specific range adapters for this exact use case (iterating over map keys or values): boost::adaptors::map_values
and boost::adaptors::map_keys
.
boost具有在迭代器范围上执行转换的“范围适配器”的概念。这个用例有特定的范围适配器(遍历映射键或值):boost::::adaptors:::map_values和boost:::adaptors:::map_keys。
So you could iterate over map values like this:
你可以像这样迭代映射值:
BOOST_FOREACH(int& size, mmap | boost::adaptors::map_values)
{
++size;
}
More information here.
更多的信息在这里。
#3
4
Another option is to use BOOST_FOREACH_PAIR, see my answer here:
另一个选项是使用BOOST_FOREACH_PAIR,请参阅我的答案:
BOOST_FOREACH & templates without typedef
没有类型定义的BOOST_FOREACH和模板
#4
0
As of C++11 consider using auto keyword:
在c++ 11中考虑使用auto关键字:
std::map<int,int> mmap;
mmap[1] = 1;
mmap[2] = 2;
mmap[3] = 3;
BOOST_FOREACH(auto& mpair, mmap)
mpair.second++;
//mmap will contain {2,3,4} here