Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>::subview

From ko.cppreference.net
 
 
 
std::basic_string
 
constexpr std::basic_string_view<CharT, Traits> subview( size_type pos = 0,
                                                         size_type count = npos ) const;
(C++26부터)

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

return std::basic_string_view<CharT, Traits>(*this).subview(pos, count);와 동일합니다.

매개변수

pos - 포함할 첫 번째 문자의 위치
count - 하위 뷰의 길이

반환값

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

예외

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

복잡도

상수 시간.

참고 사항

기능-테스트 매크로 표준 기능
__cpp_lib_string_subview 202506L (C++26) std::basic_string::subview, std::basic_string_view::subview

예제

#include <cassert>
#include <iostream>
#include <string>
#include <string_view>

int main()
{
    const std::string s{"Life is life!"};
    assert(s.subview(5) == "is life!");
    assert(s.subview(5, 13) == "is life!");
    assert(s.subview(5, 2) == "is");

    try
    {
        // pos is out of bounds, throws
        const auto pos{s.length() + 13};
        [[maybe_unused]] auto x_x{s.subview(pos)};
    }
    catch (const std::out_of_range& ex)
    {
        std::cout << "Exception: " << ex.what() << '\n';
    }
}

가능한 출력:

Exception: basic_string_view::substr: __pos (which is 26) > __size (which is 13)

같이 보기

부분 문자열을 반환합니다
(공개 멤버 함수)
(C++26)
하위 뷰를 반환합니다
(std::basic_string_view<CharT,Traits>의 공개 멤버 함수)