Namespaces
Variants

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

From cppreference.net
std::basic_string
size_type find_last_not_of ( const basic_string & str,
size_type pos = npos ) const ;
(1) (C++11以降noexcept)
(C++20以降constexpr)
size_type find_last_not_of ( const CharT * s,
size_type pos, size_type count ) const ;
(2) (C++20以降constexpr)
size_type find_last_not_of ( const CharT * s, size_type pos = npos ) const ;
(3) (C++20以降constexpr)
size_type find_last_not_of ( CharT ch, size_type pos = npos ) const ;
(4) (C++11以降noexcept)
(C++20以降constexpr)
template < class StringViewLike >

size_type
find_last_not_of ( const StringViewLike & t,

size_type pos = npos ) const noexcept ( /* 下記参照 */ ) ;
(5) (C++17以降)
(C++20以降constexpr)

指定された文字シーケンス内のいずれの文字とも等しくない最後の文字を検索します。検索は範囲 [ 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 を呼び出すことでチェックされます。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters, Return value, Exceptions, Example, Defect reports, See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、IDなどは一切変更していません - 番号部分もそのまま保持しています - フォーマットと構造は完全に維持されています

パラメータ

str - 検索対象の文字を特定する文字列
pos - 検索を終了する位置
count - 検索対象の文字を特定する文字列の長さ
s - 検索対象の文字を特定する文字列へのポインタ
ch - 検索対象の文字を特定する文字
t - 検索対象の文字を特定するオブジェクト( std::basic_string_view に変換可能)

戻り値

見つかった文字の位置、または npos そのような文字が見つからない場合。

例外

1,4) 例外を送出しない。
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)
文字の最後の不在を検索する
( std::basic_string_view<CharT,Traits> のpublic member function)