Difference between revisions of "cpp/string/basic string/clear"
From cppreference.com
< cpp | string | basic string
m (§ 21.4.4 of N3485) |
m (→Example: `assert`=>`cout` for no-warranty case; {{ddcla}}) |
||
(19 intermediate revisions by 10 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/string/basic_string/title | clear}} | + | {{cpp/string/basic_string/title|clear}} |
{{cpp/string/basic_string/navbar}} | {{cpp/string/basic_string/navbar}} | ||
− | {{ | + | {{ddcla|noexcept=c++11|constexpr=c++20| |
void clear(); | void clear(); | ||
}} | }} | ||
− | Removes all characters from the string | + | Removes all characters from the string as if by executing {{c|erase(begin(), end())}}. |
+ | |||
+ | All pointers, references, and iterators are invalidated. | ||
===Parameters=== | ===Parameters=== | ||
Line 13: | Line 15: | ||
(none) | (none) | ||
− | === | + | ===Notes=== |
− | {{ | + | Unlike for {{lc|std::vector::clear}}, the C++ standard does not explicitly require that {{lc|capacity}} is unchanged by this function, but existing implementations do not change capacity. This means that they do not release the allocated memory (see also {{lc|shrink_to_fit}}). |
===Complexity=== | ===Complexity=== | ||
− | Linear in the size of the string. | + | Linear in the size of the string, although existing implementations operate in constant time. |
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <cassert> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::string s{"Exemplar"}; | ||
+ | std::string::size_type const capacity = s.capacity(); | ||
+ | |||
+ | s.clear(); | ||
+ | assert(s.empty()); | ||
+ | assert(s.size() == 0); | ||
+ | std::cout << std::boolalpha << (s.capacity() == capacity) << '\n'; | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | true | ||
+ | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/string/basic_string/dsc erase}} | + | {{dsc inc|cpp/string/basic_string/dsc erase}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 13:58, 18 October 2024
void clear(); |
(noexcept since C++11) (constexpr since C++20) |
|
Removes all characters from the string as if by executing erase(begin(), end()).
All pointers, references, and iterators are invalidated.
Contents |
[edit] Parameters
(none)
[edit] Return value
(none)
[edit] Notes
Unlike for std::vector::clear, the C++ standard does not explicitly require that capacity is unchanged by this function, but existing implementations do not change capacity. This means that they do not release the allocated memory (see also shrink_to_fit).
[edit] Complexity
Linear in the size of the string, although existing implementations operate in constant time.
[edit] Example
Run this code
#include <cassert> #include <iostream> #include <string> int main() { std::string s{"Exemplar"}; std::string::size_type const capacity = s.capacity(); s.clear(); assert(s.empty()); assert(s.size() == 0); std::cout << std::boolalpha << (s.capacity() == capacity) << '\n'; }
Possible output:
true
[edit] See also
removes characters (public member function) |