C++ STL Set Container – std::set

In this instructional exercise you will find out about STL Set holder in C++, for example, std:: set and all capacities relevant on it.

Set is an acquainted compartment. We realize that in cooperative holders every component is one of a kind. So sets are additional compartments that store one of a kind components following in a particular request.

The word cooperative methods each worth related to key esteem. For any sort of activity, the key will be more favoured than genuine worth.

Here sets are uncommon sort of cooperative holders where esteem itself is key esteem.

Some more realities about the set are, components in the set are consistent. It implies that we can’t adjust once we embed the component.

On the off chance that we need to refresh component, at that point we ought to erase that component and again embed with the refreshed component. The components in the set are constantly arranged.

C++ STL Set

Let see a few capacities related with sets:

Before working with capacities let see iterators that can apply on the rundown to control the information in the list.

start(): returns iterator to the start

end(): returns iterator as far as possible of the rundown

rbegin(): returns switch iterator to turn around starting

rip(): returns invert iterator to turn around end.

These iterators we can use in our projects.

The first thing we have to incorporate is set header document. Which is #include

Embeddings component into the set:

There are various ways we can embed components into a set.

Note: In any strategy underneath when we embed a component into the set it consequently embedded at appropriate position dependent on climbing arranged request.

Technique 1: Insert straightforwardly by passing component. setName.insert(element);

Technique 2: Using iterator. This profits iterator at embedded position. setName.insert (iterator,value)

Technique 3: Copying from another holder.

A model program for embeddings into the set:

#include<iostream>
#include<set>
 
using namespace std;
 
int main(){
	set<int> s1; // declaring a set
	set<int> :: iterator it; // iterator for set
	
	for(int i=0;i<5;i++){
		s1.insert(i*10); // inserting using Method1
	}
	
	it= s1.begin();
	s1.insert(it,99); // inserting using Method2
	
	int ary[]= { 23, 34, 45, 56};
	s1.insert(ary, ary+4); // inserting using Method3
	
	//checking by printing
	for(it= s1.begin(); it!=s1.end(); it++)
		cout << *it << " ";
	
	// We can observe that output will be print in sorted order. That is the property of set
	
	return 0;
}

Output

0 10 20 23 30 34 40 45 56 99

Some more capacities relevant onset are:

delete(): We can eradicate a component by indicating worth or indicating iterator.

swap(): swaps components of set1 to set2 and set2 to set1.

clear(): expels all components in the rundown. It results in the rundown of size 0.

Model program to show the use of above capacities:

#include<iostream>
#include<set>
 
using namespace std;
 
int main(){
	set<int> s1;
	set<int> :: iterator it;
	
	for(int i=0; i<5; i++)
		s1.insert(i+10);
	
	s1.erase(12); // deleting element 12
	cout << "deleting  element 12 --> ";
	
	for(it= s1.begin(); it!=s1.end(); it++)
		cout << *it << " ";
	
	cout << endl;
	
	set<int> s2;
	for(int i=0;i<4;i++)
		s2.insert(i);
	
	cout << "set1 elements before swapping --> ";
	for(it= s1.begin(); it!= s1.end(); it++)
		cout<< *it << " ";
	
	cout << endl;
	
	cout << "set2 elements before swapping --> ";
	for(it= s2.begin(); it!= s2.end(); it++)
		cout<< *it << " ";
	
	cout << endl;
	
	s1.swap(s2); // swapping operation
 
	cout << "set1 elements after swapping --> ";
	for(it= s1.begin(); it!= s1.end(); it++)
		cout<< *it << " ";
	
	cout << endl;
	
	cout << "set2 elements after swapping --> ";
	for(it= s2.begin(); it!= s2.end(); it++)
		cout<< *it << " ";
	
	cout << endl;
 
	s1.clear(); // clearing list1
	s1.empty() ? cout <<"list is empty" << endl: cout << "list is not empty" << endl;
	// ternary operation which resutls list is empty or not
 
	return 0;
}

Output

deleting element 12 –> 10 11 13 14
set1 elements before swapping –> 10 11 13 14
set2 elements before swapping –> 0 1 2 3
set1 elements after swapping –> 0 1 2 3
set2 elements after swapping –> 10 11 13 14
list is empty 

Some more capacities are:

void(): restores a Boolean worth whether the set is unfilled or not.

size(): restores the size of the rundown.

max_size(): restores the most extreme size a set can have.

discover(): It returns an iterator to the component.

count(x): Returns how often components “x” present inset.

Model program to show utilization of above capacities:

#include<iostream>
#include<set>
 
using namespace std;
 
int main(){
	set<int> s1;
	set<int> :: iterator it;
	
	for(int i=0; i<5; i++)
		s1.insert(i+10);
	
	s1.empty() ? cout <<"list is empty" << endl: cout << "list is not empty" << endl;
	cout << "size of the list is " << s1.size() << endl;
	cout << "maximum size of the list is " << s1.max_size() << endl;
	cout << "finding elemnt 12 in list" << endl;
	it= s1.find(12);
	cout << *it << endl;
	
	s1.insert(12);
	if(s1.count(22))
		cout << "number 22 is in the list " << endl;
	else
		cout << "22 is not in the list";
		
	return 0;
}

Output

list is not empty
size of the list is 5
maximum size of the list is 461168601842738790
finding elemnt 12 in list
12
22 is not in the list

Remark beneath on the off chance that you have any questions or discovered any data inaccurate in above instructional exercise for STL Set compartment in C++.

Leave a Comment

error: Alert: Content is protected!!