名前空間
変種
操作

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

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
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(/* see below */);
(5) (C++17以降)
(C++20 以降 constexpr)

指定された文字シーケンスのいずれの文字とも等しくない最後の文字を検索します。検索は範囲 [0pos] のみを考慮します。範囲内のすべての文字が指定された文字シーケンスで見つかった場合、npos が返されます。

1) str の文字のいずれとも等しくない最後の文字を検索します。
2) 範囲 [ss + count) の文字のいずれとも等しくない最後の文字を検索します。この範囲にはヌル文字が含まれる場合があります。
範囲[ss + count)有効な範囲でない場合、動作は未定義です。
3) s が指す文字文字列のいずれの文字とも等しくない最後の文字を検索します。文字列の長さは、最初のヌル文字を Traits::length(s) を使用して決定されます。
範囲[ss + Traits::length(s))有効な範囲でない場合、動作は未定義です。
4) ch と等しくない最後の文字を検索します。
5) t を、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を呼び出すことによってチェックされます。

目次

[edit] パラメータ

str - 検索対象の文字を識別する文字列
pos - 検索を終了する位置
count - 検索対象の文字文字列の長さ
s - 検索対象の文字文字列を指すポインタ
文字 - 検索対象の文字を識別する文字
t - std::basic_string_view に変換可能なオブジェクトで、検索対象の文字を識別するもの

[edit] 戻り値

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

[edit] 例外

1,4) 何も投げません。
5)
noexcept 指定:  
noexcept(std::is_nothrow_convertible_v<
    const T&, std::basic_string_view<CharT, Traits>>)

何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。

[edit]

#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

[edit] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 141 C++98 オーバーロード (1) は、pos >= size() の場合にのみ npos を返すことができる この場合、検索範囲は
[0size()) です。
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 により偶発的に削除されました。
復元されました。

[edit] 関連項目

指定された部分文字列が最初に現れる位置を見つける
(public member function) [編集]
部分文字列が最後に現れる位置を見つける
(public member function) [編集]
文字が最初に現れる位置を見つける
(public member function) [編集]
文字が最初に現れない位置を見つける
(public member function) [編集]
文字が最後に現れる位置を見つける
(public member function) [編集]
文字が最後に現れない位置を見つける
(std::basic_string_view<CharT,Traits> の public メンバー関数) [edit]
English 日本語 中文(简体) 中文(繁體)