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

出自cppreference.com
 
 
 
 
constexpr const_reference back() const;
(C++17 起)

返回到視圖末字符的引用。

如果 empty()true,那麼行為未定義。

(C++26 前)

如果 empty()true,那麼:

  • 如果實現是硬化實現,那麼就會發生契約違背
  • 如果實現不是硬化實現,那麼行為未定義。
(C++26 起)

返回值

data_[size() - 1]

複雜度

常數。

示例

#include <iostream>
#include <string_view>
 
int main()
{
    for (std::string_view str{"ABCDEF"}; !str.empty(); str.remove_suffix(1))
        std::cout << str.back() << ' ' << str << '\n';
}

輸出:

F ABCDEF
E ABCDE
D ABCD
C ABC
B AB
A A

參閱

訪問首個字符
(公開成員函數) [編輯]
檢查視圖是否為空
(公開成員函數) [編輯]
(DR*)
訪問最後的字符
(std::basic_string<CharT,Traits,Allocator> 的公開成員函數) [編輯]