Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple STL question - using STL algorithm on STL map

Simple STL question - using STL algorithm on STL map

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++algorithms
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrawati
    wrote on last edited by
    #1

    Hi If I'm not wrong, an element in STL map is a pair of . Given a map of a number of elements, how can I use STL algorithms like for_each(), accumulate(), etc on just the data, ignoring the keys? Thanks!

    M P 2 Replies Last reply
    0
    • I Indrawati

      Hi If I'm not wrong, an element in STL map is a pair of . Given a map of a number of elements, how can I use STL algorithms like for_each(), accumulate(), etc on just the data, ignoring the keys? Thanks!

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      The parameter of the for_each predicate is the result of dereferencing an iterator, which in the case of a map is a map<key,value>::value_type, or pair<const key,value>. So the predicate can look at the second member of the pair:

      #include <map>
      #include <algorithm>
      typedef map<char,int> mymap;

      struct TotalIt
      {
      void operator()( mymap::value_type elt ) // same as "pair<const char,int> elt"
      {
      m_total += elt.second;
      }

      int m\_total;
      TotalIt() : m\_total(0) { }
      

      };

      int main()
      {
      mymap M;

      M\['a'\] = 1;
      M\['b'\] = 2;
      M\['c'\] = 3;
      M\['d'\] = 4;
      M\['e'\] = 5;
      
      for\_each ( M.begin(), M.end(), TotalIt() );
      

      }

      --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- If my rhyme was a drug, I'd sell it by the gram.

      1 Reply Last reply
      0
      • I Indrawati

        Hi If I'm not wrong, an element in STL map is a pair of . Given a map of a number of elements, how can I use STL algorithms like for_each(), accumulate(), etc on just the data, ignoring the keys? Thanks!

        P Offline
        P Offline
        Paul Ranson
        wrote on last edited by
        #3

        How about this?

        #include <iostream>
        #include <map>
        #include <iterator>
        #include <numeric>

        template <typename I> class Pair2ndIterator : public I
        {
        public :
        Pair2ndIterator ( I i ) : I ( i )
        {
        }
        typename I::value_type::second_type& operator* ()
        {
        return (I::operator*()).second ;
        }
        } ;

        template <typename I> Pair2ndIterator<I> MakePair2ndIterator ( I it )
        {
        return Pair2ndIterator<I> ( it ) ;
        }

        int main()
        {
        std::map<char, int> mymap ;
        mymap.insert ( std::make_pair ( 'a', 10 )) ;
        mymap.insert ( std::make_pair ( 'b', 11 )) ;
        mymap.insert ( std::make_pair ( 'c', 12 )) ;
        mymap.insert ( std::make_pair ( 'd', 13 )) ;
        mymap.insert ( std::make_pair ( 'e', 14 )) ;

        std::cout << "Accumulate ( mymap ) = " <<
            std::accumulate ( MakePair2ndIterator ( mymap.begin ()),
            MakePair2ndIterator ( mymap.end ()), 0 ) ;
        
        return 0;
        

        }

        Paul

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups