Namespaces
Variants

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

cppreference.net에서 가져옴
 
 
 
std::basic_string
 
size_type find_last_not_of( const basic_string& str,
                            size_type pos = npos ) const;
(1) (noexcept since C++11)
(constexpr since C++20)
size_type find_last_not_of( const CharT* s,
                            size_type pos, size_type count ) const;
(2) (constexpr since C++20)
size_type find_last_not_of( const CharT* s, size_type pos = npos ) const;
(3) (constexpr since C++20)
size_type find_last_not_of( CharT ch, size_type pos = npos ) const;
(4) (noexcept since C++11)
(constexpr since C++20)
template< class StringViewLike >
size_type
    find_last_not_of( const StringViewLike& t,
                      size_type pos = npos ) const noexcept(/* see below */);
(5) (since C++17)
(constexpr since C++20)

주어진 문자 시퀀스에 포함되지 않은 마지막 문자를 찾습니다. 검색은 오직 범위 [0, pos]만 고려합니다. 범위 안의 모든 문자가 주어진 문자 시퀀스에서 발견될 수 있으면 npos를 반환합니다.

1) str에 있는 문자들 중 어느 것과도 같지 않은 마지막 문자를 찾습니다.
2) 범위 [s, s + count) 안에 있는 문자들 중 어느 것과도 같지 않은 마지막 문자를 찾습니다. 이 범위는 널 문자를 포함할 수 있습니다.
만약 [s, s + count)가 유효 범위가 아니라면, 동작은 정의되지 않습니다.
3) s이 가리키는 문자 문자열에 있는 문자들 중 어느 것과도 같지 않은 마지막 문자를 찾습니다. 문자열의 길이는 Traits::length(s)을 사용하여 첫 번째 널 문자로 결정됩니다.
만약 [s, s + Traits::length(s))가 유효 범위가 아니라면, 동작은 정의되지 않습니다.
4) ch와 같지 않은 마지막 문자를 찾습니다.
5) t를 sv에 의해 std::basic_string_view<CharT, Traits> sv = t;을 사용하여 문자열 뷰로 암시적으로 변환한 다음, sv에 있는 문자들 중 어느 것과도 같지 않은 마지막 문자를 찾습니다.
이 오버로드는 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
가 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>가 false인 경우에만 오버로드 해결에 참여합니다.

모든 경우에, 동등성은 Traits::eq을 호출하여 확인합니다.

매개변수

str - 검색할 문자들을 식별하는 문자열
pos - 검색을 종료할 위치
count - 검색할 문자들을 식별하는 문자열의 길이
s - 검색할 문자들을 식별하는 문자열에 대한 포인터
ch - 검색할 문자들을 식별하는 문자
t - 검색할 문자들을 식별하는 객체 ( std::basic_string_view 로 변환 가능)

반환값

발견된 문자의 위치 또는 npos 해당 문자가 발견되지 않을 경우.

예외

1,4) 아무것도 throw하지 않습니다.
5)
noexcept 명세:
noexcept ( std:: is_nothrow_convertible_v <
const T & , std:: basic_string_view < CharT, Traits >> )

어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

예제

#include <iostream>
#include <string>
void show_pos(const std::string& str, std::string::size_type found)
{
    if (found != std::string::npos)
        std::cout << '[' << found << "] = \'" << str[found] << "\'\n";
    else
        std::cout << "not found\n";
}
int main()
{
    std::string str{"abc_123"};
    char const* skip_set{"0123456789"};
    std::string::size_type str_last_pos{std::string::npos};
    show_pos(str, str.find_last_not_of(skip_set)); // [3] = '_'
    str_last_pos = 2;
    show_pos(str, str.find_last_not_of(skip_set, str_last_pos)); // [2] = 'c'
    str_last_pos = 2;
    show_pos(str, str.find_last_not_of('c', str_last_pos)); // [1] = 'b'
    const char arr[]{'3', '4', '5'};
    show_pos(str, str.find_last_not_of(arr)); // [5] = '2'
    str_last_pos = 2;
    std::string::size_type skip_set_size{4};
    show_pos(str, str.find_last_not_of(skip_set,
                                       str_last_pos,
                                       skip_set_size)); // [2] = 'c'
    show_pos(str, str.find_last_not_of("abc")); // [6] = '3'
    str_last_pos = 2;
    show_pos(str, str.find_last_not_of("abc", str_last_pos)); // not found
}

출력:

[3] = '_'
[2] = 'c'
[1] = 'b'
[5] = '2'
[2] = 'c'
[6] = '3'
not found

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 141 C++98 오버로드 (1)은 npos 를 반환할 수 있는 경우가 pos >= size ( ) 인 경우로 제한됨 이 경우 검색 범위는
[ ​ 0 ​ , size ( ) ) 임
LWG 847 C++98 예외 안전성 보장이 없었음 강력한 예외 안전성 보장이 추가됨
LWG 2064 C++11 오버로드 (3,4) 가 noexcept였음 제거됨
LWG 2946 C++17 오버로드 (5) 가 일부 경우에 모호성을 야기함 템플릿으로 만들어 회피함
P1148R0 C++11
C++17
오버로드 (4,5) 의 noexcept가
LWG2064/LWG2946에 의해 실수로 제거됨
복원됨

참고 항목

주어진 부분 문자열의 첫 번째 발생을 찾음
(public member function)
부분 문자열의 마지막 발생을 찾음
(public member function)
문자의 첫 번째 발생을 찾음
(public member function)
문자의 첫 번째 부재를 찾음
(public member function)
문자의 마지막 발생을 찾음
(public member function)
문자의 마지막 부재를 찾음
(public member function of std::basic_string_view<CharT,Traits> )