Namespaces
Variants

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

From cppreference.net
constexpr const_reference at ( size_type pos ) const ;
(C++17부터)

지정된 위치 pos 에 있는 문자에 대한 const 참조를 반환합니다. 범위 검사가 수행되며, 잘못된 접근 시 std::out_of_range 타입의 예외가 발생합니다.

목차

매개변수

pos - 반환할 문자의 위치

반환값

Const 요청된 문자에 대한 참조.

예외

std::out_of_range 를 발생시킵니다, 만약 pos >= size ( ) 인 경우.

복잡도

상수.

예제

#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> 의 public member function)