There is code that works in C ++ 11 :

#include <algorithm> #include <iostream> #include <vector> #include <utility> #include <iterator> typedef std::pair<void*, int> pair; int main(){ std::vector<pair> s; std::vector<int> r; pair p1(NULL, 42); pair p2(NULL, 100500); s.push_back(p1); s.push_back(p2); std::transform(s.begin(), s.end(), std::back_inserter(r), static_cast<const int&(*)(const pair&)>(std::get<1>)); std::copy(r.begin(), r.end(), std::ostream_iterator<int>(std::cout, " ")); } 

Conclusion:

 42 100500 

I want to implement something like this in C ++ 03 without using a custom functor / function of the type:

 struct F { int operator()(const pair& p) { return p.second; } }; 

or

 int f(const pair& p) { return p.second; } 

and call the form:

 std::transform(s.begin(), s.end(), std::back_inserter(r), F()); 

Can this be done with a chain of ready-made library adapters? std::get in C ++ 03 was not yet.

I also don't want to use a manual for loop instead of transform , since this entails mentioning the types of iterators, and the algorithms are not designed to write cycles by hand.

  • It is unlikely, since there is no function returning one value from pair ... map/multimap works with pair , but how, having driven pairs into it, then perform a detour to get the values, and not to mention the iterator already for the map (as I understand does it not roll either?) As far as I remember, there are no other standard uses for the pair . Even if there is a solution, it will be something monstrous :( - Harry

2 answers 2

The international SO community says that there are no standard functions for getting the first and second members of the std::pair in C ++ 03. As a result, there is no possibility to solve the problem. Thank you all for participating :)

  • does not reach our community yet)) - FORTRAN
  • @FORTRAN by the number of participants will lose, of course. But the answer, by and large, was predictable. To reliably confirm the absence of something requires more votes than to confirm something existing. - αλεχολυτ
 #include <algorithm> #include <iostream> #include <vector> #include <utility> #include <iterator> typedef std::pair<void*, int> pair; struct F { int operator()(const pair& p) { return p.second; } }; int main(){ std::vector<pair> s; std::vector<int> r; pair p1 = std::make_pair<void*, int>(NULL, 42); pair p2 = std::make_pair<void*, int>(0, 100500); s.push_back(p1); s.push_back(p2); std::transform(s.begin(), s.end(), std::back_inserter(r), F()); std::copy(r.begin(), r.end(), std::ostream_iterator<int>(std::cout, " ")); } 

MSVC 2010 Compiler

Conclusion:

 42 100500 
  • one
    What did you want to show with this answer? - αλεχολυτ