std::deque::clear(3)
π NAME π
std::deque::clear β std::deque::clear
okay so bestie, welcome to std::deque::clear(3) from the C++ Standard Libary manual. Yes, the NAME line is literally the same thing twice β itβs giving minimal, itβs giving icon behavior π
.
β¨ Synopsis β¨
void clear(); (noexcept since C++11)
This is the whole signature, no cap. Itβs marked noexcept since C++11 exactly like that β¨.
What it does: Erases all elements from the container. After this call, size() returns zero. Periodt.
β οΈ Invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterators are also invalidated. So yeah, donβt be holding onto old iterators like theyβre still valid β they are not, bestie π.
π«Ά Parameters π«Ά
(none)β no arguments, no stress, no little parameter besties to feed.
π Return value π
(none)β it returns literally nothing. It just does the clear and dips.
πΈ Complexity πΈ
Linear in the size of the container, i.e., the number of elements. So the bigger the deque, the more work itβs doing β fr fr.
π¦ Example π¦
The page gives the cue // Run this code, and then this exact code drops:
#include <iostream>
#include <string_view>
#include <deque>
void print_info(std::string_view rem, const std::deque<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::deque<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
After clear: { }
Size=0
ate. no crumbs. the deque really said βempty eraβ π«Άβ¨
π 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 π
eraseβ erases elements (public member function)
source/date sign-off: http://cppreference.com Β· 2024.06.10 πβ¨
π¦ 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