Set in C++

When we want to store unique elements in any data structure then we use set.
Sets are containers that store unique elements following a specific order.

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their value.

Sets are typically implemented as binary search trees.

It have iterator and all other functionalities of any other data structure.
To add elements we use pair method. pair have two values pair.second is a boolean.It contains true when inserted and false when not inserted.

set <string> dataLines;
string str(this->m_szCurrentLine);
std::pair<std::set<string>::iterator,bool> p;
p=dataLines.insert(str);
The following two tabs change content below.
He is founder and CTO of Habilelabs.io. He senior software architect. He is guru of various Mobile and web technologies including Node.js, Angular.js, Meteor.js. He is founder member of habilelabs Pvt. Ltd.
Category : C++