std::map::clear(3)
hii bestie π you just opened the std::map::clear page from the C++ Standard Libary (yes, βLibaryβ β thatβs literally how the header spells it, and we respect the typo π). itβs manual-page time, but make it cute.
β¨ NAME β¨
std::map::clear β std::map::clear. thatβs the name. thatβs it. iconic minimalism π
π¦ Synopsis π¦
void clear(); (noexcept since C++11)
okay so what does it actually do? it erases all elements from the container π§Ήβ¨. after this call, size() returns zero β like, completely empty, not a single element left. bye bestie π
and heads up: it invalidates any references, pointers, and iterators referring to contained elements π. but the good news? any past-the-end iterator remains valid π«Ά.
πΈ Parameters πΈ
(none) β nothing to pass in. zero. nada. itβs a no-argument situation π«Ά.
π Return value π
(none) β it doesnβt hand anything back, it just clears and dips β¨.
β Complexity β
Linear in the size of the container, i.e., the number of elements. so the more stuff youβre clearing, the more work it does. periodt.
π₯ Example π₯
the original page says // Run this code β so go off, run it bestie π
#include <iostream>
#include <string_view>
#include <map>
void print_info(std::string_view rem, const std::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::map<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}};
print_info("Before clear: ", container);
container.clear();
print_info("After clear: ", container);
}
π Output π
Before clear: { [1]:x [2]:y [3]:z }
Size=3
After clear: { }
Size=0
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (retroactively!! the committee really said βweβll fix it laterβ and honestly same π)
- DR
LWG 224 - Applied to
C++98 - Behavior as published the complexity was
log(size()) + N, butNwas not defined - Correct behavior corrected to 'linear in size()'
π See also π
eraseβ erases elements (public member function).
mwah π this masterpiece lives at http://cppreference.com and is dated 2024.06.10. now go clear that map, bestie β youβre literally gonna slay π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms