關聯容器
關聯容器和順序容器有著根本的不同:關聯容器中的元素是按關鍵字來保存和訪問的,而順序容器中的元素是按它們在容器中的位置來順序保存和訪問的。
關聯容器支持高效的關鍵字查找和訪問。
兩個主要的關聯容器(associative-container),set和map。
set 中每個元素只包含一個關鍵字。set 支持高效的關鍵字查詢操作一一檢查一個給定關鍵字是否在 set 中。
map 的元素是關鍵字-值 (key-value)對(也稱鍵-值對)。其中關鍵字起到索引的作用,值則表示與索引相關聯的數據。字典是一個很好的使用 map 的例子:可以將單詞作為關鍵字,將單詞釋義作為值。
標準庫針對set和map一共提供8種不同的關聯容器。1.是否運行關鍵字重復;2.是否按順序保存元素
set和multiset定義在頭文件set中;map和multimap定義在頭文件map中;無序容器則定義在頭文件unordered set和unordered map中。?
pair 數對是什么
pair數對是 C++ 標準庫中的一個模板類,它提供了一種簡單的方式來將兩個值組合成一個單元。這兩個值可以是相同類型,也可以是不同類型。就好比一個小巧的容器,專門用來存放兩個緊密相關的數據元素。例如,在處理平面直角坐標系中的點時,我們可以用一個pair來同時存儲點的橫坐標和縱坐標;在統計單詞出現頻率時,pair能將單詞及其對應的出現次數放在一起。
使用時需要引用文件
#include <utility>//通用工具
namespace std {template <typename T1,typename T2>struct pair {T1 first; //成員1T2 second;//成員2...};
}
操作函數
pair應用舉例
pair的基本操作舉例:
#include <iostream>
#include <utility>
using namespace std;int main()
{pair<int, double>p1;//創建一個空的pair//注意訪問first和second時沒有()cout << "p1=(" << p1.first << "," << p1.second << ")" << endl;pair<int, double>p2(10, 23.4);//創建一個pair,數據為(10,23.4)cout << "p2=(" << p2.first << "," << p2.second << ")" << endl;pair<int, double>p3(p2);//通過p2拷貝構造一個p3p3.first = 100;cout << "p3=(" << p3.first << "," << p3.second << ")" << endl;p1 = p3;//把p3賦值給p1cout << "p1 = p3后" << endl;cout << "p1=(" << p1.first << "," << p1.second << ")" << endl;p3 = make_pair(100, 200);cout << "p3 = make_pair(100,200)后" << endl;//通過get訪問p3的成員cout << "p3=(" << get<0>(p3) << "," << get<1>(p3) << ")" << endl;return 0;
}
作為函數參數
pair
數對可以作為函數參數,使得函數能夠方便地接收一組相關的數據。
#include <iostream>
#include <cmath>
#include <utility>
using namespace std;double distance(pair<double, double> point1, pair<double, double> point2) {double dx = point2.first - point1.first;double dy = point2.second - point1.second;return sqrt(dx * dx + dy * dy);
}int main() {pair<double, double> p1(1.0, 2.0);pair<double, double> p2(4.0, 6.0);cout << "Distance between points: " << distance(p1, p2) << endl;return 0;
}
作為函數返回值
函數也可以返回pair
數對,這樣就能一次性返回兩個相關的結果。
#include <iostream>
#include <utility>
using namespace std;
pair<int, int> MinMax(int* arr, int len)//找到數組的最小值和最大值
{int min = arr[0]; //最小值int max = arr[0];//最大值for (int i = 1; i < len; i++){if (arr[i] < min)min = arr[i];if (arr[i] > max)max = arr[i];}return make_pair(min, max);
}int main()
{int arr[] = { 3,1,8,9,34,2,67,5,70,12,34,65,99,20 };auto p = MinMax(arr, sizeof(arr) / sizeof(arr[0]));cout << "最小值:" << p.first << ",最大值:" << p.second << endl;return 0;
}
pair 數對與 STL 容器的結合
pair 在 vector 中的應用
pair 在 vector 中的應用 vector是 C++ 中常用的動態數組容器,當需要存儲一組相關的數對時,vector與pair的結合非常實用。例如,統計單詞出現次數的場景:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;vector<pair<string, int>> countWords(const vector<string>& words) {vector<pair<string, int>> wordCount;for (const string& word : words) {auto it = find_if(wordCount.begin(), wordCount.end(),[&word](const pair<string, int>& p) {return p.first == word;});if (it != wordCount.end()) {++(it->second);}else {wordCount.emplace_back(word, 1);}}return wordCount;
}int main() {vector<string> sentence = { "apple", "banana", "apple", "cherry", "banana", "apple" };vector<pair<string, int>> result = countWords(sentence);for (const auto& pair : result) {cout << pair.first << ": " << pair.second << " 次" << endl;}return 0;
}
pair 在 map 中的應用
map
是一種關聯容器,它以鍵值對的形式存儲數據。實際上,map
的內部實現就是基于pair
的。map
的鍵和值分別對應pair
的first
和second
。
#include <iostream>
#include <map>
#include <string>using namespace std;int main() {map<string, int> ageMap;ageMap["Alice"] = 25;ageMap["Bob"] = 30;for (const auto& pair : ageMap) {cout << pair.first << " " << pair.second << "歲" << endl;}return 0;
}
這里ageMap
中的每一個元素本質上就是一個pair<string, int>
。
pair 數對的優勢與使用場景
代碼簡潔性
使用pair
數對可以避免創建復雜的自定義結構體來存儲兩個相關的數據。例如,在簡單的坐標表示或鍵值對存儲場景中,pair
能極大地簡化代碼結構,使代碼更加易讀。
數據關聯性
pair
明確地表達了兩個數據元素之間的關聯關系。在函數參數傳遞和返回值中,使用pair
能清晰地表明這兩個值是作為一個整體進行處理的,增強了代碼的語義性。
算法支持
C++ 標準庫中的許多算法,如排序、查找等,都對pair
提供了良好的支持。這使得在處理包含pair
的容器時,能夠方便地利用這些算法進行高效操作。
總結
pair
數對作為 C++ 標準庫中的一個小巧而強大的工具,為我們在編程過程中處理相關數據提供了便捷的方式。從基本的定義、初始化和訪問,到在函數參數、返回值以及與 STL 容器的結合應用,pair
數對都展現出了其獨特的優勢。它不僅能讓代碼更加簡潔明了,還能提升代碼的可讀性和可維護性。在日常編程中,當你遇到需要處理兩個相關數據的場景時,不妨嘗試使用pair
數對,相信它會為你的編程工作帶來意想不到的便利。讓我們充分利用pair
數對這一強大武器,在 C++ 的編程世界中更加游刃有余地前行。