当前位置:网站首页>STL tutorial 7-set, pair pair pair group and functor

STL tutorial 7-set, pair pair pair group and functor

2022-06-30 16:49:00 Sleepy snail

One 、set Containers

  • set and multiset It's a collection container , among set The contained elements are unique , The elements in the collection are automatically arranged in a certain order .set Using the data structure of red black tree variant to realize , Red black trees belong to balanced binary trees . It's better than... In insert and delete operations vector fast , stay n The efficiency of finding the target number in the number is log 2 n
  • set In the container Duplicate elements are not allowed ( The Chinese name set means , There can only be one ),multiset Allow repeating elements .
  • Only available Insert Method initialization
  • set/multiset The feature of is that all elements are automatically sorted according to the value of the element
  • set A value cannot be modified , If you want to modify, you can only delete and add

1、set Constructors

set<int> s1;// Automatically sort , Default from small to large .
s1.insert(7);
s1.insert(2);
s1.insert(3);// At this time, it will be printed  2 3 7, Has been automatically sorted 

2、set Assignment operation

	set<int> s2;
	s2 = s1;
	s2.swap(s1);//swap Swap collection containers 

3、set Size operation

size();// Get the number of container elements 
empty();// Determine whether the inner container is empty 

4、set Insert and delete operations

insert(elem);// Insert elements into the container 
clear();// Remove all elements 
erase(pos);// Delete the element that the iterator refers to , Both of these are iterators 
erase(beg,end);// Delete element of interval 
erase(elem);// Delete the value in the container as elem The elements of , Yes delete one , Although there is only one 

5、set Lookup ( important )

s1.find(4) look for 4 Iterator of position
lower_bound(2) Find the first one greater than or equal to 2 Of ( Elements ) Iterator value
s1.upper_bound(2); Find the first one greater than K Value

	set<int> s1;
	// Insert several elements 
	s1.insert(7);
	s1.insert(2);
	s1.insert(4);
	s1.insert(5);
	s1.insert(1);
	s1.insert(9);
    
    // Use iterators to find elements from scratch 
    //find Function to find elements , An iterator that returns the position of an element , If no return is found end() The iterator 
	set<int>::iterator ret = s1.find(4);
	
	// Next, determine whether the element is found 
	if (ret == s1.end())
	{
    
		cout << " Can't find " << endl;
	}
	else
	{
    
		cout << "ret:" << *ret << endl;
	}
//---------------------------------------------------------------
	//lower_bound(2) Find the first one greater than or equal to k Of ( Elements ) Iterator value 
	ret = s1.lower_bound(2);
	if (ret == s1.end())
	{
    
		cout << " Can't find " << endl;
	}
	else
	{
    
		cout << "ret:" << *ret << endl;
	}

//---------------------------------------------------------------
	// Find the first one greater than K Value 
	ret = s1.upper_bound(2);
	if (ret == s1.end())
	{
    
		cout << " Can't find " << endl;
	}
	else
	{
    
		cout << "ret:" << *ret << endl;
	}

	//equal_range  return Lower_bound  and  upper_bound value 
	pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(2);
	// It's used here pair We will add the relevant contents of the group below .
	/*myret.first; myret.second;*/
	if (myret.first == s1.end())
	{
    
		cout << "can't find" << endl;
	}
	else
	{
    
		cout << "myret:" << *myret.first << endl;// return Lower_bound
	}
	if (myret.second == s1.end())
	{
    
		cout << "can't find" << endl;
	}
	else
	{
    
		cout << "myret:" << *myret.second << endl; // return upper_bound
	}

6、 modify set Sort order of

Look at the following functor

set<int, mycompare> s3;// After using the functor here , The default insertion order will be modified during insertion 
	s3.insert(7);
	s3.insert(2);
	s3.insert(4);
	s3.insert(5);
	s3.insert(1);
	s3.insert(9);

7、set Add objects to

set Adding elements requires sorting , But if you sort directly ,set If you don't know how to arrange, you will report an error , So we also need an affine function

// Define a class 
class Person
{
    
public:
	Person(int age, int id):id(id),age(age){
    }
public:
	int id;
	int age;
};
// Define a functor , Sort the objects according to their age 
class mycompare2
{
    
public: 
	bool operator()(Person p1, Person p2)const
	{
    
		return p1.age > p2.age;
	}
};
void test(){
    
	// Use functor to customize sorting 
	set<Person,mycompare2> sp;
	Person p1(10, 20), p2(30, 40), p3(50, 60);
	sp.insert(p1);
	sp.insert(p2);
	sp.insert(p3);
}

It should be noted that , If you use find, At this point, you should look for the element to compare , For example, it is used above age To compare , This is also used when searching age Go find , This age It's like a key . For example, the following code , Although I didn't join p4, however p4 Inside 40 Is already joined , So we can find

	Person p4(10, 40);
	auto ret = sp.find(p4);//set<Person,mycompare2>::iterator
	if (ret == sp.end())
	{
    
		cout << "can't find" << endl;
	}
	else
	{
    
		cout << "find:" << (*ret).id << " " << (*ret).age << endl;
	}

Two 、pair

pair Yes, it will 2 Data is combined into a set of data , When you need such a requirement, you can use pair, Such as stl Medium map Will be key and value Put them together to keep . Another application is , When a function needs to return 2 Data , You can choose pair.

// Law 1 
	pair<string, int> pair1(string("number"), 20);
	cout << pair1.first << " " << pair1.second << endl;
	
	// perhaps pair<string,int> pair2=make_pair("name",30)
	pair<string, int> pair2 = make_pair("name", 30);
	cout << pair2.first << " " << pair2.second << endl;

	//pair assignment 
	pair<string, int> pair3 = pair2;
	cout << pair2.first << " " << pair2.second << endl;

3、 ... and 、 functor

// functor 
class mycompare
{
    
public:
	bool operator()(int v1,int v2)const
	{
    
		return v1 > v2;
	}
};
原网站

版权声明
本文为[Sleepy snail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206301435554880.html