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. Multimap?

Multimap?

Scheduled Pinned Locked Moved C / C++ / MFC
8 Posts 5 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.
  • J Offline
    J Offline
    john5632
    wrote on last edited by
    #1

    Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?

    N R T S 4 Replies Last reply
    0
    • J john5632

      Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      Which multimap implementation are you talking about?

      S 1 Reply Last reply
      0
      • J john5632

        Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?

        R Offline
        R Offline
        Resmi Anna
        wrote on last edited by
        #3

        multimap<string, string> mymap;
        string strKey;

        :
        :

        multimap<string, string>::iterator it;
        :
        it = mymap.find( strKey );
        if(it != mymap.end())
        {
        do
        {
        cout << strKey << " = " << it->second << "\n";
        it++;
        }
        while( it != mymap.upper_bound( strKey ));
        }

        S 1 Reply Last reply
        0
        • J john5632

          Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          cppreference.com

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
          Never mind - my own stupidity is the source of every "problem" - Mixture

          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

          1 Reply Last reply
          0
          • J john5632

            Hi, I am using multimap and key value can be duplicates. I can find first key value using find() function. But how to get next duplicate key value?

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

            Try something like this:

            // MultiMap.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"
            #include <iostream>
            #include <string>
            #include <map>

            void Go()
            {
            using namespace std;

            typedef multimap<int, string> Tmm;
            typedef Tmm::value\_type TmmVal;
            typedef Tmm::const\_iterator TmmIt;
            Tmm mm;
            
            mm.insert(TmmVal(1, "One"));
            mm.insert(TmmVal(1, "Single"));
            mm.insert(TmmVal(2, "Two"));
            mm.insert(TmmVal(2, "1+1"));
            mm.insert(TmmVal(2, "Duo"));
            mm.insert(TmmVal(3, "Three"));
            
            pair<TmmIt, TmmIt> ip = mm.equal\_range(2);
            for (TmmIt it=ip.first; it!=ip.second; ++it)
            {
            	cout << it->first << " : " << it->second << endl;
            }
            

            }

            int main(int argc, char* argv[])
            {
            Go();

            return 0;
            

            }

            Output is:

            2 : Two
            2 : 1+1
            2 : Duo

            Steve

            1 Reply Last reply
            0
            • R Resmi Anna

              multimap<string, string> mymap;
              string strKey;

              :
              :

              multimap<string, string>::iterator it;
              :
              it = mymap.find( strKey );
              if(it != mymap.end())
              {
              do
              {
              cout << strKey << " = " << it->second << "\n";
              it++;
              }
              while( it != mymap.upper_bound( strKey ));
              }

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

              This is inefficient. Why calculate the upper bound each loop?

              Steve

              1 Reply Last reply
              0
              • N Niklas L

                Which multimap implementation are you talking about?

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

                I assume any standard compliant one will do.

                Steve

                N 1 Reply Last reply
                0
                • S Stephen Hewitt

                  I assume any standard compliant one will do.

                  Steve

                  N Offline
                  N Offline
                  Niklas L
                  wrote on last edited by
                  #8

                  I can see no standard compliance requirements in the original post, but your assumptions seem right.

                  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