std::basic_string::clear(3)
π std::basic_string::clear(3) π
okay so weβre in the C++ Standard Libary manual, and this page is all about std::basic_string::clear. Bestie, itβs giving tiny-function-big-cleanup energy. β¨
π NAME π
std::basic_string::clear - std::basic_string::clear. Thatβs the name, thatβs the assignment, periodt. π
πΈ Synopsis πΈ
void clear(); (noexcept since C++11)
(constexpr since C++20)
This function removes all characters from the string as if by executing erase(begin(), end()). The whole string? Gone. β¨
π¨ All pointers, references, and iterators are invalidated. Like, every single one β do not use them after this.
π«Ά Parameters π«Ά
(none) β literally none, bestie. No args, no stress.
π Return value π
(none) β it returns nothing; the cleanup is the reward.
π 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).
π₯ Complexity π₯
Linear in the size of the string, although existing implementations operate in constant time. The glow-up is real. β
π¦ Example π¦
// Run this code
#include <cassert>
#include <string>
int main()
{
std::string s{"Exemplar"};
std::string::size_type const capacity = s.capacity();
s.clear();
assert(s.capacity() == capacity); // <- not guaranteed
assert(s.empty());
assert(s.size() == 0);
}
π See also π
eraseremoves characters (public member function) π₯
π source/version/date sign-off: http://cppreference.com, 2024.06.10, std::basic_string::clear(3) β byeee bestie! π«Ά
π¦ 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