Namespaces
Variants

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

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)

같이 보기

(C++26)
하위 뷰를 반환합니다
(공개 멤버 함수)
뷰의 문자를 복사합니다
(공개 멤버 함수)
뷰에서 문자를 찾습니다
(공개 멤버 함수)
부분 문자열을 반환합니다
(공개 멤버 함수 of std::basic_string<CharT,Traits,Allocator>)