std::basic_...emove_prefix(3)
π 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 ifn > size(). Not a cute surprise, bestie β do not letnbe bigger thansize(). π
π 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
- 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