std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::clear
来自 cppreference.cn
void clear() noexcept; |
(自 C++23 起) | |
从此容器适配器擦除所有元素。在此调用后,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 << "Size=" << v.size() << '\n'; } int main() { std::flat_map<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}}; print_info("Before clear: ", container); container.clear(); print_info("After clear: ", container); }
输出
Before clear: { [1]:x [2]:y [3]:z } Size=3 After clear: { } Size=0
[编辑] 参见
擦除元素 (公共成员函数) |