C++ 庫 - <flat_map>



<flat_map> 標頭檔案是容器庫的一部分,提供各種功能作為排序關聯容器,用於降低記憶體使用、快速訪問,並存儲具有唯一鍵的鍵值對。

flat_map 容器充當兩個底層容器的包裝器,並透過結合有序和連續儲存的優勢提供單一方法。

包含 <flat_map> 標頭檔案

要在您的 C++ 程式中包含 <flat_map> 標頭檔案,可以使用以下語法。

#include <flat_map>

<flat_map> 標頭檔案的功能

以下是 <flat_map> 標頭檔案中所有函式的列表。

元素訪問

元素訪問函式提供諸如檢索或修改容器中元素的機制。這可以透過使用索引和鍵來完成。

序號 函式及描述
1 at

此函式訪問具有邊界檢查的元素。

2 operator[]

此函式訪問或插入指定的元素。

訪問元素

在以下示例中,我們將使用 operator[] 來訪問或插入元素。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap;
    myMap[1] = "One";
    myMap[2] = "Two"; 
        std::cout << "Element at key 1: " << myMap[1] << std::endl;
    
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

Element at key 1: One

迭代器

迭代器提供了一種遍歷或操作容器元素的方法。在 flat_map 中,迭代器用於從一個鍵值對移動到另一個鍵值對。

序號 函式及描述
1 begin, cbegin

這些函式返回指向容器開頭的迭代器。

2 end, cend

這些函式返回指向容器末尾的迭代器。

3 rbegin, crbegin

這些函式返回指向開頭(最後一個元素)的反向迭代器。

4 rend, crend

這些函式返回指向末尾(第一個元素之前)的反向迭代器。

檢索迭代器

在以下示例中,我們將使用 begin() 獲取容器開頭的迭代器。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
    
    auto it = myMap.begin(); 
    std::cout << "First element: " << it->t << " -> " << it->second << std::endl;
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

First element: 1 -> One

容量

容量函式用於檢查和提供有關容器大小和容量的資訊。

序號 函式及描述
1 empty

此函式檢查容器介面卡是否為空。

2 size

此函式返回元素的數量。

3 max_size

此函式返回元素的最大可能數量。

檢查容器是否存在

在以下示例中,我們將使用 empty() 檢查容器是否為空。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap;
    
    if (myMap.empty()) {
        std::cout << "The flat_map is empty." << std::endl;
    } else {
        std::cout << "The flat_map is not empty." << std::endl;
    }
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

The flat_map is empty.

修改器

修改器函式透過插入、替換或刪除元素來更改容器的內容。

序號 函式及描述
1 emplace

此函式在適當位置構造元素。

2 emplace_hint

此函式使用插入位置的提示在適當位置構造元素。

3 try_emplace

如果鍵不存在,此函式在適當位置插入,如果鍵存在,則不執行任何操作。

4 insert_or_assign

此函式插入元素或如果鍵已存在則分配給當前元素。

5 extract

此函式提取底層容器。

6 replace

此函式替換底層容器。

7 erase

此函式擦除元素。

8 erase_if

此函式擦除滿足特定條件的所有元素。

9 operator=

此函式將值分配給容器介面卡。

在適當位置插入元素

在以下示例中,我們將使用 emplace() 在特定位置插入元素。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap;
    
    myMap.emplace(1, "One"); 
    std::cout << "Element emplaced: " << myMap[1] << std::endl;
    
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

Element emplaced: One

查詢

序號 函式及描述
1 find

此函式查詢具有特定鍵的元素。

2 count

此函式返回與特定鍵匹配的元素的數量。

3 contains

此函式檢查容器是否包含具有特定鍵的元素。

4 lower_bound

此函式返回指向不小於給定鍵的第一個元素的迭代器。

5 upper_bound

此函式返回指向大於給定鍵的第一個元素的迭代器。

6 equal_range

此函式返回與特定鍵匹配的元素範圍。

使用鍵查詢元素

在以下示例中,我們將使用 find() 透過鍵查詢元素。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
    auto it = myMap.find(1); 
    if (it != myMap.end()) {
        std::cout << "Found element: " << it->first << " -> " << it->second << std::endl;
    } else {
        std::cout << "Element not found." << std::endl;
    }
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

Found element: 1 -> One

觀察者

觀察者函式提供有關容器如何執行的資訊,特別是關於鍵比較和訪問底層資料結構的資訊。

序號 函式及描述
1 key_comp

此函式用於比較鍵。

2 value_comp

返回比較 value_type 型別物件中的鍵的函式。

3 keys

此函式提供對底層值的容器的直接訪問。

4 values

此函式提供對底層值的容器的直接訪問。

比較鍵

在以下示例中,我們將使用 key_comp() 比較鍵。

#include <iostream>
#include <flat_map>
int main() {
    std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
    auto comp = myMap.key_comp();
    if (comp(1, 2)) {
        std::cout << "Key 1 is less than Key 2." << std::endl;
    } else {
        std::cout << "Key 1 is not less than Key 2." << std::endl;
    }
    return 0;
}

輸出

如果我們執行上述程式碼,它將生成以下輸出:

Key 1 is less than Key 2.
廣告