std::basic_string_view<CharT,Traits>::remove_suffix
来自cppreference.com
< cpp | string | basic string view
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
[编辑] 参阅
通过向前移动起点收缩视图 (公开成员函数) |