Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which STL Container to use? [duplicate]

Which STL container should i use if:

  1. Data is inserted and removed regularly.
  2. Data is accessed regularly at random.

E.g : dataset(4,10,15) if i want to find the closest number to 9, then it should return me 10.

  1. I am only storing an integer.
  2. It needs to be sorted
  3. Can go to 100k datasets

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!

like image 508
mister Avatar asked May 12 '12 19:05

mister


People also ask

Which containers do not allow duplicates?

A Set is a Collection that cannot contain duplicate elements.

Which STL does not allow duplicates?

In Set duplicate values are not allowed to get stored.


1 Answers

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.

like image 193
EdChum Avatar answered Oct 14 '22 16:10

EdChum