可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have code that looks essentially like this:
std::map<int, int> map1, map2;
BOOST_FOREACH(int i, map1)
{
// do steps 1-5 here...
}
BOOST_FOREACH(int i, map2)
{
// do steps 1-5 (identical to above) here...
}
Is there any way to concatenate the maps to eliminate the duplicate code in the second loop? Or a way to extend BOOST_FOREACH to iterate over two different maps in one go? Obviously I don't want to increase the time complexity of the program (otherwise I could just create a new map and insert into it map1 and map2). I have a feeling I am missing something rudimentary here.
回答1:
You could define a function:
typedef std::map<int, int> IntMap;
void doStuffWithInt(IntMap::value_type &i)
{
// steps 1 to 5
}
BOOST_FOREACH(IntMap::value_type &i, map1)
doStuffWithInt(i);
BOOST_FOREACH(IntMap::value_type &i, map2)
doStuffWithInt(i);
Although in that case it might be even simpler to use std::for_each
:
for_each(map1.begin(), map1.end(), doStuffWithInt);
for_each(map2.begin(), map2.end(), doStuffWithInt);
回答2:
The idea here is to write a special type of iterators to virtually merge two containers, as far as BOOST_FOREACH is concerned. Note that i am not creating a new container out of the two existing ones. I am simply jumping from the first container's end() to the second container's begin() iterator.
I did not try and write the actual merged_iterator class, but although it might a bit long to write, it's not technically difficult. I am actually surprised not to have found something like that using google. I did not look for long, though !
template<typename Container>
boost::iterator_range<
merged_iterator<Container::iterator>
>
concat_containers( Container& c1, Container& c2 )
{
typedef merged_iterator<typename Container::iterator> MergedIterator;
typedef boost::iterator_range<MergedIterator> IteratorRange;
return IteratorRange(
MergeIterator( c1.begin(), c1.end(), c2.begin(), c2.end() ),
MergeIterator( c2.end(), c1.end(), c2.begin(), c2.end() ) );
}
// Now use a bit of magic to define merged_iterator<...>
// And you'll be able to write
BOOST_FOREACH( std::pair<int, int> i, concat_containers( map1, map2 ) )
{
// Do whatever you want here
}
回答3:
In addition to 1800's solution, which I would recommend, there's also various hacky solutions:
for (int stage = 0; stage < 2; stage++) {
BOOST_FOREACH(int i, stage == 0 ? map1 : map2) {
...
}
}
typedef std::map<int, int> intmap;
std::vector<intmap *> v;
v.push_back(&map1);
v.push_back(&map2);
BOOST_FOREACH(intmap *m, v) {
BOOST_FOREACH(int i, *m) {
...
}
}
Note: when I see colleagues write code like this, sometimes I am overcome by an irresistible urge to go strangle them. Use at your own risk.
回答4:
The easiest way is like this:
std::map<int, int> map1, map2;
int key, value;
BOOST_FOREACH(boost::tie(key, value), boost::join(map1, map2))
{
// do steps 1-5 here...
}
And don't worry those commas won't confuse the preprocessor because of the parenthesis.
回答5:
Of the top of my head, I'd try
std::map<int, int> map1, map2;
std::map<int, int>& maps = { map1, map2 }
BOOST_FOREACH(std::map<int, int> map, maps)
BOOST_FOREACH(int i, map)
{
// do steps 1-5 here...
}
回答6:
It's explained here.
You can do this:
std::map<int,int> m;
typedef std::pair<int,int> pair_t;
BOOST_FOREACH(pair_t p, m)