STL_set and multiset containers

1. Introduction to set/multiset

A set is a collection container, the elements contained in it are unique , and the elements in the collection are arranged in a certain order . The element insertion process is inserted according to the sorting rules , so the insertion position cannot be specified.

Set adopts the data structure of the red-black tree variant. The red-black tree belongs to the balanced binary tree. It is faster than vector in insert and delete operations .

Set cannot directly access elements. (You cannot use at.(pos) and [] operators).

The difference between multiset and set:

  • Set supports unique key values, and each element value can only appear once ;
  • The same value can appear multiple times in a multiset .

You cannot directly modify the element values ​​in set or multiset containers, because this type of container is automatically sorted. If you want to modify the value of an element, you must delete the original element first, and then insert the new element.

#include <set>

Two, set/multiset constructor

set<T> st;//set default constructor:

mulitset<T> mst; //multiset default constructor

set(const set &st); //copy constructor

set<int> setInt;      //一个存放int的set容器。
multiset<int> mulsetInt;      //一个存放int的multiset容器。
set<int> setIntB(setIntA); //1 3 5 7 9

Three, set insertion and iterator

set.insert(elem); //Insert an element in the container.

set.begin(); //Return the iterator of the first data in the container.

set.end(); //Return the iterator after the last data in the container.

set.rbegin(); //Returns the iterator of the last element in the container.

set.rend(); //Returns the iterator behind the last last element in the container.

set<int> setInt;
setInt.insert(3); 
setInt.insert(1);
setInt.insert(5);
setInt.insert(2);

for(set<int>::iterator it=setInt.begin(); it!=setInt.end(); ++it){
    
    

   int iItem = *it;
   cout << iItem;  //或直接使用cout << *it
}
//这样子便顺序输出 1 2 3 5(默认排序,从小到大)

Fourth, the sorting of elements in the set collection

set<int,less<int>> setIntA; //The container arranges elements in ascending order.

set<int,greater<int>> setIntB; //The container arranges elements in descending order.

set<int> 相当于 set<int,less<int>>。

The int in less<int> and greater<int> can be changed to other types, and this type is mainly consistent with the data type contained in the set.

set<int,greater<int>> setIntB;
setIntB.insert(3);
setIntB.insert(1);
setIntB.insert(5);
setIntB.insert(2);
//此时容器setIntB就包含了按顺序的5,3,2,1元素

Five, the usage of function object functor

Although function pointers are widely used to implement function callbacks, C++ also provides an important method for implementing callback functions, which is the function object.

functor, translated into function object, pseudo function, operator, is an ordinary class object overloaded with the "()" operator. Syntactically speaking, it behaves like ordinary functions.

greater<> and less<> are function objects.

//下面举出greater<int>的简易实现原理。
struct greater{
    
    
	bool operator() (const int& iLeft, const int& iRight){
    
    
   		return (iLeft>iRight);  //如果是实现less<int>的话,这边是写return (iLeft<iRight);
	}
}
//容器就是调用函数对象的operator()方法去比较两个值的大小。

Six, set object assignment

set& operator=(const set &st); //Overload the equal sign operator

set.swap(st); //Exchange two collection containers

	set<int> setIntA;
	set<int> setIntC;
    
	setIntA.insert(3);
    setIntA.insert(1);
    setIntA.insert(7);
    setIntA.insert(5);
	setIntA.insert(9);    
    
    setIntC = setIntA;       //1 3 5 7 9 
    setIntC.insert(6);
    setIntC.swap(setIntA);    //交换

Seven, the size of the set

set.size(); //Returns the number of elements in the container

set.empty();//Determine whether the container is empty

set<int> setIntA;
setIntA.insert(3);
setIntA.insert(1);
setIntA.insert(7);
if (!setIntA.empty()){
    
    
   int iSize = setIntA.size();      //3
}

8. Deletion of set

set.clear(); //Clear all elements

set.erase(pos); //Delete the element pointed to by the pos iterator and return the iterator of the next element.

set.erase(beg,end); //Delete all elements in the interval [beg,end), and return an iterator of the next element.

set.erase(elem); //Delete the element whose value is elem in the container.

//删除区间内的元素

//setInt是用set<int>声明的容器,现已包含按顺序的1,3,5,6,9,11元素。
set<int>::iterator itBegin=setInt.begin();
++ itBegin;
set<int>::iterator itEnd=setInt.begin();
++ itEnd;
++ itEnd;
++ itEnd;
setInt.erase(itBegin,itEnd);
//此时容器setInt包含按顺序的1,6,9,11四个元素。

//删除容器中第一个元素
setInt.erase(setInt.begin());       //6,9,11

//删除容器中值为9的元素
set.erase(9);   

//删除setInt的所有元素
setInt.clear();          //容器为空

Nine, set search

set.find(elem); //Find the elem element and return an iterator pointing to the elem element; if it does not exist, return set.end()

set.count(elem); //Returns the number of elements whose value is elem in the container. For set, it is either 0 or 1. For multiset, the value may be greater than 1.

set.lower_bound(elem); //Return the iterator of the first >=elem element.

set.upper_bound(elem); // Returns an iterator of the first >elem element.

set.equal_range(elem); //Returns two iterators of the upper and lower bounds equal to elem in the container. The upper limit is a closed interval, and the lower limit is an open interval, such as [beg,end). Two iterators are returned, and these two iterators are encapsulated in pair.

	set<int> s1;
	s1.insert(7);
	s1.insert(2);
	s1.insert(4);
	s1.insert(5);
	s1.insert(1);

	set<int>::iterator ret = s1.find(14);
	if (ret == s1.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "ret:" << *ret << endl;
	}

	//找第一个大于等于key的元素
	ret = s1.lower_bound(2);
	if (ret == s1.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "ret:" << *ret << endl;
	}

	//找第一个大于key的值
	ret = s1.upper_bound(2);
	if (ret == s1.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "ret:" << *ret << endl;
	}

	//equal_range 返回Lower_bound 和 upper_bound值
	pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(2);
	if (myret.first == s1.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "myret:" << *(myret.first) << endl;
	}

	if (myret.second == s1.end()) {
    
    
		cout << "没有找到!" << endl;
	}
	else {
    
    
		cout << "myret:" << *(myret.second) << endl;
	}
/*
结果:
没有找到!
ret:2
ret:4
myret:2
myret:4
*/

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/113127694