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. map object in STL

map object in STL

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

    I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:

    n2 = myMap[n1];
    

    instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns

    M C 2 Replies Last reply
    0
    • N ns

      I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:

      n2 = myMap[n1];
      

      instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns

      M Offline
      M Offline
      Marc Britten
      wrote on last edited by
      #2

      myMap[n1] = n2; SHOULD work, ie create an entry if it does not already exit n2 = myMap[n1]; SHOULD NOT work, it should create an entry if it does not exist and give n2 its value.

      N 1 Reply Last reply
      0
      • N ns

        I have a map of int, int. Someone told me that this statement below will create the element of index n1 and fill it with n2:

        n2 = myMap[n1];
        

        instead of the othe way around which works as well. THe benefit of the above method is that if that element already exists in the map, then in one shot we either accessed it (pree-exist) or created it (wasnt there). Seems so counterintuitive. Is it ccorrect? Didnt see anything like it in my books... Appreciate your help, ns

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        ns wrote: Someone told me that this statement below will create the element of index n1 and fill it with n2 it's not true, and you can prove it with the code below.

        map<int, int> myMap;

        int t = 50;
        t = myMap[10];

        map<int, int>::iterator it;

        for (it=myMap.begin(); it!=myMap.end(); it++)
        {
        printf("%d, %d\n", (*it).first, (*it).second);
        }

        the output is 10, 0; not 10, 50. it will create the element, but the value part of it is the default value for the object (for an int, i guess the default is 0, in debug mode anyway). if you want to set the value, you have to use : myMap[10] = 50; -c


        All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public

        War Pigs

        N 1 Reply Last reply
        0
        • M Marc Britten

          myMap[n1] = n2; SHOULD work, ie create an entry if it does not already exit n2 = myMap[n1]; SHOULD NOT work, it should create an entry if it does not exist and give n2 its value.

          N Offline
          N Offline
          ns
          wrote on last edited by
          #4

          Okay.But if myMap[n1] just got created its empty right? So n2 = 0. Appreciate your help, ns

          1 Reply Last reply
          0
          • C Chris Losinger

            ns wrote: Someone told me that this statement below will create the element of index n1 and fill it with n2 it's not true, and you can prove it with the code below.

            map<int, int> myMap;

            int t = 50;
            t = myMap[10];

            map<int, int>::iterator it;

            for (it=myMap.begin(); it!=myMap.end(); it++)
            {
            printf("%d, %d\n", (*it).first, (*it).second);
            }

            the output is 10, 0; not 10, 50. it will create the element, but the value part of it is the default value for the object (for an int, i guess the default is 0, in debug mode anyway). if you want to set the value, you have to use : myMap[10] = 50; -c


            All you have to do is tell the people they are being attacked, and denounce the opposition for lack of patriotism and exposing the country to danger. -- Herman Goering, on how to control the public

            War Pigs

            N Offline
            N Offline
            ns
            wrote on last edited by
            #5

            Many thanks. Clear now., Appreciate your help, ns

            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