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