Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/clear"

From cppreference.com
m (http -> https)
m (Example: pulled the printer out of main()
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}}}}}
 
+
{{#vardefine:headers|
+
#include <algorithm>
+
#include <iostream>
+
#include <{{Template:cpp/container/get header|{{#var:typename}}}}>
+
}}
+
{{#vardefine:common_body|
+
    std::cout << "Before clear:";
+
    std::for_each(container.begin(), container.end(), print);
+
    {{#switch: {{#var:typename}}
+
    |forward_list= std::cout << '\n';
+
    |vector= std::cout << "\nSize=" << container.size() << ", Capacity=" << container.capacity() << '\n';
+
    |#default= std::cout << "\nSize=" << container.size() << '\n'; }}
+
 
+
    std::cout << "Clear\n";
+
    container.clear();
+
+
    std::cout << "After clear:";
+
    std::for_each(container.begin(), container.end(), print);
+
    {{#switch: {{#var:typename}}
+
    |forward_list= std::cout << '\n';
+
    |vector= std::cout << "\nSize=" << container.size() << ", Capacity=" << container.capacity() << '\n';
+
    |#default= std::cout << "\nSize=" << container.size() << '\n'; }}
+
}}
+
 
+
 
{{example
 
{{example
 
|code=
 
|code=
{{#switch: {{#var:typename}}
+
#include <iostream>
|deque
+
#include <string_view>
|forward_list
+
#include <{{cpp/container/get header|{{#var:typename}}}}>
|list
+
|multiset
+
|set
+
|unordered_multiset
+
|unordered_set
+
|vector=
+
  
{{#var:headers}}
+
void print_info(std::string_view rem, const std::{{#var:typename}}{{cpp/container/if map|{{#var:typename}}|<int, char>|<int>}}& v)
+
int main()
+
 
{
 
{
     std::{{#var:typename}}<int> container{1, 2, 3};
+
     std::cout << rem << "{ ";
 
+
    {{cpp/container/if map|{{#var:typename}}
     auto print = [](const int& n) { std::cout << ' ' << n; };
+
     |for (const auto& [key, value] : v)
 
+
        std::cout << '[' << key << "]:" << value << ' ';
     {{#var:common_body}}
+
    |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';}}
 
}
 
}
  
|map
 
|multimap
 
|unordered_map
 
|unordered_multimap=
 
   
 
{{#var:headers}}
 
 
 
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'}<!---->};
     auto print = [](std::pair<const int, char>& n)
+
     |<int> container{1, 2, 3};}}
    {  
+
    print_info("Before clear: ", container);
        std::cout << " [" << n.first << "]:" << n.second << ']';  
+
     container.clear();
     };
+
     print_info("After clear: ", container);
 
+
     {{#var:common_body}}
+
 
}
 
}
 
+
|p={{cpp/container/if unord|{{#var:typename}}|true|false}}
}}
+
|p={{cpp/container/if unord|{{{1|}}}|true|}}
+
 
|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
Clear
+
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
Clear
+
After clear: { }
After clear:
+
 
Size=0
 
Size=0
 
 
|forward_list=
 
|forward_list=
Before clear: 1 2 3
+
Before clear: { 1 2 3 }
Clear
+
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
Clear
+
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

#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}}}) [edit]