天天看點

【C++】set/multiset 容器的基操set/multiset 容器

set/multiset 容器

set基本概念

  • 所有元素都會在插入時自動被排序
  • set/multiset屬于關聯式容器,底層結構是用二叉樹實作
  • set個multiset差別:
    • set不允許容器中有重複元素
    • multiset允許容器中有重複元素

set構造和指派

/*
set構造和指派
	有序不可重複
	set<T> name;			預設構造函數
	set(const set& name);	拷貝構造函數

	set& operator=(const set& name);
*/
void test01()
{
    //預設構造函數
	set<int> s1;
	s1.insert(4);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	s1.insert(1);
	printSet(s1);	//1 2 3 4	

    //拷貝構造函數
	set<int> s2(s1);
	printSet(s2);	//1 2 3 4	

    //=指派
	set<int> s3;
	s3 = s2;
	printSet(s3);	//1 2 3 4
}
           

set大小和交換

/*
set大小和交換
	size();			//傳回容器中元素的數目
	empty();		//判斷是否為空
	swap(set);		//交換兩個集合中的資料
*/
void test02()
{
	set<int> s1;
	s1.insert(4);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	s1.insert(1);
	printSet(s1);	//1 2 3 4	

	set<int> s2;
	s2.insert(9);
	s2.insert(5);
	printSet(s2);	//5 9
	
	//資料交換
	cout << "交換後:" << endl;
	s1.swap(s2);
	printSet(s1);	//5 9
	printSet(s2);	//1 2 3 4

	//計算容器大小
	cout << "大小:" << s1.size() << endl;	//2

	//判斷容器是否為空
	cout << s1.empty() << endl;				//0
}
           

set插入和删除

/*
set插入和删除
	insert(elem);		插入資料
	clear();			清空資料

	erase(pos);			删除某位置元素
	erase(beg,end);		删除區間元素
	erase(elem);		删除容器中elem元素
*/
void test03()
{
	set<int> s1;
	s1.insert(8);
	s1.insert(7);
	s1.insert(6);
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	s1.insert(1);

	printSet(s1);	//1 2 3 4 5 6 7 8
	s1.erase(s1.begin());
	printSet(s1);	//2 3 4 5 6 7 8
	
	s1.erase(++s1.begin(), --s1.end());
	printSet(s1);	//2 8

	s1.erase(8);
	printSet(s1);	//2

	s1.clear();
	printSet(s1);	//空
}
           

set查找和統計

  • set中資料不可重複,是以count函數傳回值為0或1
  • multiset中資料可重複,是以count函數傳回值為相應值的個數
/*
set查找與統計
	find(key);		查找key是否存在,若存在傳回該鍵的元素的疊代器;若不存在傳回set.end()
	count(key);		統計傳回key的個數
*/
void test04()
{
	set<int> s1;
	s1.insert(8);
	s1.insert(7);
	s1.insert(6);
	s1.insert(5);
	s1.insert(4);
	s1.insert(3);
	s1.insert(2);
	s1.insert(1);

	set<int>::iterator it = s1.find(5);
	if (it != s1.end())
		cout << "找到了,在" << *it << "位置上" << endl;
	else
		cout << "找不到!" << endl;

	int set_num = s1.count(6);
	cout << "6的個數:" << set_num << endl;		//1

	multiset<int> ms;
	ms.insert(1);
	ms.insert(1);
	ms.insert(1);
	int ms_num = ms.count(1);
	cout << "1的個數:" << ms_num << endl;		//3
}
           

set和multiset差別

  • set插入資料時,會傳回插入結果,表示是否插入成功
  • multiset插入資料時,不會傳回任何東西,不做任何檢測
/*
set的insert方法傳回值:_Pairib insert(value_type&& _Val)
					typedef pair<iterator, bool> _Pairib;
multiset的insert方法傳回值:iterator insert(value_type&& _Val)
*/
void test05()
{
	set<int> s1;
	pair<set<int>::iterator,bool> ret = s1.insert(1);
	if (ret.second)
		cout << "第一次插入成功!" << endl;
	else
		cout << "第一次插入失敗!" << endl;

	ret = s1.insert(1);
	if (ret.second)
		cout << "第二次插入成功!" << endl;
	else
		cout << "第二次插入失敗!" << endl;

	multiset<int> ms1;
	multiset<int>::iterator it = ms1.insert(1);
	cout << *it << endl;
	 
	it = ms1.insert(3);
	cout << *it << endl;
}
           

pair對組建立

/*
對組的建立
	預設構造方法
	make_pair
交換對組
	swap(pair);
*/
void test06()
{
	pair<string, int> p1("Tom", 18);
	pair<string, int> p2 = make_pair("Jerry", 18);
	cout << "Name:" << p1.first << " Age:" << p1.second << endl;
	cout << "Name:" << p2.first << " Age:" << p2.second << endl;

	p1.swap(p2);
	cout << "交換後:" << endl;
	cout << "Name:" << p1.first << " Age:" << p1.second << endl;
	cout << "Name:" << p2.first << " Age:" << p2.second << endl;
}
           

改變set容器排序規則

/*
利用仿函數重寫set内部排序方法
*/
class MySort{
public:
	bool operator()(int v1,int v2){
		return v1 > v2;
	}
};
void test07()
{
	set<int,MySort> s1;
	s1.insert(1);
	s1.insert(2);
	s1.insert(3);
	s1.insert(4);
	for each (int var in s1)
		cout << var << " ";
	cout << endl;
}
           
  • 自定義類型需要指定排序規則
class Dog
{
public:
	Dog(){}
	~Dog(){}
	Dog(int age, int weight){
		this->age = age;
		this->weight = weight;
	}
	int getAge(){
		return this->age;
	}
	int getWeight(){
		return this->weight;
	}
	friend ostream& operator<<(ostream& os, Dog& d);
private:
	int age;
	int weight;
};
ostream& operator<<(ostream& os, Dog& d){
	cout << d.getAge() << " " << d.getWeight() << endl;
	return os;
}
class CompareDog{
public:
	bool operator()(Dog d1,Dog d2){
		return d1.getAge() > d2.getAge();
	}
};
void test08()
{
	set<Dog, CompareDog> s1;
	Dog d1(1, 2), d2(2, 3), d3(2, 4), d4(3, 4);
	s1.insert(d1);
	s1.insert(d2);
	s1.insert(d3);
	s1.insert(d4);

	for each (Dog var in s1)
	{
		cout << var;
	}
	cout << endl;
}
           

繼續閱讀