Which STL container should i use if:
E.g : dataset(4,10,15) if i want to find the closest number to 9, then it should return me 10.
I thought of using vector, but vector insertion and removing is expensive.
vector<int>
If i were to use list, i would have to access O(n) elements before reaching the data.
list<int>
I was thinking of using set as it will be good if it is sorted, but im not very sure about the efficiencies for using SET
So i hope someone can give a good solution!
A Set is a Collection that cannot contain duplicate elements.
In Set duplicate values are not allowed to get stored.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With