• 跳至主要導覽
  • 跳至主要內容
  • 跳至主要資訊欄
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++

讀者互動方式

發佈留言 取消回覆

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

主要資訊欄

搜尋

近期文章

  • C++ 語言使用 Crypto++ 實作 RSA 數位簽章教學與範例
  • C++ 語言使用 Crypto++ 實作 RSA-OAEP 搭配 SHA256 加密教學與範例
  • C++ 語言使用 Crypto++ 實作 AES 加密、解密、認證加密教學與範例
  • C++ 語言使用 Crypto++ 實作 MD5、SHA1、SHA2、BLAKE2 雜湊教學與範例
  • Ubuntu Linux 安裝、使用 Crypto++ 加密函式庫教學與範例
  • C 語言使用 OpenSSL 實作橢圓曲線 ECDH 金鑰交換教學與範例
  • Python 以 eciespy 實作 ECC 非對稱式加密方法教學與範例
  • C 語言使用 OpenSSL 實作 PBKDF2 教學與範例

推薦網站

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

關注本站

  • 電子郵件
  • Facebook

公益

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

Copyright © 2021 · Office Guide