code_junkie

Providing an iterator for the first element of a container of pairs

Nov 12th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #include <map>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <string>
  5. #include <iterator>
  6. #include <boost/iterator/transform_iterator.hpp>
  7. #include <boost/bind.hpp>
  8. #include <boost/function.hpp>
  9.  
  10. int main() {
  11. typedef std::map<std::string, int>::value_type value_type;
  12. std::map<std::string, int> a;
  13. a["one"] = 1;
  14. a["two"] = 2;
  15.  
  16. // returns the second element
  17. boost::function<int(value_type&)> f = boost::bind(&value_type::second, _1);
  18. std::copy(boost::make_transform_iterator(a.begin(), f),
  19. boost::make_transform_iterator(a.end(), f),
  20. std::ostream_iterator<int>(std::cout, " "));
  21.  
  22. }
Add Comment
Please, Sign In to add comment