Set c++ example: In the previous article, we have discussed about C++ std::set example and tutorial with user defined classes. Let us learn how to Set example and Tutorial – Part 1 in C++ Program.
Set Example and Tutorial
C++ set example: In this we will discuss how to use std::set with default sorting criteria.
What is std::set :
Std::set is a associative container that contains a sorted set of unique objects of type key.
Properties of std::set :
std::setinternally store elements in balanced binary tree.- It doesn’t allow duplicate elements.
- It can contain elements of any specified type in template argument.
- By default it uses the operator < for comparing two elements but if user passes external sorting criteria then it will use that instead of default operator.
std::setwill keep the inserted elements in sorted order based on the assigned sorting criteria i.e. either by default criteria operator < or by passed comparator (if passed).
Example Program using std::set :
#include<iostream>
#include<set>
#include<string>
int main()
{
std::set<std::string> animals;
//Insert elements
animals.insert("Dog");
animals.insert("Cat");
animals.insert("Cow");
animals.insert("Rabbit");
animals.insert("Dog");
animals.insert("Lion");
animals.insert("Panda");
animals.insert("Tiger");
animals.insert("Deer");
animals.insert("Bear");
animals.insert("Elephant");
animals.insert("Zebra");
animals.insert("Giraffe");
animals.insert("Panda");
//It will show the total number of unique elements
std::cout<<"Set Size = "<<animals.size()<<std::endl;
//Iterate through all the elements in a set and display the set
for (std::set<std::string>::iterator it=animals.begin(); it!=animals.end(); ++it)
std::cout << ' ' << *it<<std::endl;
std::cout<<"\n";
return 0;
}
Output: Set Size = 12 Bear Cat Cow Deer Dog Elephant Giraffe Lion Panda Rabbit Tiger Zebra
It only counts the number of unique elements i.e. 12 and ignore repeated elements.
Here total number of elements passed is 14 and repeated elements are 2 which is “Dog” and “Panda”. So, it counts 12(14 – 2=12).
Iterating through std::set :
CPP set: Now we will see how to iterate through std::set.
We can use iterators i.e.
int main()
{
std::set<std::string> animals;
//Insert elements
animals.insert("Dog");
animals.insert("Cat");
animals.insert("Rabbit");
animals.insert("Panda");
animals.insert("Elephant");
animals.insert("Giraffe");
//Iterate through all the elements in a set and display the value
for (std::set<std::string>::iterator it=animals.begin(); it!=animals.end(); ++it)
std::cout << ' ' << *it<<std::endl;
std::cout<<"\n";
return 0;
}
Output : Cat Dog Elephant Giraffe Panda Rabbit
Note : But we cannot modify the elements using iterators because if we modify the element value then internal data structure of std::set will get corrupt and it will not remain balanced binary search tree. Hence further additions and find operations will not work properly.
Searching an element in std::set :
It will search the container for an element equivalent to value and returns an iterator to it if found, otherwise it returns an iterator to set::end.
Let’s try with an example:
#include<iostream>
#include<set>
#include<string>
int main()
{
std::set<std::string> animals;
//Insert elements
animals.insert("Dog");
animals.insert("Cat");
animals.insert("Rabbit");
animals.insert("Panda");
animals.insert("Elephant");
animals.insert("Giraffe");
//Search element using find member functions
std::set<std::string>::iterator it = animals.find("Panda");
if(it != animals.end())
std::cout<<"'Panda' found"<<std::endl;
else
std::cout<<"'Panda' not found"<<std::endl;
it = animals.find("Lion");
if(it != animals.end())
std::cout<<"'Lion' found"<<std::endl;
else
std::cout<<"'Lion' not found"<<std::endl;
it = animals.find("Bear");
if(it == animals.end())
std::cout<<"'Bear' not found"<<std::endl;
else
std::cout<<"'Bear' found"<<std::endl;
return 0;
}
Output : 'Panda' found 'Lion' not found 'Bear' not found
Using std::set::find member method instead of std::find standard generic algorithm ?
Because it will take lesser time then standard algorithm i.e. std::find.
We should use std::set::find member method because find member function knows its internal data structure is balance binary tree and hence designed to corporate on that only. This is the reason why why it takes lesser time then standard algorithm which is std::find.
Verifying during insertion, if element is already added or not :
As we all know std::set uses operator < by default.
It internally maintains binary balanced tree and when we insert elements then it will compare the element with the existing elements and if that element already exist then it doesn’t insert that element and if it will not present then it will insert that element.
But what if it uses the operator < for comparison of two elements then think what will happen if it checks the two elements are equal or not with < operator. Let’s take two variables a and b. if a<b is false and b<a is false then it means a and b are equal.
#include<iostream>
#include<set>
std::set<int> number;
void check(int n)
{
if(number.insert(n).second)
std::cout<<"'"<<n<<"' inserted successfully\n";
else
std::cout<<"'"<<n<<"' was already exist\n";
}
int main()
{
check(6);
check(1);
check(2);
check(1);
//Gives total number unique elements
std::cout<<"Total number of unique elements: "<<number.size()<<std::endl;
// Iterate through all the elements in a set and display the value.
for (std::set<int>::iterator it=number.begin(); it!=number.end(); ++it)
std::cout<< ' '<<"\n" << *it;
std::cout<<"\n"<<"Above are unique elements";
return 0;
}
Output : '6' inserted successfully '1' inserted successfully '2' inserted successfully '1' was already exist Total number of unique elements: 3 1 2 6 Above are unique elements
Removing an element in std::set :
We can remove an element in std::set by using member erase.
Let’s try with an example:
#include<iostream>
#include<set>
#include<string>
int main()
{
std::set<std::string> animals;
//Insert elements
animals.insert("Dog");
animals.insert("Cat");
animals.insert("Lion");
animals.insert("Tiger");
// Iterate through all the elements in a set and display the value.
for (std::set<std::string>::iterator it=animals.begin(); it!=animals.end(); ++it)
std::cout << ' '<< *it;
std::cout<<" -> Before removing an element 'Lion'";
std::cout<<std::endl;
//Remove element "Lion"
animals.erase("Lion");
for (std::set<std::string>::iterator it=animals.begin(); it!=animals.end(); ++it)
std::cout << ' ' << *it;
std::cout<<" -> After removing an element 'Lion'";
std::cout<<std::endl;
return 0;
}
Output: Cat Dog Lion Tiger -> Before removing an element 'Lion' Cat Dog Tiger -> After removing an element 'Lion'