如何在 C++ 中對 Map 進行迭代

Jinku Hu 2023年10月12日
  1. 使用 while 迴圈在 std::map 元素上迭代
  2. 使用傳統的 for 迴圈在 std::map 元素上迭代
  3. 使用基於範圍的 for 迴圈迭代 std::map 元素
  4. 使用基於範圍的 for 迴圈迭代 std::map 鍵-值對
如何在 C++ 中對 Map 進行迭代

本文將解釋如何在 C++ 中使用多種方法對 map 進行迭代。

使用 while 迴圈在 std::map 元素上迭代

首先,我們定義一個臨時對映結構 tempMap,並在其中填充任意的鍵/值對,我們將在 stdout 輸出,以更好地展示建議的解決方案。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  map<int, string> tempMap = {{
                                  1,
                                  "Apple",
                              },
                              {
                                  2,
                                  "Banana",
                              },
                              {
                                  3,
                                  "Mango",
                              },
                              {
                                  4,
                                  "Raspberry",
                              },
                              {
                                  5,
                                  "Blackberry",
                              },
                              {
                                  6,
                                  "Cocoa",
                              }};

  auto iter = tempMap.begin();
  while (iter != tempMap.end()) {
    cout << "[" << iter->first << "," << iter->second << "]\n";
    ++iter;
  }
  cout << endl;
  return 0;
}

輸出:

[1,Apple]
[2,Banana]
[3,Mango]
[4,Raspberry]
[5,Blackberry]
[6,Cocoa]

請注意,我們使用 auto 型別指定符來宣告 std::map 迭代器,因為為了可讀性,推薦使用這種方法。它是 map<int, string>::iterator,可以顯式指定。

使用傳統的 for 迴圈在 std::map 元素上迭代

現在,讓我們用傳統的 for 迭代來實現同樣的迴圈,這可以說是在可讀性上最差的。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  map<int, string> tempMap = {{
                                  1,
                                  "Apple",
                              },
                              {
                                  2,
                                  "Banana",
                              },
                              {
                                  3,
                                  "Mango",
                              },
                              {
                                  4,
                                  "Raspberry",
                              },
                              {
                                  5,
                                  "Blackberry",
                              },
                              {
                                  6,
                                  "Cocoa",
                              }};

  for (auto iter = tempMap.begin(); iter != tempMap.end(); ++iter) {
    cout << "[" << iter->first << "," << iter->second << "]\n";
  }
  cout << endl;
  return 0;
}

使用基於範圍的 for 迴圈迭代 std::map 元素

一段時間以來,基於範圍的迴圈一直是 C++ 程式設計師的普遍選擇。如果你的編譯器支援 C++11 版本,那麼你就不用再考慮傳統的繁瑣迴圈了,可以欣賞下面這個例子的優雅。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  map<int, string> tempMap = {{
                                  1,
                                  "Apple",
                              },
                              {
                                  2,
                                  "Banana",
                              },
                              {
                                  3,
                                  "Mango",
                              },
                              {
                                  4,
                                  "Raspberry",
                              },
                              {
                                  5,
                                  "Blackberry",
                              },
                              {
                                  6,
                                  "Cocoa",
                              }};

  for (const auto &item : tempMap) {
    cout << "[" << item.first << "," << item.second << "]\n";
  }
  cout << endl;
  return 0;
}

使用基於範圍的 for 迴圈迭代 std::map 鍵-值對

這個版本從 C++17 標準開始定義,以在關聯容器中提供更靈活的迭代。與之前的例子相比,該方法的主要優勢在於方便地訪問 map 結構中的鍵值,這也保證了程式設計師更好的可讀性。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  map<int, string> tempMap = {{
                                  1,
                                  "Apple",
                              },
                              {
                                  2,
                                  "Banana",
                              },
                              {
                                  3,
                                  "Mango",
                              },
                              {
                                  4,
                                  "Raspberry",
                              },
                              {
                                  5,
                                  "Blackberry",
                              },
                              {
                                  6,
                                  "Cocoa",
                              }};

  for (const auto& [key, value] : tempMap) {
    cout << "[" << key << "," << value << "]\n";
  }
  cout << endl;
  return 0;
}
作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相關文章 - C++ Map