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

出自cppreference.com
< cpp‎ | string‎ | basic string view
在2017年12月26日 (二) 22:10由Fruderica對話 | 貢獻所做的修訂版本

 
 
 
 
constexpr void remove_suffix(size_type n);
(C++17 起)

向後移動視圖終點 n 個字符。

n > size() 則行為未定義。

目錄

參數

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 << "Array: '" << arr << "', size=" << sizeof arr << '\n'
              << "View : '" << v << "', size=" << v.size() << '\n';
}

輸出:

Array: 'abcd', size=7
View : 'abcd', size=4

參閱

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