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;
}
Code: Select all
std::map<int, double> my_map;
int key;
double value;
BOOST_FOREACH(boost::tie(key, value), my_map) { ... }
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)