stl containers?

...Yea...I've been programming in c++ proficiently for about 2 years but I'm far from fluent.


This is going to sound like the newbiest question to come from anyone programming at all for as long as I have (6 years...) but how and when do I use stl containers?

IE: Vectors, lists, deque and the such.


edit: btw, the search function on this forum doesnt seem to work for me...I tried searching the above question, and i got a 404 error....everytime.
For the second problem, try the usual delete temp files, cookies etc and retry your search - the search is working fine.
how and when do I use stl containers?


Well, there are many good tutorials online about how to use containers. And as for when to use containers; well it totally depends on the requirement. For example,

1. if you want a key and value pair, like domain name and email address, where domain name is the key (it should be unique) and email address is the value (no need to be unique), then use map.

2. If you don't want to specify the size of the array and want a dynamic array then use vectors.

3. If you want a double linked list, i.e. flexibility to insert and delete elements from the middle or anywhere then use list.

4. If you want the functionality to add and delete elements from the front and back then use deque.

Other than that, I don't know if you know about this, but there is this excellent book called as Effective STL by Scott Meyers. In that he has discussed all about when to use which container.

Hope this helps !


Edit: Edited information clarified by @kempofighter
Last edited on
Start by clicking on the Reference link near the top left corner of this website. This website has a link for every single container type which explains the purpose of each and includes a short example for each member function. I recommend starting with vector, map, and string. You can copy and paste some of the examples into a project, compile them, and change them to experiment with them. I tend to learn faster when I am reading and fiddling with things to see how they work.
Last edited on

2. If you don't want to specify the size of the array and want a dynamic array then use vectors. Note: Insertion and Deletion can only be done from the back.


That is not true at all. Vectors have the insert and erase functions. You can insert and erase at any point within the vector.
That is not true at all. Vectors have the insert and erase functions. You can insert and erase at any point within the vector.


I am very sorry I did not verified before posting, you are absolutely right. Thanks for correcting it.
Although you can insert/erase, they aren't the most effective containers for it...I think an std::list (unordered_list?) would be better (although I don't use them often so I could be wrong) if you want to add/erase at random areas.
how and when do I use stl containers?


Are you asking when should you use STL containers rather than a plain array or making your own basic data structure?

Or are you asking how to know which STL container to choose?

The answer to the first question is that STL containers should be used for virtually all mutable containers in your code. Arrays should only be used for static, constant data that must be initialized by the compiler such as when you need an array of hard-coded error messages. You just cannot initialize a vector<string> container with that data. It must be char*[]. Even then you should be questioning why this isn't in a properties file somewhere. You will rarely need to create your own container.

If you are asking which container to use, that's a much more difficult question to answer. In general, use vector. If that doesn't meet your requirements, you need to know the trade-offs associated with each container type. If you find you need sorted data, consider using a set. If you need an associative container, consider unordered_map. After that, lists, deques, and so forth get more specialized. You will need to know which algorithms you intend to use and how those algorithms perform against each container type in order to make an educated decision.

This is a good reference: http://www.sgi.com/tech/stl/
Topic archived. No new replies allowed.