std::vector::clear(3)
π NAME π
std::vector::clear β literally std::vector::clear (yes bestie, the name is the name π
). This is the std::vector::clear(3) page from the C++ Standard Libary π.
β¨ Synopsis β¨
void clear(); (noexcept since C++11)
(constexpr since C++20)
Okay so clear() erases all elements from the container. After this call, size() returns zero. Slay.
It invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterators are also invalidated. So those handles are literally done after this π.
It leaves the capacity() of the vector unchanged. (Note: the standard's restriction on the changes to capacity() is in the specification of reserve(), see SO.)
πΈ Parameters πΈ
(none) β zero arguments, zero stress, bestie π.
π Return value π
(none) β it hands you nothing back, and that is correct π«Ά.
π¦ Complexity π¦
Linear in the size of the container, i.e., the number of elements. It's giving linear-in-the-number-of-elements energy, no cap β¨.
π Example π
// Run this code
#include <iostream>
#include <string_view>
#include <vector>
void print_info(std::string_view rem, const std::vector<int>& v)
{
std::cout << rem << "{ ";
for (const auto& value : v)
std::cout << value << ' ';
std::cout << "}\n";
std::cout << "Size=" << v.size() << ", Capacity=" << v.capacity()
<< '\n';
}
int main()
{
std::vector<int> container{1, 2, 3};
print_info("Before clear: ", container);
container.clear();
print_info("After clear: ", container);
}
π¨οΈ Output: π¨οΈ
Before clear: { 1 2 3 }
Size=3, Capacity=3
After clear: { }
Size=0, Capacity=3
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 2231- Applied to: C++11
- Behavior as published: complexity guarantee was mistakenly omitted in C++11
- Correct behavior: complexity reaffirmed as linear
π See also π
eraseerases elements (public member function) β¨
Mwah π this page came from http://cppreference.com, dated 2024.06.10, and it's the std::vector::clear(3) page. Bye bestie, go clear some vectors responsibly π«Άβ¨
π¦ 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