fork download
  1. #include <map>
  2. #include <set>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. // work - for let say - sets
  8. template <class Type_>
  9. struct Key {
  10. Key(typename Type_::const_iterator it) : value(*it) {}
  11. typename Type_::key_type value;
  12. };
  13.  
  14. // work - for map<***>
  15. template <class Key_, class Value_, class Comp_, class Alloc_>
  16. struct Key<map<Key_, Value_,Comp_,Alloc_> > {
  17. typedef map<Key_, Value_,Comp_,Alloc_> Type_;
  18. Key(typename Type_::const_iterator it) : value(it->first) {}
  19. typename Type_::key_type value;
  20. };
  21.  
  22.  
  23. template< typename T >
  24. const typename T::key_type getKey( const typename T::const_iterator& it )
  25. {
  26. return Key<T>(it).value;
  27. }
  28.  
  29. template< typename T >
  30. void dumpOut( T& coll )
  31. {
  32. for ( typename T::const_iterator it = coll.begin(); it != coll.end(); ++it )
  33. {
  34. const typename T::key_type& a = getKey<T>(it);
  35. cout << a << endl;
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. set<int> s1;
  42. s1.insert(10);
  43. s1.insert(15);
  44. s1.insert(20);
  45.  
  46. dumpOut< set<int> >( s1 );
  47.  
  48. map<int, int> m1;
  49. m1.insert( pair<int, int>(11, -1) );
  50. m1.insert( pair<int, int>(16, -1) );
  51. m1.insert( pair<int, int>(21, -1) );
  52.  
  53. dumpOut< map<int, int> >( m1 );
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 3064KB
stdin
Standard input is empty
stdout
10
15
20
11
16
21