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() 是 true,那么行为未定义。 |
(C++26 前) |
如果 n > size() 是 true,那么: |
(C++26 起) |
目录 |
[编辑] 参数
n | - | 要从视图起始移除的字符数 |
[编辑] 复杂度
常数。
[编辑] 示例
运行此代码
#include <algorithm> #include <iostream> #include <string_view> using namespace std::literals; [[nodiscard("纯函数")]] constexpr std::size_t count_substrings(std::string_view hive, const std::string_view 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 << "字符串:'" << str << "'\n" << "视图 :'" << v << "'\n"; constexpr auto hive{"bee buzz bee buzz bee"}; std::cout << "蜂巢中有 " << count_substrings(hive, "bee") << " 只蜜蜂。\n"; }
输出:
字符串:' trim me' 视图 :'trim me' 蜂巢中有 3 只蜜蜂。
[编辑] 参阅
通过向后移动末尾收缩视图 (公开成员函数) |