
- C標準庫
- C標準庫
- C++標準庫
- C++庫 - 首頁
- C++庫 - <fstream>
- C++庫 - <iomanip>
- C++庫 - <ios>
- C++庫 - <iosfwd>
- C++庫 - <iostream>
- C++庫 - <istream>
- C++庫 - <ostream>
- C++庫 - <sstream>
- C++庫 - <streambuf>
- C++庫 - <atomic>
- C++庫 - <complex>
- C++庫 - <exception>
- C++庫 - <functional>
- C++庫 - <limits>
- C++庫 - <locale>
- C++庫 - <memory>
- C++庫 - <new>
- C++庫 - <numeric>
- C++庫 - <regex>
- C++庫 - <stdexcept>
- C++庫 - <string>
- C++庫 - <thread>
- C++庫 - <tuple>
- C++庫 - <typeinfo>
- C++庫 - <utility>
- C++庫 - <valarray>
- C++ STL庫
- C++庫 - <array>
- C++庫 - <bitset>
- C++庫 - <deque>
- C++庫 - <forward_list>
- C++庫 - <list>
- C++庫 - <map>
- C++庫 - <multimap>
- C++庫 - <queue>
- C++庫 - <priority_queue>
- C++庫 - <set>
- C++庫 - <stack>
- C++庫 - <unordered_map>
- C++庫 - <unordered_set>
- C++庫 - <vector>
- C++庫 - <algorithm>
- C++庫 - <iterator>
- C++高階庫
- C++庫 - <any>
- C++庫 - <barrier>
- C++庫 - <bit>
- C++庫 - <chrono>
- C++庫 - <cinttypes>
- C++庫 - <clocale>
- C++庫 - <condition_variable>
- C++庫 - <coroutine>
- C++庫 - <cstdlib>
- C++庫 - <cstring>
- C++庫 - <cuchar>
- C++庫 - <charconv>
- C++庫 - <cfenv>
- C++庫 - <cmath>
- C++庫 - <ccomplex>
- C++庫 - <expected>
- C++庫 - <format>
- C++庫 - <future>
- C++庫 - <flat_set>
- C++庫 - <flat_map>
- C++庫 - <filesystem>
- C++庫 - <generator>
- C++庫 - <initializer_list>
- C++庫 - <latch>
- C++庫 - <memory_resource>
- C++庫 - <mutex>
- C++庫 - <mdspan>
- C++庫 - <optional>
- C++庫 - <print>
- C++庫 - <ratio>
- C++庫 - <scoped_allocator>
- C++庫 - <semaphore>
- C++庫 - <source_location>
- C++庫 - <span>
- C++庫 - <spanstream>
- C++庫 - <stacktrace>
- C++庫 - <stop_token>
- C++庫 - <syncstream>
- C++庫 - <system_error>
- C++庫 - <string_view>
- C++庫 - <stdatomic>
- C++庫 - <variant>
- C++ STL庫速查表
- C++ STL - 速查表
C++庫 - <flat_set>
<flat_set> 標頭檔案是標準庫中的一個容器,設計為具有類似陣列結構的有序關聯容器。
這類似於 std::vector,並提供記憶體效率和快速訪問,尤其適用於小型集合。此標頭檔案是容器庫的一部分。包含 <flat_set> 標頭檔案
要在 C++ 程式中包含 <flat_set> 標頭檔案,可以使用以下語法。
#include <flat_set>
<flat_set> 標頭檔案的函式
以下是 <flat_set> 標頭檔案的所有函式列表。
迭代器
迭代器指的是允許訪問容器中元素的物件,類似於陣列中的指標。在 flat_set 中,它們用於以排序順序遍歷元素,而不會修改底層容器。
序號 | 函式和描述 |
---|---|
1 | begin & cbegin
這些函式返回指向容器中第一個元素的迭代器。 |
2 | end & cend
這些函式返回指向容器中第一個元素的迭代器。 |
3 | rbegin & crbegin
這些函式返回指向最後一個元素的反向迭代器。 |
4 | rend & crend
這些函式返回指向第一個元素之前的一個的反向迭代器。 |
檢索迭代器
在下面的示例中,我們將使用 begin() 返回指向容器第一個元素的迭代器。
#include <iostream> #include <boost/container/flat_set.hpp> int main() { boost::container::flat_set<int> fs = {10, 20, 30, 40}; auto it = fs.begin(); std::cout << "First element: " << *it << std::endl; return 0; }
輸出
如果我們執行上面的程式碼,它將生成以下輸出:
First element: 10
容量
容量函式用於查詢容器的大小和儲存限制,例如檢查容器是否為空,容器包含的元素數量。
序號 | 函式和描述 |
---|---|
1 | empty
此函式檢查容器是否為空。 |
2 | size
此函式返回容器中元素的數量。 |
3 | max_size
此函式返回容器可以容納的最大元素數量。 |
檢查容器
在下面的示例中,我們將使用 empty() 來檢查容器是否為空,如果它不包含任何元素,則返回 true。
#include <iostream> #include <boost/container/flat_set.hpp> int main() { boost::container::flat_set<int> fs; if (fs.empty()) { std::cout << "The flat_set is empty." << std::endl; } return 0; }
輸出
如果我們執行上面的程式碼,它將生成以下輸出:
The flat_set is empty.
修改器
修改器函式用於透過新增、刪除或替換元素來更改容器的內容。
序號 | 函式和描述 |
---|---|
1 | emplace
此函式在容器中就地構造並插入一個元素。 |
2 | emplace_hint
此函式使用其位置提示就地插入一個元素。 |
3 | insert
此函式將元素插入容器。 |
4 | insert_range
此函式將一系列元素插入容器。 |
5 | extract
此函式從 flat_set 中提取底層容器。 |
6 | replace
此函式用一組新的元素替換底層容器。 |
7 | erase
此函式從容器中刪除元素。 |
8 | swap
此函式交換兩個 flat_set 物件的內容。 |
9 | clear
此函式清除所有元素,使容器為空。 |
新增元素
在下面的示例程式碼中,我們將使用 insert() 將元素新增到容器中。
#include <boost/container/flat_set.hpp> #include <iostream> int main() { boost::container::flat_set<int> fs = {10, 20, 30}; fs.insert(25); for (int elem : fs) { std::cout << elem << " "; } return 0; }
輸出
如果我們執行上面的程式碼,它將生成以下輸出:
10 20 25 30
查詢
查詢函式允許根據其鍵搜尋和訪問元素。
序號 | 函式和描述 |
---|---|
1 | find
此函式查詢具有特定鍵的元素。 |
2 | count
此函式返回與特定鍵匹配的元素數量。 |
3 | contains
此函式檢查容器是否包含具有特定鍵的元素。 |
4 | lower_bound
此函式返回指向第一個不小於給定鍵的元素的迭代器。 |
5 | upper_bound
此函式返回指向第一個大於給定鍵的元素的迭代器。 |
6 | equal_range
此函式返回匹配特定鍵的元素範圍內的迭代器對。 |
查詢元素
在下面的示例程式碼中,我們將使用 find() 搜尋具有特定鍵的元素。
#include <boost/container/flat_set.hpp> #include <iostream> int main() { boost::container::flat_set<int> fs = {10, 20, 30, 40}; auto it = fs.find(30); if (it != fs.end()) { std::cout << "Element 30 found." << std::endl; } return 0; }
輸出
如果我們執行上面的程式碼,它將生成以下輸出:
Element 30 found.
觀察者
觀察者函式提供對容器內部使用的比較和排序機制的訪問。
序號 | 函式和描述 |
---|---|
1 | key_comp
此函式用於比較鍵。 |
2 | value_comp
返回比較 value_type 型別物件中的鍵的函式。 |
比較鍵
在下面的示例中,我們將使用 key_comp(),因為它返回比較鍵的物件。
#include <boost/container/flat_set.hpp> #include <iostream> int main() { boost::container::flat_set<int> fs = {30, 10, 20}; auto comp = fs.key_comp(); std::cout << std::boolalpha << comp(10, 30) << std::endl; return 0; }
輸出
如果我們執行上面的程式碼,它將生成以下輸出:
true