名稱空間
變體
操作

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

來自 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 起無異常丟擲)
(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 起無異常丟擲)
(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)

查詢不等於給定字元序列中任何字元的最後一個字元。搜尋僅考慮範圍 [0pos]。如果範圍內的所有字元都可以在給定的字元序列中找到,則返回 npos

1) 查詢不等於 str 中任何字元的最後一個字元。
2) 查詢不等於範圍 [ss + count) 中任何字元的最後一個字元。此範圍可以包含空字元。
如果 [ss + count) 不是有效範圍,則行為未定義。
3) 查詢不等於 s 指向的字串中任何字元的最後一個字元。字串的長度由使用 Traits::length(s) 的第一個空字元確定。
如果 [ss + 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>>
truestd::is_convertible_v<const StringViewLike&, const CharT*>false 時才參與過載決議。

在所有情況下,透過呼叫 Traits::eq 檢查相等性。

目錄

[編輯] 引數

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>>)

如果由於任何原因丟擲異常,此函式無效果(強異常安全保證)。

[編輯] 示例

#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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
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 意外刪除
恢復

[編輯] 另請參閱

查詢給定子字串的第一次出現
(公共成員函式) [編輯]
查詢子串的最後一次出現
(公共成員函式) [編輯]
查詢字元的首次出現
(公共成員函式) [編輯]
查詢字元的首次缺席
(公共成員函式) [編輯]
查詢字元的最後一次出現
(公共成員函式) [編輯]
查詢字元的最後一次缺席
(std::basic_string_view<CharT,Traits> 的公共成員函式) [編輯]