std::basic_string_view<CharT,Traits>::subview
cppreference.com에서
constexpr std::basic_string_view<CharT, Traits> subview( size_type pos = 0,
size_type count = npos ) const;
|
(C++26부터) | |
[pos, pos + rlen) 부분 문자열의 뷰를 반환합니다. 여기서 rlen은 count과 size() - pos 중 더 작은 값입니다.
매개변수
| pos | - | 첫 번째 문자의 위치 |
| count | - | 요청된 길이 |
반환값
[pos, pos + rlen) 부분 문자열의 뷰.
예외
인 경우 std::out_of_range.
pos > size()
복잡도
상수 시간.
참고 사항
| 기능-테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_string_subview |
202506L |
(C++26) | std::basic_string_view::subview, std::basic_string::subview
|
예제
이 코드 실행하기
#include <cassert>
#include <iostream>
#include <string_view>
int main()
{
const std::string_view 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) |
부분 뷰를 반환합니다 (공개 멤버 함수) |