std::basic_...emove_prefix(3)

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

πŸ’… std::basic_...emove_prefix(3) πŸ’…

okay so this is the std::basic_string_view::remove_prefix page from the C++ Standard Libary manual β€” yes, that's the manual name up top, and we're folding it in right here instead of copying the header line, periodt. 🫢


πŸ’– NAME πŸ’–

std::basic_string_view::remove_prefix β€” and the original literally says it twice, so we're keeping that main-character energy: std::basic_string_view::remove_prefix. ✨

πŸ“œ Synopsis πŸ“œ

constexpr void remove_prefix( size_type n );  (since C++17)

Moves the start of the view forward by n characters. πŸ¦‹

⚠️ Undefined behavior alert: the behavior is undefined if n > size(). Not a cute surprise, bestie β€” do not let n be bigger than size(). πŸ’€

πŸŽ€ Parameters πŸŽ€

  • n β€” number of characters to remove from the start of the view. πŸ“

🌸 Return value 🌸

(none) β€” literally nothing gets returned, and that's the whole vibe. πŸ’œ

⭐ Complexity ⭐

Constant. Yup, that's it. Iconic behavior, no cap. πŸ”₯

🐝 Example 🐝

       // Run this code

        #include <algorithm>
        #include <iostream>
        #include <string_view>

        using namespace std::literals;

        [[nodiscard("a pure function")]]
        constexpr std::size_t count_substrings(std::string_view hive,
       std::string_view const bee)
        {
            if (hive.empty() || bee.empty())
                return 0U;

            std::size_t buzz{};
            while (bee.size() <= hive.size())
            {
                const auto pos = hive.find(bee);
                if (pos == hive.npos)
                    break;
                ++buzz;
                hive.remove_prefix(pos + bee.size());
            }
            return buzz;
        }

        int main()
        {
            std::string str = "   trim me";
            std::string_view v = str;
            v.remove_prefix(std::min(v.find_first_not_of(" "), v.size()));
            std::cout << "String: '" << str << "'\n"
                      << "View  : '" << v << "'\n";

            constexpr auto hive{"bee buzz bee buzz bee"};
            std::cout << "There are " << count_substrings(hive, "bee") << "
       bees in this hive.\n";
        }

🐝 Output 🐝

String: '   trim me'
View  : 'trim me'
There are 3 bees in this hive.

🌈 See also 🌈

  • remove_suffix β€” shrinks the view by moving its end backward (public member function). πŸ’…

mwah πŸŽ€ that's the whole page, bestie. Source/version: http://cppreference.com, dated 2024.06.10, for the std::basic_...emove_prefix(3) page. Go slay your string views πŸ’–βœ¨

πŸ“¦ where this page comes from