天天看點

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

文章目錄

  • 1. set
    • 1.1 特點
    • 1.2 構造與指派
      • ① 函數原型
      • ② 函數原型
      • 代碼展示
    • 1.3 大小交換
      • ① 函數原型
      • ② 代碼展示
      • ③ 測試結果
    • 1.4 插入删除
      • ① 函數原型
      • ② 代碼展示
      • ③ 測試結果
    • 1.5 查找和統計
      • ① 函數原型
      • ② 代碼展示
      • ③ 測試結果
    • 1.6 排序規則
    • Ⅰ. 内置資料類型排序
      • ① 代碼展示
      • ② 測試結果
    • Ⅱ. 自定義資料類型排序
      • ① 代碼展示
      • ②測試結果
  • 2. multiset與set差別
    • ① 差別
    • ② 代碼測試
    • ③ 測試結果
  • 3. pair隊組建立
    • 3.1 函數原型
    • 3.2 代碼展示
    • 3.3 測試結果
  • 最後,感謝大家支援u (^ _ ^)

1. set

1.1 特點

  • set容器插入時容器的所有元素都會自動排序【自動排序】
  • set容器不允許插入重複的值【自動去重】

1.2 構造與指派

① 函數原型

  • 構造
    • set<T> st;

      預設構造函數
    • set(const set &st);

      拷貝構造函數
  • 指派
    • set& operator=(const set &st);

      重載等号操作符

② 函數原型

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

	//insert插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	
	//周遊容器
	printSet(s1);

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

	//指派操作
	set<int> s3;
	s3 = s2;
	printSet(s3);
}

int main() {
	text01();
	return 0;
}
           

代碼展示

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

1.3 大小交換

① 函數原型

  • size();

    傳回容器中元素的數目
  • empty();

    判空
  • swap(st);

    交換兩個集合容器

② 代碼展示

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

void text02() {
	set<int> s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	printSet(s1);

	//判空
	if (s1.empty()) cout << "容器為空" << endl;
	else cout << "容器不為空" << endl;

	//大小
	cout << "大小為: " << s1.size() << endl;

	//交換
	set<int> s2;
	for (int i = 0; i < 10; i++) {
		s2.insert(i);
	}
	cout << "s1為: "; printSet(s1);
	cout << "s2為: "; printSet(s2);
	swap(s1, s2);
	cout << "s1為: "; printSet(s1);
	cout << "s2為: "; printSet(s2);
}
int main() {
	//text01();
	text02();
	return 0;
}
           

③ 測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

1.4 插入删除

① 函數原型

  • insert(elem);

    插入元素
  • clear();

    清楚元素
  • erase(pos);

    删除pos疊代器所指的元素,傳回下一個元素的疊代器
  • erase(beg, end);

    删除區間[beg, end)的所有元素,傳回下一個元素的疊代器
  • erase(elem);

    删除容器中值為elem的元素

② 代碼展示

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

void text03() {
	set<int> s1;
	//插入元素
	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);
	set<int>s2(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);
	s1.erase(30);
	printSet(s1);

	//清空
	s1.erase(s1.begin(), s1.end());
	printSet(s1);
	s2.clear();
	printSet(s2);
}
int main() {
	//text01();
	//text02();
	text03();
	return 0;
}
           

③ 測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

1.5 查找和統計

① 函數原型

  • find(key);

    查找key是否存在
    • 若存在,傳回該鍵的疊代器
    • 否則,傳回set.end()
  • count(key);

    統計key的個數

② 代碼展示

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

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

	//查找
	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end()) cout << "找到該元素" << endl;
	else cout << "未找到該元素" << endl;

	//統計【自動去重後,統計結果為0/1】
	int cnt = s1.count(30);
	cout << "30的個數為: " << cnt << endl;
}
int main() {
	//text01();
	//text02();
	//text03();
	text04();
	return 0;
}
           

③ 測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

1.6 排序規則

Ⅰ. 内置資料類型排序

① 代碼展示

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

class MyCompare { 
public:
	bool operator()(int x, int y) const {	//	第一個()表示重載的符号,第二個()表示重載函數的參數清單
		return x > y;  //由大到小排的仿函數,傳回類型為bool類型
	}
};
void text07() {
	set<int> s1;
	s1.insert(60);
	s1.insert(30);
	s1.insert(80);
	s1.insert(50);
	s1.insert(10);
	s1.insert(20);
	
	//預設從小到大排
	printSet(s1);    
	
	//利用仿函數從大到小排
	set<int, MyCompare> s2;
	s2.insert(60);
	s2.insert(30);
	s2.insert(80);
	s2.insert(50);
	s2.insert(10);
	s2.insert(20);
	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << (*it) << " ";
	} cout << endl;
}
int main(){
	text08();
	return 0;
}
           
非靜态成員函數後面加const(加到非成員函數或靜态成員後面會産生編譯錯誤),表示成員函數隐含傳入的this指針為const指針,決定了在該成員函數中,任意修改它所在的類的成員的操作都是不允許的(因為隐含了對this指針的const引用);容器這邊大部分類似,聽着有點枯燥。但不加const的成員函數隻能被非const對象調用。

② 測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

Ⅱ. 自定義資料類型排序

① 代碼展示

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;
class Player {
public:
	Player(string name, int age) {
		this->m_name = name;
		this->m_age = age;
	}
public:
	string m_name;
	int m_age;
};

class cmp {
public:
	bool operator()(const Player& p1, const Player& p2) const{
		return p1.m_age > p2.m_age; //按年齡降序排
	}
};

void text08() {
	set<Player, cmp> s;
	Player p1("詹姆斯", 38);
	Player p2("杜蘭特", 34);
	Player p3("戴維斯", 33);
	Player p4("利拉德", 35);
	Player p5("東契奇", 25);
	
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);

	for (set<Player, cmp>::iterator it = s.begin(); it != s.end(); it++) {
		cout << (*it).m_name << "  " << (*it).m_age << endl;
	}

	//set預設從小到大排,自定義資料類型無法确定按什麼類進行排序
}
int main(){
	text08();
	return 0;
}
           

②測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

2. multiset與set差別

① 差別

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

② 代碼測試

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;

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

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

void text05() {
	set<int> s;
	s.insert(10);
	s.insert(10);
	printSet(s);

	pair< set<int>::iterator, bool > ret = s.insert(10);
	//pair相當于傳回兩個資料類型的函數,一個是疊代器,一個是布爾類型
	if (ret.second) // .second 就是第二個布爾的傳回類型
		cout << "第一次插入成功" << endl;
	else
		cout << "第一次插入失敗" << endl;

}
int main() {
	//text01();
	//text02();
	//text03();
	//text04();
	text05();
	text06();
	return 0;
}
           

③ 測試結果

  1. set
    Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)
    Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)
  2. multiset
    Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

3. pair隊組建立

3.1 函數原型

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

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

3.2 代碼展示

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

void text01() {
	//第一種——使用"點"運算符進行操作
	pair<string, int> p("James", 38);
	cout << "姓名: " << p.first << " 年齡: " << p.second << endl;

	//第二種——用make_pair()創造隊組
	pair<string, int> p2 = make_pair("Curry", 34);
	cout << "姓名: " << p2.first << " 年齡: " << p2.second << endl;
}

int main() {
	text01();
	return 0;
}
           

3.3 測試結果

Day17 C++STL入門基礎知識十——set、multiset容器、pair組隊 基本概念-構造函數-指派變換-大小操作-插入删除-資料存取-反轉排序 【全面深度剖析+例題代碼展示】1. set2. multiset與set差別3. pair隊組建立最後,感謝大家支援u (^ _ ^)

最後,感謝大家支援u (^ _ ^)

如果感覺這篇文章對你有幫助的話,不妨三連支援下,十分感謝(✪ω✪)。

繼續閱讀