Difference between revisions of "Template:cpp/container/clear"
From cppreference.com
Andreas Krug (Talk | contribs) m (http -> https) |
|||
Line 37: | Line 37: | ||
===Example=== | ===Example=== | ||
<!-- temp set typename to test code below --> | <!-- temp set typename to test code below --> | ||
− | {{#vardefine:typename|{{{1|}}}}} | + | {{#vardefine:typename|{{{1|unordered_multimap}}}}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
{{example | {{example | ||
|code= | |code= | ||
− | {{ | + | #include <iostream> |
− | + | #include <string_view> | |
− | + | #include <{{cpp/container/get header|{{#var:typename}}}}> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | {{#var: | + | void print_info(std::string_view rem, const std::{{#var:typename}}{{cpp/container/if map|{{#var:typename}}|<int, char>|<int>}}& v) |
− | + | ||
− | int | + | |
{ | { | ||
− | std::{{#var:typename}} | + | std::cout << rem << "{ "; |
− | + | {{cpp/container/if map|{{#var:typename}} | |
− | auto | + | |for (const auto& [key, value] : v) |
− | + | std::cout << '[' << key << "]:" << value << ' '; | |
− | + | |for (const auto& value : v) | |
+ | std::cout << value << ' '; | ||
+ | }} | ||
+ | {{#switch:{{#var:typename}} | ||
+ | |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() | int main() | ||
{ | { | ||
− | std::{{#var:typename}}<int, char> container{<!---->{1, 'x'}, {2, 'y'}, {3, 'z'}<!---->}; | + | std::{{#var:typename}}{{cpp/container/if map|{{#var:typename}} |
− | + | |<int, char> container{<!---->{1, 'x'}, {2, 'y'}, {3, 'z'}<!---->}; | |
− | + | |<int> container{1, 2, 3};}} | |
− | + | print_info("Before clear: ", container); | |
− | + | container.clear(); | |
− | + | print_info("After clear: ", container); | |
− | + | ||
− | + | ||
} | } | ||
− | + | |p={{cpp/container/if unord|{{#var:typename}}|true|false}} | |
− | + | ||
− | |p={{cpp/container/if unord|{{ | + | |
|output= | |output= | ||
− | {{#switch: {{#var:typename}} | + | {{#switch:{{#var:typename}} |
|vector= | |vector= | ||
− | Before clear: 1 2 3 | + | Before clear: { 1 2 3 } |
Size=3, Capacity=3 | Size=3, Capacity=3 | ||
− | + | After clear: { } | |
− | After clear: | + | |
Size=0, Capacity=3 | Size=0, Capacity=3 | ||
− | |||
|deque | |deque | ||
|list | |list | ||
Line 122: | Line 84: | ||
|unordered_multiset | |unordered_multiset | ||
|unordered_set= | |unordered_set= | ||
− | Before clear: 1 2 3 | + | Before clear: { 1 2 3 } |
Size=3 | Size=3 | ||
− | + | After clear: { } | |
− | After clear: | + | |
Size=0 | Size=0 | ||
− | |||
|forward_list= | |forward_list= | ||
− | Before clear: 1 2 3 | + | Before clear: { 1 2 3 } |
− | + | After clear: { } | |
− | After clear: | + | |
− | + | ||
|map | |map | ||
|multimap | |multimap | ||
|unordered_map | |unordered_map | ||
|unordered_multimap= | |unordered_multimap= | ||
− | + | Before clear: { [1]:x [2]:y [3]:z } | |
− | Before clear: [1]:x [2]:y [3]:z | + | |
Size=3 | Size=3 | ||
− | + | After clear: { } | |
− | After clear: | + | |
Size=0 | Size=0 | ||
}} | }} |
Revision as of 14:39, 10 October 2023
void clear() noexcept; |
(since {std}) | |
Erases all elements from the container. After this call, size() returns zero.
Invalidates any references, pointers, or iterators referring to contained elements.
Contents |
Parameters
(none)
Return value
(none)
Complexity
Linear in the size of the container, i.e., the number of elements.
Example
Run this code
#include <iostream> #include <string_view> #include <unordered_map> void print_info(std::string_view rem, const std::unordered_multimap<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::unordered_multimap<int, char> container{{1, 'x'}, {2, 'y'}, {3, 'z'}}; print_info("Before clear: ", container); container.clear(); print_info("After clear: ", container); }
Possible 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.
DR | Applied to | Behavior as published | Correct behavior
|
---|
See also
erases elements (public member function of std::{{{1}}} )
|