std::string_view remove_suffix() 方法
- 自 C++17 起
// Const version only
constexpr void remove_suffix( size_type n );
将视图的末尾向后移动 n
个字符。
未定义行为
行为未定义
如果n > size()
。参数
n
- 要从视图末尾移除的字符数
返回值
(无)
复杂度
常数 - O(1)。
示例
Main.cpp
#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