fork download
  1. #include <iostream>
  2. #include <boost/function_output_iterator.hpp>
  3. #include <boost/variant.hpp>
  4. #include <string>
  5. #include <map>
  6. #include <algorithm>
  7.  
  8. typedef std::map<int, std::string> FirstMap;
  9. typedef std::map<short, double> SecondMap;
  10.  
  11. typedef boost::variant<FirstMap::mapped_type, SecondMap::mapped_type> Both;
  12. typedef std::map<long, Both> BothMap;
  13.  
  14. struct CustomOutput {
  15. CustomOutput(BothMap& ref) : _ref(ref) {}
  16.  
  17. template <typename Pair>
  18. void operator()(const Pair& pair) const {
  19. _ref.insert(BothMap::value_type { pair.first, pair.second });
  20. }
  21. private: BothMap& _ref;
  22. };
  23.  
  24. struct CustomPred {
  25. template <typename P1, typename P2>
  26. bool operator()(const P1& p1, const P2& p2) const { return p1.first < p2.first; }
  27. };
  28.  
  29. int main()
  30. {
  31. FirstMap first_map { { 1, "aap" }, { 3, "noot" }, { 5, "mies" } };
  32. SecondMap second_map { { 1, 1/3. }, { 2, 2/3. }, { 4, 4/3.} };
  33.  
  34. BothMap both_map;
  35.  
  36. // iterate over maps union
  37. std::set_union(
  38. first_map.begin(), first_map.end(),
  39. second_map.begin(), second_map.end(),
  40. boost::make_function_output_iterator(CustomOutput(both_map)),
  41. CustomPred()
  42. );
  43.  
  44. for (auto& pair : both_map)
  45. std::cout << pair.first << ":\t" << pair.second << "\n";
  46. }
  47.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
E:\mingw64>.\a.exe
1:      aap
2:      0.666667
3:      noot
4:      1.33333
5:      mies
compilation info
prog.cpp:2:46: fatal error: boost/function_output_iterator.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty