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

Recommended Answers

All 3 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.