std::basic_string::clear(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 🌈

  • erase removes 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