STL(标准模板库)笔记——平衡二叉树set

STL(标准模板库)笔记---平衡二叉树set

本系列是观看北大郭炜老师程序与算法课程的笔记,用于复习与巩固。

set

set的用法

  • set和multiset的区别在于容器里不能有重复元素
    a和b重复 = “a必须排在b前面” 和“b必须排在a前面”都不成立
  • set插入元素可能不成
#include <iostream> 
#include <cstring> 
#include <set> 
using namespace std; 
int main() 
{ 
	set<int> st; 
	int a[10] ={ 1,2,3,8,7,7,5,6,8,12 }; 
	for(int i = 0;i < 10; ++i) 
		st.insert(a[i]); 
	cout << st.size() << endl;  //输出:8 
	set<int>::iterator i; 
	for(i = st.begin(); i != st.end(); ++i) cout << * i << ","; //输出:1,2,3,5,6,7,8,12, 
	cout << endl; 
	pair<set<int>::iterator, bool> result =  st.insert(2); 
	if( ! result.second ) //条件成立说明插入不成功 
		cout << * result.first <<" already exists." << endl; 
	else 
		cout << * result.first << " inserted." << endl; 
return 0;
/*输出: 2 already exists*/
/*
pair<set<int>::iterator, bool> 
=
struct { 
	set<int>::iterator first; 
	bool second; 
};
*/
}	
pair<T1,T2>类型等价于:
struct { 
	T1 first; 
	T2 second; 
};
例如:pair<int, double> a;  
等价于:
struct { 
	int first; 
	double second; 
} a; 
a.first = 1; 
a.second = 93.93; 
原文地址:https://www.cnblogs.com/shuai841952238/p/13154686.html