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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multimap (STL) equal_range function

Multimap (STL) equal_range function

Scheduled Pinned Locked Moved C / C++ / MFC
c++
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.
  • A Offline
    A Offline
    AkashAg
    wrote on last edited by
    #1

    Hi, I have a doubt regarding multimap::equal_range function for the following program.

    int main ()
    {
    multimap<char,int> mymm;
    multimap<char,int>::iterator it,it1,it2;
    pair<multimap<char,int>::iterator,multimap<char,int>::iterator> ret;

    mymm.insert(std::pair<const char,int>('a',10));
    mymm.insert(std::pair<const char,int>('b',20));
    mymm.insert(std::pair<const char,int>('b',30));
    mymm.insert(std::pair<const char,int>('c',40));
    mymm.insert(std::pair<const char,int>('b',50));
    mymm.insert(std::pair<const char,int>('c',60));
    mymm.insert(std::pair<const char,int>('d',70));

    cout << "mymm contains:\n";
    for (char ch='a'; ch<='d'; ch++)
    {
    cout << ch << " =>";
    ret = mymm.equal_range(ch);
    it1 = ret.first;
    it2 = ret.second;
    cout << (*it1).second << " " << (*it2).second << endl;
    for (it=ret.first; it!=ret.second; ++it)
    cout << " " << (*it).second;
    cout << endl;
    }

    return 0;
    }

    It is giving following output:

    mymm contains:
    a =>10 20
    10
    b =>20 40
    20 30 50
    c =>40 70
    40 60
    d =>70 70

    What I am not getting is the o/p for 'b'. I think it should be

    b =>20 60
    20 30 50

    Please revert. BR, Akash

    CPalliniC S 2 Replies Last reply
    0
    • A AkashAg

      Hi, I have a doubt regarding multimap::equal_range function for the following program.

      int main ()
      {
      multimap<char,int> mymm;
      multimap<char,int>::iterator it,it1,it2;
      pair<multimap<char,int>::iterator,multimap<char,int>::iterator> ret;

      mymm.insert(std::pair<const char,int>('a',10));
      mymm.insert(std::pair<const char,int>('b',20));
      mymm.insert(std::pair<const char,int>('b',30));
      mymm.insert(std::pair<const char,int>('c',40));
      mymm.insert(std::pair<const char,int>('b',50));
      mymm.insert(std::pair<const char,int>('c',60));
      mymm.insert(std::pair<const char,int>('d',70));

      cout << "mymm contains:\n";
      for (char ch='a'; ch<='d'; ch++)
      {
      cout << ch << " =>";
      ret = mymm.equal_range(ch);
      it1 = ret.first;
      it2 = ret.second;
      cout << (*it1).second << " " << (*it2).second << endl;
      for (it=ret.first; it!=ret.second; ++it)
      cout << " " << (*it).second;
      cout << endl;
      }

      return 0;
      }

      It is giving following output:

      mymm contains:
      a =>10 20
      10
      b =>20 40
      20 30 50
      c =>40 70
      40 60
      d =>70 70

      What I am not getting is the o/p for 'b'. I think it should be

      b =>20 60
      20 30 50

      Please revert. BR, Akash

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      The output is correct, the map is orderd by the key. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A AkashAg

        Hi, I have a doubt regarding multimap::equal_range function for the following program.

        int main ()
        {
        multimap<char,int> mymm;
        multimap<char,int>::iterator it,it1,it2;
        pair<multimap<char,int>::iterator,multimap<char,int>::iterator> ret;

        mymm.insert(std::pair<const char,int>('a',10));
        mymm.insert(std::pair<const char,int>('b',20));
        mymm.insert(std::pair<const char,int>('b',30));
        mymm.insert(std::pair<const char,int>('c',40));
        mymm.insert(std::pair<const char,int>('b',50));
        mymm.insert(std::pair<const char,int>('c',60));
        mymm.insert(std::pair<const char,int>('d',70));

        cout << "mymm contains:\n";
        for (char ch='a'; ch<='d'; ch++)
        {
        cout << ch << " =>";
        ret = mymm.equal_range(ch);
        it1 = ret.first;
        it2 = ret.second;
        cout << (*it1).second << " " << (*it2).second << endl;
        for (it=ret.first; it!=ret.second; ++it)
        cout << " " << (*it).second;
        cout << endl;
        }

        return 0;
        }

        It is giving following output:

        mymm contains:
        a =>10 20
        10
        b =>20 40
        20 30 50
        c =>40 70
        40 60
        d =>70 70

        What I am not getting is the o/p for 'b'. I think it should be

        b =>20 60
        20 30 50

        Please revert. BR, Akash

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Some suggestions:

        • Use typedefs:

          typedef multimap<char,int> char2int;
          typedef char2int::iterator char2map_it;
          char2map mymm;
          char2map_it it,it1,it2;
          pair<char2map_it, char2map_it> ret;

          There's no need to repeat long type names all over the place. One design decision should, if possible, be expressed in one place (or as few as possible): Now I can change from a map from chars to ints to a map from chars to std::strings by changing one line, for example.

        • Use make_pair from #include <utility>:

          mymm.insert(make_pair('a', 10));

        None of this is related to your question...

        Steve

        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