std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::clear

来自cppreference.com
< cpp‎ | container‎ | flat map
 
 
 
 
void clear() noexcept;
(C++23 起)
(C++26 起为 constexpr)

从容器适配器擦除所有元素。此调用后 size() 返回零。

使任何指代所含元素的引用、指针和迭代器失效。

[编辑] 复杂度

与容器适配器的大小,即元素数成线性。

[编辑] 示例

#include <iostream>
#include <string_view>
#include <flat_map>
 
void print_info(std::string_view rem, const std::flat_map<int, char>& v)
{
    std::cout << rem << "{ ";
    for (const auto& [key, value] : v)
        std::cout << '[' << key << "]:" << value << ' ';
    std::cout << "}\n";
    std::cout << "大小=" << v.size() << '\n';
}
 
int main()
{
    std::flat_map<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}};
    print_info("clear 前:", container);
    container.clear();
    print_info("clear 后:", container);
}

输出:

clear 前:{ [1]:x [2]:y [3]:z }
大小=3
clear 后:{ }
大小=0

[编辑] 参阅

擦除元素
(公开成员函数) [编辑]