0

The containers SET/VECTOR/LIST/MAP are very similar in use,so how to choose which one to use? Is where any special cases? Thanks

4
Contributors
3
Replies
30
Views
2 Years
Discussion Span
Last Post by mike_2000_17
Featured Replies
  • 2

    They differ when you look at the time/space complexity of the operations. Genenrally, you look at all of the operations you'll be using first, and then you'll choose which container you'll use. For example, if you need random access by index, you're probably going to want to use a vector. … Read More

  • Our very own Viking Mike has an answer (with a nice flow chart) on this stackoverflow page: http://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container Read More

  • Thanks Moschops for that shout out. I was just going to point to that stack overflow answer. Choosing a good STL container for a given situation is a pretty subtle task, unless it's a trivial problem. I would generally recommend writing your code in a generic way to be able … Read More

2

They differ when you look at the time/space complexity of the operations. Genenrally, you look at all of the operations you'll be using first, and then you'll choose which container you'll use.

For example, if you need random access by index, you're probably going to want to use a vector. If you are going to be iterating through the items, and add elements in the middle you're going to want to use a list.

1

Thanks Moschops for that shout out. I was just going to point to that stack overflow answer.

Choosing a good STL container for a given situation is a pretty subtle task, unless it's a trivial problem. I would generally recommend writing your code in a generic way to be able to swap the container type without too much trouble, then use the one that seems the most appropriate (it's usually vector), and test different containers if this code seems to be a bottleneck in your software.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.