fork(5) download
  1. #include <map>
  2. #include <iostream>
  3.  
  4. template <
  5. template <class K, class V, class C, class A> class mapImpl,
  6. class K,
  7. class V,
  8. class C=std::less<K>,
  9. class A=std::allocator<std::pair<const K, V> >
  10. >
  11. class value_with_iterator {
  12. public:
  13. typedef typename mapImpl<const K,value_with_iterator,C,A>::iterator value_type;
  14. value_type value;
  15. };
  16.  
  17. typedef std::map<size_t, value_with_iterator <std::map, size_t, size_t> > map_size_t_to_itself;
  18.  
  19. map_size_t_to_itself::iterator insert(map_size_t_to_itself& mapRef, size_t value)
  20. {
  21. map_size_t_to_itself::value_type v(value, map_size_t_to_itself::mapped_type());
  22. std::pair<map_size_t_to_itself::iterator, bool> res = mapRef.insert(v);
  23. if (res.second)
  24. res.first->second.value = res.first;
  25. return res.first;
  26. }
  27.  
  28. int main() {
  29. map_size_t_to_itself mapObj;
  30. map_size_t_to_itself::iterator i1 = insert(mapObj, 1);
  31. map_size_t_to_itself::iterator i2 = insert(mapObj, 1);
  32. map_size_t_to_itself::iterator i3 = insert(mapObj, 2);
  33.  
  34. std::cout << i1->first << ": " << i1->second.value->first << std::endl;
  35. std::cout << i2->first << ": " << i2->second.value->first << std::endl;
  36. std::cout << i3->first << ": " << i3->second.value->first << std::endl;
  37. }
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
1: 1
1: 1
2: 2