在 C++ 中使用 STL Map 容器
- 
          
            在 C++ 中使用 std::map來宣告一個map容器物件
- 
          
            在 map 中在 C++ 中使用 find成員函式搜尋特定元素
- 
          
            在 map 中在 C++ 中使用 insert成員函式儲存元素
- 
          
            在 C++ 中使用 merge成員函式連線兩個 map 物件的元素
 
本指南將向你展示如何在 C++ 中使用 STL map 容器的幾種方法。
在 C++ 中使用 std::map 來宣告一個 map 容器物件
    
std::map 容器實現了一個排序的鍵值對資料結構,其中鍵是唯一的。鍵值預設按升序排列 pair 元素;但是,使用者可以選擇將比較函式傳遞給 std::map 模板。你可以使用值列表初始化 map 物件,其中每個元素都在單獨的大括號中指定。在這種情況下,我們建立一個 string 對的 map,然後將它的元素列印到 cout 流。請注意,每個元素都作為 for 迴圈中的 std::pair 成員訪問。
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  printMap(m1);
  return EXIT_SUCCESS;
}
輸出:
11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;
在 map 中在 C++ 中使用 find 成員函式搜尋特定元素
find 成員函式可用於在 map 容器中查詢特定的鍵值對。查詢操作具有對數複雜性。find 函式獲取對鍵的引用,並將迭代器返回到具有等效鍵的元素。當沒有找到具有給定鍵的元素時,返回一個尾後迭代器。
以下程式碼片段演示瞭如何使用範圍迭代器初始化新的 map 容器。請注意,第一個迭代器是使用 find 函式檢索的。此外,檢查返回的迭代器的有效性可能很有用。
#include <cassert>
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  std::map<string, string> m2(m1.find("11700K"), m1.end());
  printMap(m2);
  auto ret = m1.find("11700K");
  assert(ret != m1.end());
  std::map<string, string> m3(ret, m1.end());
  printMap(m3);
  return EXIT_SUCCESS;
}
輸出:
11700K : Core i7;
11900K : Core i9;
1390P : Xeon W;
在 map 中在 C++ 中使用 insert 成員函式儲存元素
你可以使用 insert 成員函式向現有的 map 容器新增新元素。insert 成員獲取對要新增的物件的引用,並返回一個容器 std::pair,該容器由插入元素的迭代器和指示成功插入狀態的 bool 值組成。當插入失敗時,迭代器指向阻止操作的元素。
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  auto ret1 = m1.insert({"1390P", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }
  ret1 = m1.insert({"1390", "Xeon W"});
  if (!ret1.second) {
    cout << "Not inserted!" << endl;
  }
  printMap(m1);
  return EXIT_SUCCESS;
}
輸出:
Not inserted!
11600K : Core i5;
11700K : Core i7;
11900K : Core i9;
1390 : Xeon W;
1390P : Xeon W;
在 C++ 中使用 merge 成員函式連線兩個 map 物件的元素
merge 成員函式可用於連線來自兩個 Map 容器的元素。它是從需要儲存組合元素的 map 物件呼叫的,並將另一個 map 作為引數。操作後,所有指向傳輸元素的指標和引用都是有效的。
#include <iostream>
#include <map>
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void printMap(Map& m) {
  for (auto& p : m) cout << p.first << " : " << p.second << ";" << endl;
  cout << endl;
}
int main() {
  std::map<string, string> m1 = {
      {"11900K", "Core i9"},
      {"11700K", "Core i7"},
      {"11600K", "Core i5"},
      {"1390P", "Xeon W"},
  };
  std::map<string, string> m4 = {
      {"11900KF", "Core i9"}, {"11600T", "Core i5"}, {"11700K", "Core i7"}};
  m1.merge(m4);
  printMap(m1);
  return EXIT_SUCCESS;
}
輸出:
11600K : Core i5;
11600T : Core i5;
11700K : Core i7;
11900K : Core i9;
11900KF : Core i9;
1390P : Xeon W;
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Jinku Hu
    
