C++ STL 中的對映運算子 =
在這篇文章中,我們將討論 C++ STL 中對映等號“=”運算子的工作原理、語法和示例。
什麼是 C++ STL 中的對映?
對映是關聯容器,有助於按特定順序儲存由鍵值和對映值組合而成的元素。在對映容器中,資料始終藉助其關聯鍵進行內部排序。對映容器中的值透過其唯一鍵進行訪問。
什麼是對映等號“=”運算子?
map:operator= 是一個等號運算子。該運算子用於透過覆蓋容器的當前內容,將元素從一個容器複製到另一個容器。
語法
Map_name.max_size();
引數
有一個對映在運算子的左側,另一個對映在容器的右側。右側的內容被複制到左側的對映中。
返回值
運算子沒有返回值
示例
輸入
map<char, int> newmap, themap; newmap.insert({1, 20}); newmap.insert({2, 30}); themap = newmap
輸出
themap = 1:20
示例
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP, temp; TP.insert({ 2, 20 }); TP.insert({ 1, 10 }); TP.insert({ 3, 30 }); TP.insert({ 4, 40 }); TP.insert({ 6, 50 }); temp = TP; cout<<"\nData in map TP is: \n"; cout << "KEY\tELEMENT\n"; for (auto i = TP.begin(); i!= TP.end(); ++i) { cout << i->first << '\t' << i->second << '\n'; } cout << "\nData in copied map temp is : \n"; cout << "KEY\tELEMENT\n"; for (auto i = TP.begin(); i!= TP.end(); ++i) { cout << i->first << '\t' << i->second << '\n'; } return 0; }
輸出
Data in map TP is: MAP_KEY MAP_ELEMENT 1 10 2 20 3 30 4 40 6 50 Data in copied map temp is : MAP_KEY MAP_ELEMENT 1 10 2 20 3 30 4 40 6 50
廣告