What is the most efficient way of adding non-repeated elements into STL container and what kind of container is the fastest? I have a large amount of data and I'm afraid each time I try to check if it is a new element or not, it takes a lot of time. I hope map be very fast.
// 1- Map
map<int, int> Map;
...
if(Map.find(Element)!=Map.end()) Map[Element]=ID;
// 2-Vector
vector<int> Vec;
...
if(find(Vec.begin(), Vec.end(), Element)!=Vec.end()) Vec.push_back(Element);
// 3-Set
// Edit: I made a mistake: set::find is O(LogN) not O(N)
A Set is a Collection that cannot contain duplicate elements.
The three types of containers found in the STL are sequential, associative and unordered.
Container library as flat_set .
set::begin() and set::end() in C++ STL. Sets are a type of associative container in which each element has to be unique because the value of the element identifies it.
Both set
and map
has O(log(N))
performance for looking up keys. vector
has O(N)
.
The difference between set
and map
, as far as you should be concerned, is whether you need to associate a key with a value, or just store a value directly. If you need the former, use a map
, if you need the latter, use a set
.
In both cases, you should just use insert()
instead of doing a find()
.
The reason is insert()
will insert the value into the container if and only if the container does not already contain that value (in the case of map
, if the container does not contain that key). This might look like
Map.insert(std::make_pair(Element, ID));
for a map or
Set.insert(Element);
for a set.
You can consult the return value to determine whether or not an insertion was actually performed.
If you're using C++11, you have two more choices, which are std::unordered_map
and std::unordered_set
. These both have amortized O(1)
performance for insertions and lookups. However, they also require that the key (or value, in the case of set) be hashable, which means you'll need to specialize std::hash<>
for your key. Conversely, std::map
and std::set
require that your key (or value, in the case of set) respond to operator<()
.
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