[STL]set common usage details

Posted by miasma on Mon, 24 Jan 2022 02:07:52 +0100

catalogue

introduce

Introduction to common usage

1. Definition of set

2. Access to elements in the set container

3. set common function analysis

1)insert()

2)find()

3)erase()

4)size()

5)clear()

Common use

extend

introduce

Set is translated into a set, which is an internally ordered container without duplicate elements. In the exam, it may be necessary to remove duplicate elements, and the element is too large or the element type is not int. these situations will make it difficult to open the hash table. In this case, set can be used to retain the element itself regardless of its number. Compared with array operation, set is often more intuitive and less thinking.

When using set, don't forget to use the header file #include < set >, and add "using namespace std;" under the header file, Next, you can use set in your code.

Introduction to common usage

1. Definition of set

The method of defining a set is as follows:

set<typename> name;

You may find that the definition is written in the same way as vector (if you don't know vector, I suggest reading another blog first https://blog.csdn.net/m0_59924193/article/details/122535504 ), or most stl containers are defined in this way, and the typename here can still be any type. Like vector, when the defined typename is an STL container, you need to add a space between > > to avoid some old compilers considering it as a shift operation. Examples are as follows:

set<int> name;
set<char> name;
set<node> name; //node is a structure type 

The definition of set array is the same as that of vector:

set<typename> Arrayname[arraySize]; 

Thus, each from Arrayname[0]~Arrayname[arraySize - 1] is a set container.

2. Access to elements in the set container

set can only be accessed through iterator s:

set<typename>::iterator it;

typename is the same as the type filled in when defining set. After we get the iterator it, we can access the elements in the set through * it.

Since STL containers other than vector and string do not support * (it + 1) access, they can only be enumerated in the following ways:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	//insert(x) inserts x into the set 
	st.insert(3); 
	st.insert(5);
	st.insert(2);
	st.insert(3);
	//Note: It < st.end() is not supported 
	for(set<int>::iterator it = st.begin(); it != st.end(); it++)
		printf("%d ", *it);
	
	return 0; 
}
/*
result: 2 3 5
*/

It is not difficult to find that the elements in the set are automatically sorted incrementally and the duplicate elements are automatically removed.

3. set common function analysis

1)insert()

insert(x) can insert x into the set container, and automatically increment, sort and de duplicate. The time complexity is O(logN), where N is the number of elements in the set. The example is in the element access section above.

2)find()

find(value) returns the iterator with the corresponding value of value in the set. The time complexity is O(logN), and N is the number of elements in the set.

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	for(int i = 1; i <= 3; i++){
		st.insert(i);
	}
	set<int>::iterator it = st.find(2);
	printf("%d\n", *it);
	//The above two sentences can also be written as printf("%d\n", *(st.find(2)); 
	return 0;
}
/*
result: 2
*/

3)erase()

erase() has two uses: delete a single element and delete all elements in an interval.

To delete a single element:

Method 1: st.erase(it), which is the iterator of the element to be deleted. The time complexity is O(1), which can be used in combination with the find function.

Examples are as follows:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	st.insert(100);
	st.insert(200);
	st.insert(100);
	st.insert(300);
	st.erase(st.find(100));
	//Use the find() function to find 100, and then use the erase function to delete it
	for(set<int>::iterator it = st.begin(); it != st.end(); it++)
		printf("%d ", *it);
	
	return 0; 
}
/*
result:200 300
*/

st.erase(value), value is the value of the element to be deleted. The time complexity is O(logN), and N is the number of elements obtained in set. Examples are as follows:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	st.insert(100);
	st.insert(200);
	st.erase(100); //Delete the element with a value of 100 in the set
	for(set<int>::iterator it = st.begin(); it != st.end(); it++){
		printf("%d\n", *it);
	}
	return 0;
}
/*
result: 200
*/ 

Delete all elements in an interval:

First.erase (first, last) can delete all elements in an interval, where first is the start iterator of the interval to be deleted, and last is the next address of the end iterator of the interval to be deleted, that is, delete [first, last). The time complexity is O(last - first).

Examples are as follows:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	st.insert(20);
	st.insert(30);
	st.insert(10);
	st.insert(40);
	set<int>::iterator it = st.find(30);
	st.erase(it, st.end());
	//Delete the elements between element 30 and the end of set, i.e. 30 and 40 
	for(it = st.begin(); it != st.end(); it++){
		printf("%d ", *it);
	}
	
	return 0;
}
/*
result: 10 20
*/ 

4)size()

size() is used to obtain the number of elements in the set. The time complexity is O(1).

Examples are as follows:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	st.insert(2); //Insert 2 5 4 
	st.insert(5);
	st.insert(4);
	printf("%d\n", st.size());
	
	return 0;
}
/*
result: 3
*/

5)clear()

clear() is used to clear all elements in the set. The complexity is O(N), where N is the number of elements in the set.

Examples are as follows:

#include <iostream>
#include <set>
using namespace std;
int main(){
	set<int> st;
	st.insert(2);
	st.insert(3);
	st.insert(5);
	printf("%d ", st.size());
	st.clear();
	printf("%d\n", st.size());
	
	return 0;
}
/*
result:3 0
*/

Common use

The main function of set is to automatically remove duplicates and sort them in ascending order. Therefore, when it is necessary to remove duplicates but inconvenient to open the array, you can use set to solve it.

extend

The elements in set are unique. If you need to deal with non unique cases, you need to use multiset. In addition, unordered is added to the C++ 11 standard_ Set, which replaces the implementation of Red Black Tree (a self balanced binary search tree) in set with hash, so that it can be used to deal with the requirements of only de duplication but no sorting, which is faster than set.

Topics: C++ Back-end