在 C++ 建立一個字典
    
    Jinku Hu
    2023年10月12日
    
    C++
    C++ Dictionary
    
 
本文將介紹如何在 C++ 中建立一個字典。
使用初始化列表建構函式在 C++ 中建立字典
在 C++ 標準容器庫中,有一個名為 std::map 的字典,它實現了具有唯一鍵的排序鍵值對。對 map 元素的搜尋、刪除和插入對等操作具有對數的複雜度。這個方法使用大括號用文字值來初始化 map 物件。注意,每個元素對都用逗號隔開,如下面的程式碼示例所示。
#include <iostream>
#include <map>
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void PrintMap(Map& m) {
  cout << "[ ";
  for (auto& item : m) {
    cout << item.first << ":" << item.second << " ";
  }
  cout << "]\n";
}
int main() {
  map<int, string> map1 = {{
                               1,
                               "Apple",
                           },
                           {
                               2,
                               "Banana",
                           },
                           {
                               3,
                               "Mango",
                           },
                           {
                               4,
                               "Raspberry",
                           },
                           {
                               5,
                               "Blackberry",
                           },
                           {
                               6,
                               "Cocoa",
                           }};
  cout << "map1 - ";
  PrintMap(map1);
  cout << endl;
  return EXIT_SUCCESS;
}
輸出:
map1 - [ 1:Apple 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]
使用預設建構函式在 C++ 中建立一個字典
另外,也可以用給定的引數宣告一個 map 型別的物件,然後用一個單獨的語句初始化每個鍵值對。在這個示例中,我們演示了一個帶有 int 鍵和 string 值的 map。我們總是可以把這些語句放在迴圈中,或者從使用者輸入中取值,這將更符合現實世界的應用場景。
#include <iostream>
#include <map>
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void PrintMap(Map& m) {
  cout << "[ ";
  for (auto& item : m) {
    cout << item.first << ":" << item.second << " ";
  }
  cout << "]\n";
}
int main() {
  map<int, string> map2;
  map2[1] = "Banana";
  map2[2] = "Mango";
  map2[3] = "Cocoa";
  map2[4] = "Raspberry";
  cout << "map2 - ";
  PrintMap(map2);
  cout << endl;
  return EXIT_SUCCESS;
}
輸出:
map2 - [ 1:Banana 2:Mango 3:Cocoa 4:Raspberry ]
使用 copy 建構函式在 C++ 中建立一個字典
另一種建立新的 map 物件的方法是使用 copy 建構函式,它將另一個現有的 map 變數作為引數,並將鍵值對複製到新初始化的物件中。注意,這個方法不會移動現有的 map 物件,可以在以後的程式執行過程中重複使用。
#include <iostream>
#include <map>
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void PrintMap(Map& m) {
  cout << "[ ";
  for (auto& item : m) {
    cout << item.first << ":" << item.second << " ";
  }
  cout << "]\n";
}
int main() {
  map<int, string> map1 = {{
                               1,
                               "Apple",
                           },
                           {
                               2,
                               "Banana",
                           },
                           {
                               3,
                               "Mango",
                           },
                           {
                               4,
                               "Raspberry",
                           },
                           {
                               5,
                               "Blackberry",
                           },
                           {
                               6,
                               "Cocoa",
                           }};
  map<int, string> map3(map1);
  cout << "map3 - ";
  PrintMap(map3);
  cout << endl;
  return EXIT_SUCCESS;
}
輸出:
map3 - [ 1:Apple 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]
使用基於範圍的建構函式在 C++ 中建立一個字典
基於範圍的建構函式是前述方法的另一種選擇。這個解決方案可以用來初始化一個新的 map 變數,使用一些現有 map 物件的子集鍵值對。在這個例子中,我們使用 find 方法來指定範圍內的第一個鍵值對。因此,一個新的 map2 變數包含了從鍵值 2 開始到 map1 物件最後一個元素的鍵值對。
#include <iostream>
#include <map>
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::map;
using std::string;
template <typename Map>
void PrintMap(Map& m) {
  cout << "[ ";
  for (auto& item : m) {
    cout << item.first << ":" << item.second << " ";
  }
  cout << "]\n";
}
int main() {
  map<int, string> map1 = {{
                               1,
                               "Apple",
                           },
                           {
                               2,
                               "Banana",
                           },
                           {
                               3,
                               "Mango",
                           },
                           {
                               4,
                               "Raspberry",
                           },
                           {
                               5,
                               "Blackberry",
                           },
                           {
                               6,
                               "Cocoa",
                           }};
  map<int, string> map4(map1.find(2), map1.end());
  cout << "map4 - ";
  PrintMap(map4);
  cout << endl;
  return EXIT_SUCCESS;
}
輸出:
map4 - [ 2:Banana 3:Mango 4:Raspberry 5:Blackberry 6:Cocoa ]
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
