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

出自cppreference.com
 
 
 
 
constexpr void remove_suffix( size_type n );
(C++17 起)

將視圖末尾向後移動 n 個字符。

如果 n > size()true,那麼行為未定義。

(C++26 前)

如果 n > size()true,那麼:

  • 如果實現是硬化實現,那麼就會發生契約違背。並且契約違背處理函數在“觀察”求值語義下返回時行為未定義。
  • 如果實現不是硬化實現,那麼行為未定義。
(C++26 起)

目錄

[編輯] 參數

n - 要從視圖終點移除的字符數

[編輯] 複雜度

常數。

[編輯] 示例

#include <iostream>
#include <string_view>
 
int main()
{
    char arr[] = {'a', 'b', 'c', 'd', '\0', '\0', '\0'};
    std::string_view v(arr, sizeof arr);
    auto trim_pos = v.find('\0');
    if(trim_pos != v.npos)
        v.remove_suffix(v.size() - trim_pos);
    std::cout << "数组:'" << arr << "',size=" << sizeof arr << '\n'
              << "视图:'" << v << "',size=" << v.size() << '\n';
}

輸出:

数组:'abcd',size=7
视图:'abcd',size=4

[編輯] 參閱

通過向前移動起點收縮視圖
(公開成員函數) [編輯]