Namespaces
Variants

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

From cppreference.net
constexpr basic_string_view substr ( size_type pos = 0 ,
size_type count = npos ) const ;
(C++17부터)

부분 문자열의 뷰를 반환합니다 [ pos , pos + rlen ) , 여기서 rlen count size ( ) - pos 중 더 작은 값입니다.

목차

매개변수

pos - 첫 번째 문자의 위치
count - 요청된 길이

반환값

부분 문자열의 뷰 [ pos , pos + rlen ) .

예외

std::out_of_range 만약 pos > size ( ) 인 경우.

복잡도

상수.

예제

#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string_view>
int main()
{
    typedef std::size_t count_t, pos_t;
    constexpr std::string_view data{"ABCDEF"};
    std::cout << data.substr() << '\n'; // ABCDEF, 즉 data[0, 5] 즉 [0, 6)
    std::cout << data.substr(pos_t(1)) << '\n'; // BCDEF, 즉 [1, 6)
    std::cout << data.substr(pos_t(2), count_t(3)) << '\n'; // CDE, 즉 [2, 2 + 3)
    std::cout << data.substr(pos_t(4), count_t(42)) << '\n'; // EF, 즉 [4, 6)
    try
    {
        [[maybe_unused]]
        auto sub = data.substr(pos_t(666), count_t(1)); // 예외 발생: pos > size()
    {
    catch (std::out_of_range const& ex)
    {
        std::cout << ex.what() << '\n';
    }
}

가능한 출력:

ABCDEF
BCDEF
CDE
EF
basic_string_view::substr: __pos (which is 666) > __size (which is 6)

참고 항목

문자 복사
(public member function)
뷰에서 문자 검색
(public member function)
부분 문자열 반환
( std::basic_string<CharT,Traits,Allocator> 의 public member function)