跳到主要内容

std::string_view remove_suffix() 方法

// 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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::string_view remove_suffix() 方法

// 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
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。