Getting Last Object inserted in STL Container

Started by
4 comments, last by Cocalus 18 years, 9 months ago
I'd like to be able to get access to the last object I've added to my containers. Namely for a Vector, List or Map. For an STL Vector it seems like I can do *(myvector._Last-1) //for VC6 *(myvector._Mylast-1) //for VC.Net 2k3 I suppose for an STL Map, I could keep a copy of my Key when I add to my Map, but I'm sure there might be a more elegant way. And I'm clueless on an STL List. >.<; (Tho it should be simple.) It seems like I'm also going to have to keep track of my compiler versions too (or Maybe STL versions), since it seems to differ. I'm currently using MS compilers, so I care mostly about these options as of right now. -Daichi
Advertisement
Just save off an iterator to the element when you add it.

Or if you by last you meant the last in the list, then just get the .end() iterator and decrement it ( as long as .end() != .begin() ).
Maybe you could explain what your doing that needs access to the last object you've added. And it looks to me that *(myvector._Last-1) returns the last object in the container (you said last you've added, but you can add objects to various positions, but I don't know what your doing so I can't say). Some ways of getting the last object are.

*(--mycontainer.end()) //I think this should work in most cases

*(mycontainer.rbegin()) //do note that map iterators point to pair, and that this iterator is reversed. (If you don't know what I mean by reversed, then only dereference it)

myvector[myvector.size()]

[Edited by - Cocalus on July 14, 2005 6:07:36 AM]
Um, Ok, using a pointer to the end() iterator works fine for containers where objects are only added to the end, like i'll be doing for Vector and in my case for a List.

But can you go back over a Map? It's a sorted container, so the end could not be the last. (then again, like I said in my first post, I suppose I could just save the key when I add, since it's unique).

Thanks for the help btw... I think most of my troubles are due to lack of sleep. I'm going to bed now. ^^;

-Daichi
No one is aware of the back() function?

back() will return a reference to the last item in the container (if begin() != end()).

For map, the insert function returns a pair<iterator, bool>. This is probably the best way to get the last element inserted. No, you can't get a "back" element out of a sorted container.

Example code:
// This works for vector or list:std::list<int> mylist;mylist.push_back(3);mylist.push_back(4);print(mylist.back()); // prints "4"mylist.back() = 3;// This is for map (set, multiset and multimap are similar)typedef std::map<std::string, int> mymaptype;mymaptype mymap;mymap.insert(mymaptype::value_type("three", 3));mymaptype::iterator back = mymap.insert(mymaptype::value_type("four", 4)).first;print(back->second); // prints "4"print(back->first); // prints "four"back->second = 3;
Quote:Original post by Andrew Russell
No one is aware of the back() function?


*Smacks forehead*

The last time I needed the last element was for a map, I guess I just forgot back().

This topic is closed to new replies.

Advertisement