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 Deque Container - question

STL Deque Container - question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++dockerperformancehelp
5 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.
  • C Offline
    C Offline
    CoolVini
    wrote on last edited by
    #1

    I have made a deque list of my class/structs and it's working fine, all is fine exept one problem of me that I can not understnd or can not find....:confused: I need to get a reference to an object inside my deque and keep that reference for doing thinks but when I do sort in deque this reference change. For example. (MyListToShow is my Deque) (SendMe is my class to add) Now I put it on my list MyListToShow.push_back(SendMe); Now I find the pointer to my last element on deque and save it eg to pointer [KeepMyRef] Now I do sort the deque.... and now [KeepMyRef] is point to a diferent element. How can I get a pointer to the same class - to the memory it self of my data ? Thank you in advanced Vini:rose:

    P 1 Reply Last reply
    0
    • C CoolVini

      I have made a deque list of my class/structs and it's working fine, all is fine exept one problem of me that I can not understnd or can not find....:confused: I need to get a reference to an object inside my deque and keep that reference for doing thinks but when I do sort in deque this reference change. For example. (MyListToShow is my Deque) (SendMe is my class to add) Now I put it on my list MyListToShow.push_back(SendMe); Now I find the pointer to my last element on deque and save it eg to pointer [KeepMyRef] Now I do sort the deque.... and now [KeepMyRef] is point to a diferent element. How can I get a pointer to the same class - to the memory it self of my data ? Thank you in advanced Vini:rose:

      P Offline
      P Offline
      Paul Ranson
      wrote on last edited by
      #2

      Why are you using a deque? Anyway your object has been moved into its sort position, so you cannot keep a reference to it. You could use a search algorithm to find it afterwards. But any iterator will be invalidated after another insert/delete/sort/etc. It may be that a set or map is more suitable for your application. With those the iterator you get when you insert is valid until a further insertion/deletion/whatever. Paul

      C 1 Reply Last reply
      0
      • P Paul Ranson

        Why are you using a deque? Anyway your object has been moved into its sort position, so you cannot keep a reference to it. You could use a search algorithm to find it afterwards. But any iterator will be invalidated after another insert/delete/sort/etc. It may be that a set or map is more suitable for your application. With those the iterator you get when you insert is valid until a further insertion/deletion/whatever. Paul

        C Offline
        C Offline
        CoolVini
        wrote on last edited by
        #3

        Paul thank you for your replay. I have also use the vector but is not solving my problem. About [map] I didn't know it and now just read about. What else do you suggest insted of deque ? I use deque lists and vectors all the time to store informations data that I don't know from the begining the size. I need to be sort them also. Vini

        P 1 Reply Last reply
        0
        • C CoolVini

          Paul thank you for your replay. I have also use the vector but is not solving my problem. About [map] I didn't know it and now just read about. What else do you suggest insted of deque ? I use deque lists and vectors all the time to store informations data that I don't know from the begining the size. I need to be sort them also. Vini

          P Offline
          P Offline
          Paul Ranson
          wrote on last edited by
          #4

          vector is good to add/delete on the end, bad for the middle or beginning. deque is good to add/delete on either end, bad for the middle. map/set are good to add/delete anywhere (but slower than vector/deque) and maintain their contents already sorted so that you don't have to sort manually. list is good to insert/delete anywhere but very bad to sort. etc. You have to make a decision based on your particular application. You might find looking in a good STL reference helps, I recommend the Josuttis book. Paul

          C 1 Reply Last reply
          0
          • P Paul Ranson

            vector is good to add/delete on the end, bad for the middle or beginning. deque is good to add/delete on either end, bad for the middle. map/set are good to add/delete anywhere (but slower than vector/deque) and maintain their contents already sorted so that you don't have to sort manually. list is good to insert/delete anywhere but very bad to sort. etc. You have to make a decision based on your particular application. You might find looking in a good STL reference helps, I recommend the Josuttis book. Paul

            C Offline
            C Offline
            CoolVini
            wrote on last edited by
            #5

            Paul thank you for the informations. ;) Vini

            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