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. Question about return value of STD MAP::INSERT

Question about return value of STD MAP::INSERT

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comdocker
6 Posts 2 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I am looking at the documentation for map::insert std::map<Key,T,Compare,Allocator>::insert - cppreference.com[^] seems that when you use two parameters such a key and value it returns a iterator to the inserted or the element that prevented listed below is my code

    procpointer->extsymcollector->insert(pair{exsympointer->SYMESDID, *exsympointer });

    my code does NOT complie unless I specfify a return value of a pair iterator and bool and the following

    std::pair::iterator, bool> ret;
    ret = procpointer->extsymcollector->insert(pair{exsympointer->SYMESDID, *exsympointer });

    dont understand why ?

    M 1 Reply Last reply
    0
    • F ForNow

      Hi I am looking at the documentation for map::insert std::map<Key,T,Compare,Allocator>::insert - cppreference.com[^] seems that when you use two parameters such a key and value it returns a iterator to the inserted or the element that prevented listed below is my code

      procpointer->extsymcollector->insert(pair{exsympointer->SYMESDID, *exsympointer });

      my code does NOT complie unless I specfify a return value of a pair iterator and bool and the following

      std::pair::iterator, bool> ret;
      ret = procpointer->extsymcollector->insert(pair{exsympointer->SYMESDID, *exsympointer });

      dont understand why ?

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      You can simplify to:

      auto ret = procpointer->extsymcollector->insert({exsympointer->SYMESDID, *exsympointer})

      ForNow wrote:

      my code does NOT complie

      What compiler to you use? what message to you get?

      Mircea

      F 3 Replies Last reply
      0
      • M Mircea Neacsu

        You can simplify to:

        auto ret = procpointer->extsymcollector->insert({exsympointer->SYMESDID, *exsympointer})

        ForNow wrote:

        my code does NOT complie

        What compiler to you use? what message to you get?

        Mircea

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        That worked with iterator thanks these overloads drive me crazy 🙂

        1 Reply Last reply
        0
        • M Mircea Neacsu

          You can simplify to:

          auto ret = procpointer->extsymcollector->insert({exsympointer->SYMESDID, *exsympointer})

          ForNow wrote:

          my code does NOT complie

          What compiler to you use? what message to you get?

          Mircea

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          when I specify a return of a iterator I get no suitable conversion

          map ::iterator extsymiter;

          extsympointer = procpointer->extsymcollector->insert({exsympointer->SYMESDID, * exsympointer });

          M 1 Reply Last reply
          0
          • F ForNow

            when I specify a return of a iterator I get no suitable conversion

            map ::iterator extsymiter;

            extsympointer = procpointer->extsymcollector->insert({exsympointer->SYMESDID, * exsympointer });

            M Offline
            M Offline
            Mircea Neacsu
            wrote on last edited by
            #5

            std::map::insert returns a pair. Nothing you can do about that. Your code should look like:

            map ::iterator extsymiter;
            auto ret = procpointer->extsymcollector->insert({exsympointer->SYMESDID, * exsympointer });
            if (ret.second)
            extsymiter = ret.first;
            else
            //map already had an element with same key

            If you don't care if the element was there or not, you can just skip the if.

            Mircea

            1 Reply Last reply
            0
            • M Mircea Neacsu

              You can simplify to:

              auto ret = procpointer->extsymcollector->insert({exsympointer->SYMESDID, *exsympointer})

              ForNow wrote:

              my code does NOT complie

              What compiler to you use? what message to you get?

              Mircea

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              thanks, I tried that BTW I looked at the value of the iterator in storage and seems it prefixes my data with a int with the value of the entry number Thank you again

              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