• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
Office 指南

Office 指南

辦公室工作實用教學

  • Excel
  • Word
  • PowerPoint
  • Windows
  • PowerShell
  • R

C++ 程式語言 std::map 儲存鍵值對應用法教學與範例

介紹如何在 C++ 程式中使用標準程式庫的 map 處理鍵值對應(key-value)的資料。

std::map 簡介

C++ 標準函式庫中的 std::map 可以用來儲存一對一鍵值對應的資料,例如姓名對應年紀的資料。

std::map 中的鍵(key)是不可以重複的,例如若要儲存姓名對應年紀的資料,以姓名作為鍵的話,就不可以出現兩個人有相同姓名的狀況。

std::map 內部的資料在存放時是有經過排序的,所以不管是新增或查詢資料的速度都很快(O(log n))。

std::map 基本範例

以下是一個最簡單的 std::map 基本使用範例。

#include <iostream>
#include <map>
using namespace std;
int main() {

  // 宣告一個以 string 對應 unsigned int 的 map 變數
  map<string, unsigned int> ageTable;

  // 新增姓名與年紀的對應
  ageTable["Jake"] = 10;
  ageTable["Thomas"] = 12;
  ageTable["Michael"] = 16;

  // 以姓名查詢對應的年紀
  cout << "Thomas => " << ageTable["Thomas"] << endl;

  // 以迴圈逐一處理 map 中的每個元素
  map<string, unsigned int>::iterator it;
  for (it = ageTable.begin(); it != ageTable.end(); ++it){
     cout << it->first << ": " << it->second << endl;
  }

  return 0;
}
Thomas => 12
Jake: 10
Michael: 16
Thomas: 12

加入鍵值對應

若要在 std::map 中加入新的鍵值對應,除了使用 [] 運算子之外,也可以使用 insert 函數:

#include <iostream>
#include <map>
using namespace std;
int main() {

  map<string, unsigned int> ageTable;

  // 第一種新增鍵值方式
  ageTable["Jake"] = 10;
  ageTable["Thomas"] = 12;

  // 第二種新增鍵值方式
  ageTable.insert(pair<string, unsigned int>("Michael", 16));

  map<string, unsigned int>::iterator it;
  for (it = ageTable.begin(); it != ageTable.end(); ++it){
     cout << it->first << ": " << it->second << endl;
  }

  return 0;
}
Jake: 10
Michael: 16
Thomas: 12

查詢鍵值對應

若要根據鍵(key)來查詢 std::map 中對應的值,除了以 [] 運算子之外,也可以使用 find 函數:

#include <iostream>
#include <map>
using namespace std;
int main() {

  map<string, unsigned int> ageTable;
  ageTable["Jake"] = 10;
  ageTable["Thomas"] = 12;
  ageTable["Michael"] = 16;

  // 第一種查詢鍵值方式
  cout << "Thomas => " << ageTable["Thomas"] << endl;
  cout << "Non-exist => " << ageTable["Non-exist"] << endl; // 不存在的鍵

  // 第二種查詢鍵值方式
  map<string, unsigned int>::iterator iter = ageTable.find("Thomas");
  if (iter != ageTable.end()) {
    cout << "Thomas => " << iter->second << endl;
  } else {
    cout << "Not found." << endl;
  }

  return 0;
}
Thomas => 12
Non-exist => 0
Thomas => 12

使用 find 函數的話,可以明確區隔出查不到鍵值對應的狀況,所以是比較推薦的方法。

刪除鍵值對應

若要刪除 std::map 中的鍵值對應關係,可以使用 erase 函數:

#include <iostream>
#include <map>
using namespace std;
int main() {

  map<string, unsigned int> ageTable;
  ageTable["Jake"] = 10;
  ageTable["Thomas"] = 12;
  ageTable["Michael"] = 16;

  // 以迭代器刪除
  map<string, unsigned int>::iterator iter = ageTable.find("Thomas");
  ageTable.erase(iter);

  // 以鍵刪除,成功刪除傳回 1,否則傳回 0
  int n = ageTable.erase("Thomas");

  // 以迭代器把整個 map 清空,等同於 ageTable.clear()
  ageTable.erase(ageTable.begin(), ageTable.end());

  return 0;
}

std::map 元素個數

若要查詢 std::map 內部的元素個數,可以使用 size() 函數,而若要清空整個 std::map,則可使用 clear() 函數,並以 empty() 函數來檢查:

#include <iostream>
#include <map>
using namespace std;
int main() {

  map<string, unsigned int> ageTable;
  ageTable["Jake"] = 10;
  ageTable["Thomas"] = 12;
  ageTable["Michael"] = 16;

  // 元素個數
  cout << "size = " << ageTable.size() << endl;

  // 清空 map
  ageTable.clear();

  // 檢查 map 是否是空的
  if (ageTable.empty()) {
    cout << "ageTable is empty." << endl;
  }

  return 0;
}

分類:C/C++

讀者互動方式

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

主要資訊欄

搜尋

近期文章

  • Linux 檢查、測試 CPU AES-NI 硬體加速加密指令集教學
  • CentOS Linux 7.9 自行編譯、安裝 OpenSSH 9.0p1 伺服器教學與範例
  • Python 使用 zipfile 模組壓縮、解壓縮 ZIP 檔案教學與範例
  • Python 以 LINE Notify 自動傳送即時訊息、圖片教學與範例
  • Linux 使用 Prometheus 與 Grafana 監控伺服器狀態、發送告警 Email 簡訊教學與範例
  • Linux 設定 pam_tty_audit 記錄 SSH 使用者操作指令教學與範例
  • Linux 封鎖、解鎖登入失敗次數過多的帳號 pam_faillock 教學與範例
  • Python 使用 pytube 自動下載 YouTube 影片教學與範例

推薦網站

  • Udemy 線上教學課程
  • Coursera 線上教學課程

關注本站

  • 電子郵件
  • Facebook

公益

  • 家扶基金會
  • 台灣世界展望會
  • Yahoo 奇摩公益
  • igiving 公益網
  • 兒福聯盟

Copyright © 2021 · Office Guide