Difference between revisions of "Template:cpp/container/clear"
From cppreference.com
m (Shorten template names. Use {{lc}} where appropriate.) |
m (Hide DR-section on inplace_vector::clear page.) |
||
(44 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|inplace_vector}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|clear}} |
− | {{ | + | {{cpp/container/{{#var:cont}}/navbar}} |
+ | {{dcl begin}} | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |vector= | ||
+ | {{dcl|notes={{mark noexcept since c++11}}<br>{{mark constexpr since c++20}}| | ||
void clear(); | void clear(); | ||
}} | }} | ||
+ | |inplace_vector= | ||
+ | {{dcl|since=c++26| | ||
+ | constexpr void clear() noexcept; | ||
+ | }} | ||
+ | |flat_set|flat_multiset|flat_map|flat_multimap= | ||
+ | {{dcl|since=c++23| | ||
+ | void clear() noexcept; | ||
+ | }} | ||
+ | | | ||
+ | {{cpp/container/noexcept dcl|{{#var:cont}}|dcl= | ||
+ | void clear() | ||
+ | }} | ||
+ | }} | ||
+ | {{dcl end}} | ||
− | + | Erases all elements from the container{{cpp/container/if ad|{{#var:cont}}|{{nbsp}}adaptor}}. {{#ifeq:{{#var:cont}}|forward_list||After this call, {{lc|size()}} returns zero.}} | |
+ | Invalidates any references, pointers, and iterators referring to contained elements. | ||
+ | {{#switch:{{#var:cont}} | ||
+ | <!--|flat_set|flat_multiset|flat_map|flat_multimap={{todo}}--> | ||
+ | |list|forward_list|map|multimap|set|multiset= | ||
+ | Any past-the-end iterator remains valid. | ||
+ | |unordered_map|unordered_multimap|unordered_set|unordered_multiset= | ||
+ | May also invalidate past-the-end iterators. | ||
+ | |inplace_vector|deque= | ||
+ | Any past-the-end iterators are also invalidated. | ||
+ | |vector= | ||
+ | Any past-the-end iterators are also invalidated. | ||
+ | |||
+ | Leaves the {{lc|capacity()}} of the vector unchanged (Note: the standard's restriction on the changes to capacity is in the specification of {{lc|reserve()}}, see [https://stackoverflow.com/a/18467916 SO]). | ||
+ | }} | ||
+ | <!----> | ||
===Parameters=== | ===Parameters=== | ||
(none) | (none) | ||
Line 12: | Line 45: | ||
===Return value=== | ===Return value=== | ||
(none) | (none) | ||
− | |||
− | |||
− | |||
===Complexity=== | ===Complexity=== | ||
− | + | Linear in the size of the container{{cpp/container/if ad|{{#var:cont}}|{{nbsp}}adaptor}}, i.e., the number of elements. | |
− | {{cpp/container/if | + | |
− | {{ | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <iostream> | ||
+ | #include <string_view> | ||
+ | #include <{{cpp/container/get header|{{#var:cont}}}}> | ||
+ | |||
+ | void print_info(std::string_view rem, const std::{{#var:cont}}{{cpp/container/if map|{{#var:cont}}|<int, char>|<int{{#ifeq:{{#var:cont}}|inplace_vector|, 3}}>}}& v) | ||
+ | { | ||
+ | std::cout << rem << "{ "; | ||
+ | {{cpp/container/if map|{{#var:cont}} | ||
+ | |for (const auto& [key, value] : v) | ||
+ | std::cout << '[' << key << "]:" << value << ' '; | ||
+ | |for (const auto& value : v) | ||
+ | std::cout << value << ' '; | ||
+ | }} | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |forward_list=std::cout << "}\n"; | ||
+ | |vector=std::cout << "}\n"; | ||
+ | std::cout << "Size=" << v.size() << ", Capacity=" << v.capacity() << '\n'; | ||
+ | |#default=std::cout << "}\n"; | ||
+ | std::cout << "Size=" << v.size() << '\n';}} | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::{{#var:cont}}{{cpp/container/if map|{{#var:cont}} | ||
+ | |<int, char> container{<!---->{1, 'x'}, {2, 'y'}, {3, 'z'}<!---->}; | ||
+ | |<int{{#ifeq:{{#var:cont}}|inplace_vector|, 3}}> container{1, 2, 3};}} | ||
+ | print_info("Before clear: ", container); | ||
+ | container.clear(); | ||
+ | print_info("After clear: ", container); | ||
+ | } | ||
+ | |p={{cpp/container/if unord|{{#var:cont}}|true|false}} | ||
+ | |output= | ||
+ | {{#switch:{{#var:cont}} | ||
+ | |vector= | ||
+ | Before clear: { 1 2 3 } | ||
+ | Size=3, Capacity=3 | ||
+ | After clear: { } | ||
+ | Size=0, Capacity=3 | ||
+ | |inplace_vector | ||
+ | |deque | ||
+ | |list | ||
+ | |multiset | ||
+ | |set | ||
+ | |flat_set | ||
+ | |flat_multiset | ||
+ | |unordered_multiset | ||
+ | |unordered_set= | ||
+ | Before clear: { 1 2 3 } | ||
+ | Size=3 | ||
+ | After clear: { } | ||
+ | Size=0 | ||
+ | |forward_list= | ||
+ | Before clear: { 1 2 3 } | ||
+ | After clear: { } | ||
+ | |map | ||
+ | |multimap | ||
+ | |flat_map | ||
+ | |flat_multimap | ||
+ | |unordered_map | ||
+ | |unordered_multimap= | ||
+ | Before clear: { [1]:x [2]:y [3]:z } | ||
+ | Size=3 | ||
+ | After clear: { } | ||
+ | Size=0 | ||
+ | }} | ||
+ | }} | ||
+ | <!----> | ||
+ | {{#switch:{{#var:cont}}|inplace_vector|flat_map|flat_set|flat_multimap|flat_multiset=| | ||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{cpp/container/if seq|{{#var:cont}}| | ||
+ | {{dr list item|wg=lwg|dr=2231|std=C++11|before=complexity guarantee was mistakenly omitted in C++11|after=complexity reaffirmed as linear}} | ||
+ | }}<!-- | ||
+ | -->{{cpp/container/if ord|{{#var:cont}}| | ||
+ | {{dr list item|wg=lwg|dr=224|std=C++98|before=the complexity was {{c|log(size()) + N}}, but {{c|N}} was not defined|after=corrected to 'linear in {{c|size()}}'}} | ||
+ | }}<!-- | ||
+ | -->{{cpp/container/if unord|{{#var:cont}}| | ||
+ | {{dr list item|wg=lwg|dr=2550|std=C++11|before=for unordered associative containers, unclear if complexity<br>is linear in the number of elements or buckets|after=clarified that it's linear in the number of elements}} | ||
+ | }} | ||
+ | <!----> | ||
+ | {{dr list end}} | ||
+ | }} | ||
+ | <!----> | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{#ifeq: {{ | + | {{#ifeq:{{#var:cont}}|forward_list |
− | + | |{{dsc inc|cpp/container/dsc erase_after|{{#var:cont}}}} | |
− | + | |{{dsc inc|cpp/container/dsc erase|{{#var:cont}}}} | |
}} | }} | ||
{{dsc end}} | {{dsc end}} |
Latest revision as of 09:50, 16 August 2024
constexpr void clear() noexcept; |
(since C++26) | |
Erases all elements from the container. After this call, size() returns zero.
Invalidates any references, pointers, and iterators referring to contained elements. Any past-the-end iterators are also invalidated.
Contents |
[edit] Parameters
(none)
[edit] Return value
(none)
[edit] Complexity
Linear in the size of the container, i.e., the number of elements.
[edit] Example
Run this code
#include <iostream> #include <string_view> #include <inplace_vector> void print_info(std::string_view rem, const std::inplace_vector<int, 3>& v) { std::cout << rem << "{ "; for (const auto& value : v) std::cout << value << ' '; std::cout << "}\n"; std::cout << "Size=" << v.size() << '\n'; } int main() { std::inplace_vector<int, 3> 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
[edit] See also
erases elements (public member function of std::inplace_vector<T,N> )
|