名稱空間
變體
操作

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

來自 cppreference.com
 
 
 
 
constexpr const_reference at( size_type pos ) const;
(C++17 起)

返回指定位置 pos 字元的 const 引用。執行邊界檢查,無效訪問時會丟擲 std::out_of_range 型別的異常。

目錄

[編輯] 引數

pos - 要返回的字元的位置

[編輯] 返回值

所請求字元的 const 引用。

[編輯] 異常

pos >= size() 則丟擲 std::out_of_range

[編輯] 複雜度

常數時間。

[編輯] 示例

#include <iostream>
#include <stdexcept>
#include <string_view>
 
int main()
{
    std::string_view str_view("abcdef");
 
    try
    {
        for (std::size_t i = 0; true; ++i)
            std::cout << i << ": " << str_view.at(i) << '\n';
    }
    catch (const std::out_of_range& e)
    {
        std::cout << "Whooops. Index is out of range.\n";
        std::cout << e.what() << '\n';
    }
}

可能的輸出

0: a
1: b
2: c
3: d
4: e
5: f
6: Whooops. Index is out of range.
basic_string_view::at: __pos (which is 6) >= this->size() (which is 6)

[編輯] 亦參見

訪問指定的字元
(public member function) [編輯]
帶邊界檢查訪問指定字元
(std::basic_string<CharT,Traits,Allocator> 的公開成員函式) [編輯]