Jump to content
  • Advertisement
Sign in to follow this  
Daichi

Getting Last Object inserted in STL Container

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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() ).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!