1. 程式人生 > 實用技巧 >STL—set/multiset容器

STL—set/multiset容器

set/ multiset 容器

1 set基本概念

簡介:

  • 所有元素都會在插入時自動被排序

本質:

  • set/multiset屬於關聯式容器,底層結構是用二叉樹實現。

set和multiset區別

  • set不允許容器中有重複的元素
  • multiset允許容器中有重複的元素

2 set構造和賦值

功能描述:建立set容器以及賦值

構造:

  • set<T> st; //預設建構函式:
  • set(const set &st); //拷貝建構函式

賦值:

  • set& operator=(const set &st);
    //過載等號操作符

示例:

#include <set>

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//構造和賦值
void test01()
{
	set<int> s1;

    //set容器只能用insert方式插入資料,並且插入時會自動排序
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	//拷貝構造
	set<int>s2(s1);
	printSet(s2);

	//賦值
	set<int>s3;
	s3 = s2;
	printSet(s3);
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

  • set容器插入資料時用insert
  • set容器插入資料的資料會自動排序

3 set大小和交換

功能描述:

  • 統計set容器大小以及交換set容器

函式原型:

  • size(); //返回容器中元素的數目
  • empty(); //判斷容器是否為空
  • swap(st); //交換兩個集合容器

示例:

#include <set>

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//大小
void test01()
{

	set<int> s1;
	
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

	if (s1.empty())
	{
		cout << "s1為空" << endl;
	}
	else
	{
		cout << "s1不為空" << endl;
		cout << "s1的大小為: " << s1.size() << endl;
	}

}

//交換
void test02()
{
	set<int> s1;

	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

	set<int> s2;

	s2.insert(100);
	s2.insert(300);
	s2.insert(200);
	s2.insert(400);

	cout << "交換前" << endl;
	printSet(s1);
	printSet(s2);
	cout << endl;

	cout << "交換後" << endl;
	s1.swap(s2);
	printSet(s1);
	printSet(s2);
}

int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}

總結:

  • 統計大小 --- size
  • 判斷是否為空 --- empty
  • 交換容器 --- swap

4 set插入和刪除

功能描述:

  • set容器進行插入資料和刪除資料

函式原型:

  • insert(elem); //在容器中插入元素。
  • clear(); //清除所有元素
  • erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。
  • erase(beg, end); //刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。
  • erase(elem); //刪除容器中值為elem的元素。

示例:

#include <set>

void printSet(set<int> & s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//插入和刪除
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	//刪除
	s1.erase(s1.begin());
	printSet(s1);

	s1.erase(30);
	printSet(s1);

	//清空
	//s1.erase(s1.begin(), s1.end());
	s1.clear();
	printSet(s1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

  • 插入 --- insert
  • 刪除 --- erase
  • 清空 --- clear

5 set查詢和統計

功能描述:

  • 對set容器進行查詢資料以及統計資料

函式原型:

  • find(key); //查詢key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();
  • count(key); //統計key的元素個數

示例:

#include <set>

//查詢和統計
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	
	//查詢
	set<int>::iterator pos = s1.find(30);

	if (pos != s1.end())
	{
		cout << "找到了元素 : " << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//統計
	int num = s1.count(30);
	cout << "num = " << num << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

  • 查詢 --- find (返回的是迭代器)
  • 統計 --- count (對於set,結果為0或者1)

6 set和multiset區別

學習目標:

  • 掌握set和multiset的區別

區別:

  • set不可以插入重複資料,而multiset可以
  • set插入資料的同時會返回插入結果,表示插入是否成功
  • multiset不會檢測資料,因此可以插入重複資料

示例:

#include <set>

//set和multiset區別
void test01()
{
	set<int> s;
	pair<set<int>::iterator, bool>  ret = s.insert(10);//將這裡的insert方法選中轉到定義檢視原始碼
	if (ret.second) {
		cout << "第一次插入成功!" << endl;
	}
	else {
		cout << "第一次插入失敗!" << endl;
	}

	ret = s.insert(10);
	if (ret.second) {
		cout << "第二次插入成功!" << endl;
	}
	else {
		cout << "第二次插入失敗!" << endl;
	}
    
	//multiset
	multiset<int> ms;
	ms.insert(10);//將這裡的insert方法選中轉到定義檢視原始碼
	ms.insert(10);

	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

  • 如果不允許插入重複資料可以利用set
  • 如果需要插入重複資料利用multiset

7 pair對組建立

功能描述:

  • 成對出現的資料,利用對組可以返回兩個資料

兩種建立方式:

  • pair<type, type> p ( value1, value2 );
  • pair<type, type> p = make_pair( value1, value2 );

示例:

#include <string>

//對組建立
void test01()
{
	pair<string, int> p(string("Tom"), 20);
	cout << "姓名: " <<  p.first << " 年齡: " << p.second << endl;

	pair<string, int> p2 = make_pair("Jerry", 10);
	cout << "姓名: " << p2.first << " 年齡: " << p2.second << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

兩種方式都可以建立對組,記住一種即可

8 set容器排序

學習目標:

  • set容器預設排序規則為從小到大,掌握如何改變排序規則

主要技術點:

  • 利用仿函式,可以改變排序規則

示例一 set存放內建資料型別

#include <set>

class MyCompare 
{
public:
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};
void test01() 
{    
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(50);

	//預設從小到大
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//指定排序規則
	set<int,MyCompare> s2;
	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);

	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:利用仿函式可以指定set容器的排序規則

示例二 set存放自定義資料型別

#include <set>
#include <string>

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;

};
class comparePerson
{
public:
	bool operator()(const Person& p1, const Person &p2)
	{
		//按照年齡進行排序  降序
		return p1.m_Age > p2.m_Age;
	}
};

void test01()
{
    //自定義的資料型別,一般都會指定排序規則
	set<Person, comparePerson> s;

	Person p1("劉備", 23);
	Person p2("關羽", 27);
	Person p3("張飛", 25);
	Person p4("趙雲", 21);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

	for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名: " << it->m_Name << " 年齡: " << it->m_Age << endl;
	}
    	//姓名: 關羽    年齡: 27
		//姓名: 張飛    年齡: 25
		//姓名: 劉備    年齡: 23
		//姓名: 趙雲    年齡: 21
}
int main() {

	test01();

	system("pause");

	return 0;
}

總結:

對於自定義資料型別,set必須指定排序規則才可以插入資料