STL set & multiset insert and pair usage

Set insertion and pair usage

Pair represents a pair, which treats two values ​​as a unit and binds the two values ​​together.
pair <T1, T2> is used to store two types of values, which can be different or the same. For example, T1 is int and T2 is float. T1, T2 can also be a custom class.

pair.first is the first value in the pair and is of type T1.
pair.second is the second value in the pair, which is of type T2.

Operating environment: vc ++ 2010 learning version
example:

#include <set>
#include <iostream>
#include <functional>
#include <algorithm>

using namespace std;

int main()
{
	set<int> setInt;

	for(int i = 5; i > 0; i--)
	{
		pair<set<int>::iterator, bool> ret = setInt.insert(i);
		if(ret.second)
		{
			cout<<"插入 "<<i<<" 成功! "<<endl;
		}else
		{
			cout<<"插入 "<<i<<" 失败! "<<endl;
		}
	}
	
	//bool ret = setInt.insert(5); 返回的并不是布尔类型
	pair<set<int>::iterator, bool> ret = setInt.insert(5);

	cout<<"第一个: "<<*(ret.first)<<endl;
	cout<<"第二个: "<<ret.second<<endl;
	
	system("pause");
	return 0;
}

operation result:
Insert picture description here

Published 14 original articles · Like1 · Visits 119

Guess you like

Origin blog.csdn.net/m0_45867846/article/details/105458287