名稱空間
變體
操作

std::set<Key,Compare,Allocator>::clear

來自 cppreference.com
< cpp‎ | 容器‎ | set
 
 
 
 
void clear();
(C++11 起無異常丟擲)

從容器中擦除所有元素。在此呼叫之後,size() 返回零。

使所有引用、指標和指向所含元素的迭代器失效。任何尾後迭代器仍然有效。

目錄

[edit] 複雜度

與容器大小(即元素數量)呈線性關係。

[edit] 示例

#include <iostream>
#include <string_view>
#include <set>
 
void print_info(std::string_view rem, const std::set<int>& v)
{
    std::cout << rem << "{ ";
    for (const auto& value : v)
        std::cout << value << ' ';
    std::cout << "}\n";
    std::cout << "Size=" << v.size() << '\n';
}
 
int main()
{
    std::set<int> container{1, 2, 3};
    print_info("Before clear: ", container);
    container.clear();
    print_info("After clear: ", container);
}

輸出

Before clear: { 1 2 3 }
Size=3
After clear: { }
Size=0

缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 224 C++98 複雜度為 log(size()) + N,但 N 未定義 更正為“與 size() 成線性關係”

[edit] 參閱

擦除元素
(public member function) [編輯]