In Which Scenario Do I Use a Particular Stl Container

In which scenario do I use a particular STL container?

This cheat sheet provides a pretty good summary of the different containers.

See the flowchart at the bottom as a guide on which to use in different usage scenarios:

http://linuxsoftware.co.nz/containerchoice.png

Created by David Moore and licensed CC BY-SA 3.0

Which STL Container to use? [duplicate]

I think you should check this SO post: In which scenario do I use a particular STL container? for small sizes vector will suit most scenarios irrespective of what you intend to do.

The chart is a guide though, the fact that the container is accessed regularly does not affect container choice, the fact that you are storing int is unimportant unless you care about the size of the container, in which case does the overhead of the pointers in a list container or map matter to you?

Sorting is done automatically by map but sorting a vector and list can be very fast if the container size is small enough to fit in memory.

Data insertion is optimised for lists and maps anywhere in the container, for maps you get the benefit that it will sort itself but again if the size is small enough then constructing a new vector with the new entry could be very fast still.

You may also want to consider hash maps, you would still be best to profile your code, trying to second guess what is optimal depends on your usage and you really need to measure and profile.

You could also just decide that an STL <map> is a fine enough balance or a <set> and use those containers as they automatically sort on insertion and deletion and look up is fast but there is the overhead of maintaining the pointers in each entry that increases the size of the memory used compared to vector, if you don't care about this then you could consider these containers.

Still if it matters then test and profile and compare the performance of each container, you will be surprised by how the code will perform against your assumptions.

Which STL container should I use? C++

We can give you guidelines, but no definitive answer – you need to benchmark that yourself because it crucially depends on your collection and object size:

  • For small objects and/or a relatively small collection, std::vector will be faster because even though you need to copy more data, the better random access time (O(1) vs O(n) for std::list) and the cache locality will dominate.
  • For large objects and/or a large collection, std::list will be faster because although you need O(n) to pick a random object, insertion will be much faster since the copying of many large objects is very slow.

But where exactly the cut-off between these two scenarios lies I cannot say.

Furthermore, if you can get away with swapping the elements instead of insertion, this is a no-brainer: always use a std::vector.

which STL container to use for inter-related data?

You don't define container requirements by what they need to contain, you define it by what operations will be common and how fast they need to operate.

Somewhere there's a wonderful diagram, kind of like a flow chart, that guides you through selecting a container. If I find it I'll update this answer.

Edit: Here it is: In which scenario do I use a particular STL container?

General use cases for C++ containers

A picture is worth a thousand words.

container choice flowchart

It's available from nolyc, the informative bot of ##C++ on Freenode, using the command "container choice" or "containerchoice". The link to this picture you receive in response is hosted at adrinael.net, which suggests we should thank Adrinael, member of Freenode's ##C++ community.

In which scenario do I use a particular STL container?

This cheat sheet provides a pretty good summary of the different containers.

See the flowchart at the bottom as a guide on which to use in different usage scenarios:

http://linuxsoftware.co.nz/containerchoice.png

Created by David Moore and licensed CC BY-SA 3.0

Which STL container(s)/algorithm(s) could I use to solve this?

Just use a std::multimap, with the key type either an integer timestamp (the fastest way) or as you suggested a time string if the default string sort keeps the timestamp order (this is slower)

std::multimap<time_t, std::wstring>

or

std::multimap<std::string, std::wstring>

Insert with:

myFileMap.insert(std::pair<time_t, std::wstring>(time,wPath));


Related Topics



Leave a reply



Submit