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(),則行為未定義。
目錄 |
[編輯] 引數
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
[編輯] 參閱
透過前移其起始位置來收縮檢視 (public member function) |