views:

1284

answers:

4

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.

+7  A: 

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);
1800 INFORMATION
If you are mapping ints to ints (std::map<int,int>), I believe the iterators will point to std::pair<const int, int> and not plain int?
David Rodríguez - dribeas
yeah probably, I hadn't actually noticed that - not to confuse the issue though, it shouldn't be too difficult to fix up
1800 INFORMATION
+1, sometimes the simplest solutions are the best :)
j_random_hacker
+2  A: 

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.

ephemient
+1 on the note: You must always consider what you get and what you loose. If you only gain compaction of the code and you loose readability, then it does not compensate.
David Rodríguez - dribeas
i also voted up, because i like both your ways of doing it. the first is quite compact and readable. the second one too. although i would change it to read intmap* v[] = { BOOST_FOREACH(intmap *m, v) { ... } . i think "raw" arrays are fine in this case.
Johannes Schaub - litb
+2  A: 

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
}
Benoît
I implemented it a couple of days ago for another question. Note that it is not proven and might require some tweaking: http://stackoverflow.com/questions/757153/concatenating-c-iterator-ranges-into-a-const-vector-member-variable-at-construc/757328#757328
David Rodríguez - dribeas
+1 for providing a very general approach that e.g. could be made to work even across different container types. But I feel this may be overengineering for the problem at hand -- just make a function like 0800 INFORMATION suggested! :)
j_random_hacker
Just it is overkill for this particular question. I just thought i could try and have fun :)
Benoît
@Benoit: Fair enough, having fun is the whole point! :)
j_random_hacker
A: 

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...
  }
Kees-Jan