名稱空間
變體
操作

std::set

來自 cppreference.com
< cpp‎ | 容器
 
 
 
 
在標頭檔案 <set> 中定義
template<

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class set;
(1)
namespace pmr {

    template<
        class Key,
        class Compare = std::less<Key>
    > using set = std::set<Key, Compare, std::pmr::polymorphic_allocator<Key>>;

}
(2) (C++17 起)

std::set 是一個關聯容器,它包含一個已排序的唯一 Key 型別物件的集合。排序使用鍵比較函式 Compare 完成。搜尋、刪除和插入操作具有對數複雜度。Set 通常實現為 紅黑樹

在標準庫中使用 Compare 要求的地方,唯一性是透過使用等價關係來確定的。不精確地說,如果兩個物件 ab 都不比對方小,則認為它們是等價的:!comp(a, b) && !comp(b, a)

std::set 滿足 ContainerAllocatorAwareContainerAssociativeContainerReversibleContainer 的要求。

std::set 的所有成員函式都是 constexpr:可以在常量表達式的評估中建立和使用 std::set 物件。

然而,std::set 物件通常不能是 constexpr,因為任何動態分配的儲存都必須在同一常量表達式的評估中釋放。

(C++26 起)

目錄

[編輯] 模板引數

[編輯] 成員型別

型別 定義
key_type Key[編輯]
value_type Key[編輯]
size_type 無符號整數型別(通常為 std::size_t[編輯]
difference_type 有符號整數型別(通常為 std::ptrdiff_t[編輯]
key_compare Compare[編輯]
value_compare Compare[編輯]
allocator_type Allocator[編輯]
reference value_type&[編輯]
const_reference const value_type&[編輯]
pointer

Allocator::pointer

(C++11 前)

std::allocator_traits<Allocator>::pointer

(C++11 起)
[編輯]
const_pointer

Allocator::const_pointer

(C++11 前)

std::allocator_traits<Allocator>::const_pointer

(C++11 起)
[編輯]
iterator 指向 value_type 的常量 LegacyBidirectionalIterator ConstexprIterator(C++26 起)[編輯]
const_iterator 指向 const value_typeLegacyBidirectionalIteratorConstexprIterator(C++26 起)[編輯]
reverse_iterator std::reverse_iterator<iterator>[編輯]
const_reverse_iterator std::reverse_iterator<const_iterator>[編輯]
node_type (C++17 起) 節點控制代碼 的特化,表示一個容器節點[編輯]
insert_return_type (C++17起) 描述插入 node_type 結果的型別,一個特化

template<class Iter, class NodeType>
struct /*未指定*/
{
    Iter     position;
    bool     inserted;
    NodeType node;
};

使用模板引數 iteratornode_type 例項化。[編輯]

[編輯] 成員函式

構造 set
(public 成員函式) [編輯]
析構 set
(public 成員函式) [編輯]
將值賦給容器
(public 成員函式) [編輯]
返回關聯的分配器
(public 成員函式) [編輯]
迭代器
返回指向起始的迭代器
(public 成員函式) [編輯]
(C++11)
返回指向末尾的迭代器
(public 成員函式) [編輯]
返回指向起始的逆向迭代器
(public 成員函式) [編輯]
(C++11)
返回指向末尾的逆向迭代器
(public 成員函式) [編輯]
容量
檢查容器是否為空
(public 成員函式) [編輯]
返回元素數量
(public 成員函式) [編輯]
返回元素的最大可能數量
(public 成員函式) [編輯]
修改器
清除內容
(public 成員函式) [編輯]
插入元素 或節點(C++17 起)
(public 成員函式) [編輯]
插入元素範圍
(public 成員函式) [編輯]
(C++11)
就地構造元素
(public 成員函式) [編輯]
使用提示就地構造元素
(public 成員函式) [編輯]
擦除元素
(public 成員函式) [編輯]
交換內容
(public 成員函式) [編輯]
(C++17)
從容器中提取節點
(public 成員函式) [編輯]
(C++17)
從另一個容器拼接節點
(public 成員函式) [編輯]
查詢
返回匹配特定鍵的元素數量
(public 成員函式) [編輯]
查詢具有特定鍵的元素
(public 成員函式) [編輯]
(C++20)
檢查容器是否包含具有特定鍵的元素
(public 成員函式) [編輯]
返回與特定鍵匹配的元素範圍
(public 成員函式) [編輯]
返回指向第一個不小於給定鍵的元素的迭代器
(public 成員函式) [編輯]
返回指向第一個大於給定鍵的元素的迭代器
(public 成員函式) [編輯]
觀察器
返回比較鍵的函式
(public 成員函式) [編輯]
返回比較 value_type 型別物件中的鍵的函式
(public 成員函式) [編輯]

[編輯] 非成員函式

(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20)
按字典順序比較兩個 set 的值
(函式模板) [編輯]
特化 std::swap 演算法
(函式模板) [編輯]
擦除所有滿足特定標準的元素
(函式模板) [編輯]

推導指引

(C++17 起)

[編輯] 注意

成員型別 iteratorconst_iterator 可以是相同型別的別名。這意味著定義一對使用這兩種型別作為引數型別的函式過載可能會違反一條定義規則。由於 iterator 可以轉換為 const_iterator,因此使用 const_iterator 作為引數型別的單個函式將起作用。

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 容器的範圍構造和插入
__cpp_lib_constexpr_containers 202502L (C++26) constexpr std::set

[編輯] 示例

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <set>
#include <string_view>
 
template<typename T>
std::ostream& operator<<(std::ostream& out, const std::set<T>& set)
{
    if (set.empty())
        return out << "{}";
    out << "{ " << *set.begin();
    std::for_each(std::next(set.begin()), set.end(), [&out](const T& element)
    {
        out << ", " << element;
    });
    return out << " }";
}
 
int main()
{
    std::set<int> set{1, 5, 3};
    std::cout << set << '\n';
 
    set.insert(2);
    std::cout << set << '\n';
 
    set.erase(1);
    std::cout << set << "\n\n";
 
    std::set<int> keys{3, 4};
    for (int key : keys)
    {
        if (set.contains(key))
            std::cout << set << " does contain " << key << '\n';
        else
            std::cout << set << " doesn't contain " << key << '\n';
    }
    std::cout << '\n';
 
    std::string_view word = "element";
    std::set<char> characters(word.begin(), word.end());
    std::cout << "There are " << characters.size() << " unique characters in "
              << std::quoted(word) << ":\n" << characters << '\n';
}

輸出

{ 1, 3, 5 }
{ 1, 2, 3, 5 }
{ 2, 3, 5 }
 
{ 2, 3, 5 } does contain 3
{ 2, 3, 5 } doesn't contain 4
 
There are 5 unique characters in "element":
{ e, l, m, n, t }

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 103 C++98 迭代器允許修改鍵 迭代器設為常量
LWG 230 C++98 不要求 Key可複製構造的 (CopyConstructible)
(型別為 Key 的鍵可能無法構造)
Key 也要求
可複製構造 (CopyConstructible)

[編輯] 參見

鍵的集合,按鍵排序
(類模板) [編輯]
由鍵雜湊的唯一鍵集合
(類模板) [編輯]
(C++23)
適配容器以提供唯一鍵的集合,按鍵排序
(類模板) [編輯]