Access to member variables through lambda placeholder formatting

I am trying to print the second member variable of all elements in stl map using lambda expression

map<int, int> theMap; for_each(theMap.begin(), theMap.end(), cout << bind(&pair<int, int>::second, _1) << constant(" ")); 

but this is not a compilation. I essentially want to remove the link from the placeholder. Any idea what I'm missing here?

Thanks in advance!

+4
source share
2 answers

std::map add const to its key; this should prevent spoiling the order. Your couple should be:

 std::pair<const int, int> 

As usually suggested, use value_type to always get the correct type. Verbosity is facilitated by typedef:

 typedef std::map<int, int> int_map; int_map::value_type::second 
+2
source

Try:

 for_each(theMap.begin(), theMap.end(), cout << bind(&map<int, int>::value_type::second, _1) << constant(" ")); 
+3
source

Source: https://habr.com/ru/post/1302196/


All Articles