Use BOOST_FOREACH with maps with variations!

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Use BOOST_FOREACH with maps with variations!

Postby Ahmed Sayed » Mon May 11, 2020 3:50 am

Hi all,
I want to use BOOST_FOREACH with std maps but i am having a hard time doing so. I am still using RAD studio berlin 10.1 upd 2 with boost lib ver 1.39 and 1.55 installed, also i am using the classic compiler.

I want to create 4 macros
1- one to iterate using the pair of map
2- one to iterate key and value of map
3- one to iterate only keys
4- one to iterate only values with ability to edit value inside the loop

I managed to create the first one but the other 3 are hard with the classic compiler and boost 1.39.
I don't know if it is possible to use boost 1.55 with the classic compiler or not but when ever i try to the path to the project i get tons of compiling errors.

and because of that I can't use this:
Code: Select all
BOOST_FOREACH(int& size, mmap | boost::adaptors::map_values)
{
   ++size;
}


as for the key value macro i will be forced to use this example:
Code: Select all
std::map<int, double> my_map;
int key;
double value;
BOOST_FOREACH(boost::tie(key, value), my_map) { ... }

But this also will cause problems as the compiler will complain if i used this macro twice in the same function for example. Sure the data types above will be deduced from the actual types in the map. So the compiler will give me an error that int key is declared multiple times.

Is there a way to declare a variable inside a macro and make its scope only to that macro knowing that the macro will end with a for loop statement?

This is what i managed to achieve so far hope any one can help me with this?

Code: Select all
#define PairType(_map) std::remove_reference<varType(_map)>::type::value_type
#define KeyType(_map) std::remove_const<PairType(_map)::first_type>::type
#define ValueType(_map) PairType(_map)::second_type
#define MapTuple(_map) boost::tuples::tuple<KeyType(_map), ValueType(_map)>

/*
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.

So you could iterate over map values like this:

BOOST_FOREACH(int& size, mmap | boost::adaptors::map_values)
{
   ++size;
}

std::map<int, double> my_map;
int key;
double value;
BOOST_FOREACH(boost::tie(key, value), my_map) { ... }

*/

#define forMap(pair, _map) \
      typedef PairType(_map) pairtype; \
      BOOST_FOREACH(pairtype& pair, _map)

#define forMapKey(key, _map) \
      std::remove_const<varType(_map.begin()->first)>::type key = _map.begin()->first;\
      for (varType(_map.begin()) it = _map.begin(); it != _map.end(); ++it, key = it->first)

#define forMapValue(value, _map) \
      varType(_map.begin()->second) value = _map.begin()->second; \
      for (varType(_map.begin()) it = _map.begin(); it != _map.end(); ++it, value = it->second)

#define forMapKeyValue(key, value, _map) \
        std::remove_const<varType(_map.begin()->first)>::type key = _map.begin()->first;\
      varType(_map.begin()->second) value = _map.begin()->second; \
      for (varType(_map.begin()) it = _map.begin(); it != _map.end(); ++it, key = it->first, value = it->second)


Thanks in advance any help will be appreciated
Ahmed Sayed
Top Poster
Top Poster
 
Posts: 35
Joined: Thu Nov 08, 2018 4:12 pm

Re: Use BOOST_FOREACH with maps with variations!

Postby Ahmed Sayed » Mon May 11, 2020 10:53 pm

Any help !!!
Ahmed Sayed
Top Poster
Top Poster
 
Posts: 35
Joined: Thu Nov 08, 2018 4:12 pm


Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 39 guests