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. STL::map, how to prevent use of temporary object?

STL::map, how to prevent use of temporary object?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
6 Posts 4 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.
  • V Offline
    V Offline
    vmaltsev
    wrote on last edited by
    #1

    Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery

    G J C 3 Replies Last reply
    0
    • V vmaltsev

      Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery

      G Offline
      G Offline
      Giles
      wrote on last edited by
      #2

      You need to pass it a pair. e.g.

      map<long, CString> mapNames;

      // Insert an item :

      mapNames.insert(make_pair(5, "Hello"))

      Maps are a bit like Hashes, so the first value is the key, and the second is the value to be returned. You may need to define a copy constructor for you class in the pair, depending on what it contains. e.g. pointers and the like. hope this helps, Giles

      1 Reply Last reply
      0
      • V vmaltsev

        Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        The temporary object myClassObj2 is created as part of the pair passed to map::insert, as you correctly guessed. This pair is used then internally to recreate the object to be inserted. Unfortunately, the value passed to map::insert cannot be used directly for two reasons:

        1. What is passed is a const reference, with which little can be done except replicate it.
        2. The object actually inserted into the map is allocated by special means (namely resorting to an STL allocator), so the pair you just passed simply does not fit.

        I hope this helped you understand the problem. In short, you can't get rid of the temporary object in any easy way. If it is very important not to do excessive copying you should go to some approach using ref-counted pointers or something like that. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        1 Reply Last reply
        0
        • V vmaltsev

          Hi, everyone. Sure, this is a dummy question, but I'm newbie with STL, so please, don't be angry. :) The problem (or maybe feature) is in stl::map.insert(stl::map::value_type(int, myClassObj1)). In this procedure I can see: 1. myClassObj2 is created by copy constructor from myClassObj1 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj2 is destroyed. I guess, myClassObj2 is created in pair(const _T1& _V1, const _T2& _V2) and myClassObj3 is created in map::insert itself. I don't get it. :confused: Could you please explain to me, why temporary object created? And is it possible to get rid of it? Thank you in advance, va'Lery

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You can insert into a map using map[key] = value. Check out my STL article series, part D for an article on the workings of set and map. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

          V 1 Reply Last reply
          0
          • C Christian Graus

            You can insert into a map using map[key] = value. Check out my STL article series, part D for an article on the workings of set and map. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

            V Offline
            V Offline
            vmaltsev
            wrote on last edited by
            #5

            If I use map[key] = myClassObj1 then result is even more weird. 1. myClassObj2 is created by default constructor (constructor without arguments) 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj4 is created by copy constructor from myClassObj3 4. myClassObj3 is destroyed 5. myClassObj2 is destroyed 6. myClassObj4 call operator= myClassObj1 Hmmm... It's too much for me... :) As Joaquín mentioned, there isn't simple way to prevent temporary object. But I have to, because I have collection of collections, so duplication of object will cost me too much. I think there is only one way to do it and it is creation of my own container. Anyway, thanks a lot for all your answers, gents. Life is good but it's better to live good.

            J 1 Reply Last reply
            0
            • V vmaltsev

              If I use map[key] = myClassObj1 then result is even more weird. 1. myClassObj2 is created by default constructor (constructor without arguments) 2. myClassObj3 is created by copy constructor from myClassObj2 3. myClassObj4 is created by copy constructor from myClassObj3 4. myClassObj3 is destroyed 5. myClassObj2 is destroyed 6. myClassObj4 call operator= myClassObj1 Hmmm... It's too much for me... :) As Joaquín mentioned, there isn't simple way to prevent temporary object. But I have to, because I have collection of collections, so duplication of object will cost me too much. I think there is only one way to do it and it is creation of my own container. Anyway, thanks a lot for all your answers, gents. Life is good but it's better to live good.

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              think there is only one way to do it and it is creation of my own container. I'd think twice before going into writing your home made container. Why not have containers of smart pointers to containers? Check Boost shared_ptr class template, which automagically gives you ref-counted access to dynamically allocated objects. The hassle of inserting this into your current code should be minimal. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              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