名稱空間
變體
操作

std::basic_string_view<CharT,Traits>::remove_prefix

來自 cppreference.com
 
 
 
 
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.

[編輯] 參閱

通過後移其末尾位置來收縮檢視
(public member function) [編輯]