std::basic_string_view<CharT,Traits>::remove_prefix
来自 cppreference.com
< cpp | string | basic string view
constexpr void remove_prefix( size_type n ); |
(自 C++17 起) | |
将视图的开头向前移动 n 个字符。
如果 n > size(),则行为未定义。
内容 |
[编辑] 参数
n | - | 要从视图开头删除的字符数 |
[编辑] 返回值
(无)
[编辑] 复杂度
常数。
[编辑] 示例
运行此代码
#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"; }
输出
String: ' trim me' View : 'trim me' There are 3 bees in this hive.
[编辑] 另请参见
通过向后移动视图的末尾来缩小视图 (公共成员函数) |