how to use BOOST_FOREACH for map container

Hello every one,
I would like to know how I can use BOOST_FOREACH for map container. if the map format is like : std::map< std::string, std::set<int> > how I can access each key with BOOST_FOREACH.
I will appreciate any help
regards
The type is pair:

1
2
3
4
5
6
std::map< std::string, std::set<int> > m;
...
BOOST_FOREACH(const std::pair< std::string, std::set<int> > &d, m)
{
  d.first // access the key
}


The C++11 for loop can be use equally.
thanks for your help. however, may I know why I have received this error " test6.cpp:15:53: error: macro "BOOST_FOREACH" passed 3 arguments, but takes just
2
BOOST_FOREACH(const std::pair<int, int> & k, my_map)
^
test6.cpp: In function 'int main()':
test6.cpp:15:2: error: 'BOOST_FOREACH' was not declared in this scope
BOOST_FOREACH(const std::pair<int, int> & k, my_map)" for below code :


#include <iostream>
#include <set>
#include <map>
#include <boost/foreach.hpp>
using namespace boost;
int main()
{
typedef std::pair<int, int> IdSizePair_t;
std::map<int, int> my_map;
my_map[1] = 1;
my_map[2] = 2;
my_map[3] = 3;
BOOST_FOREACH(const std::pair<int, int> & k, my_map)
k.first++;
}


thanks
regards
Last edited on
You've got the type wrong.

1
2
3
4
5
6
7
8
9
10
11
12
    using map_type = std::map<int, int>;

    map_type my_map;
    my_map[1] = 1;
    my_map[2] = 2;
    my_map[3] = 3;
    BOOST_FOREACH(map_type::value_type & k, my_map)
    // or BOOST_FOREACH(auto& k, my_map) also works in for C++11.
    {
        k.first++;
    }
}


But you should note that this code won't work anyway. The key in a map is constant, so k.first++ is illegal.

[edit: And it was trivial to find a working solution by using a little google-fu.]
Last edited on
Yes, cire is right: The key needs to be const.

And I don't think that you can omit the brackets.

The key is const for a good reason. Any change of the value would need a reorganization of the map. In your case 1 becomes 2 -> What happens to the already existing 2?

[EDIT]
Oh, and you cannot use a comma for a macro parameter (like the comma in pair: std::pair<int, int>). So you need a typedef or what cire showed.
Last edited on
This being the future, you can just use an ordinary ranged for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <map>

int main()
{
  std::map<int, int> my_map;
  my_map[1] = 7;
  my_map[2] = 8;
  my_map[3] = 9;
  for (auto& pair : my_map)
    {
      std::cout << pair.first << " has value " << pair.second << std::endl;
    }
}


There's nothing wrong with using BOOST_FOREACH, of course, but when it's just being used instead of plain C++, it seems unnecessary.
Last edited on
thanks for all of help. however the below code works too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <set>
#include <map>
#include <boost/foreach.hpp>
using namespace boost;
int main()
{
	typedef std::pair<int, int> MyPair;
std::map<int, int> my_map;
my_map[1] = 4;
my_map[2] = 5;
my_map[3] = 6;
int key;
double value;
BOOST_FOREACH(MyPair p, my_map) {
 //BOOST_FOREACH(const std::pair<int, int> & k, my_map) {
 	p.first++;
std::cout << p.first;}
}

2 3 4
however the below code works too.


Yes. It works.. if you want to modify a copy of what the map contains and print the modified copy. Nothing in the map is changed.
Topic archived. No new replies allowed.