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

来自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
成员函数
元素访问
迭代器
容量
操作
搜索
basic_string::find_last_of
常量
推导指引 (C++17)
非成员函数
I/O
比较
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
数值转换
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
辅助类
 
(1)
size_type find_last_of( const basic_string& str,
                        size_type pos = npos ) const;
(C++11 前)
constexpr size_type find_last_of( const basic_string& str,
                                   size_type pos = npos ) const noexcept;
(C++11 起)
(C++20 前)
(2)
size_type find_last_of( const CharT* s,
                        size_type pos, size_type count ) const;
(C++11 前)
constexpr size_type find_last_of( const CharT* s,
                                  size_type pos, size_type count ) const;
(C++11 起)
(3)
size_type find_last_of( const CharT* s, size_type pos = npos ) const;
(C++20 前)
constexpr size_type find_last_of( const CharT* s, size_type pos = npos ) const;
(C++20 起)
(4)
size_type find_last_of( CharT ch, size_type pos = npos ) const;
(C++11 前)
size_type find_last_of( CharT ch, size_type pos = npos ) const noexcept;
(C++11 起)
(C++20 前)
constexpr size_type find_last_of( CharT ch,
                                  size_type pos = npos ) const noexcept;
(C++20 起)
(5)
template < class T >

size_type
    find_last_of( const T& t,

                  size_type pos = npos ) const noexcept(/* see below */);
(C++17 起)
(C++20 前)
template < class T >

constexpr size_type
    find_last_of( const T& t,

                  size_type pos = npos ) const noexcept(/* see below */);
(C++20 起)

寻找等于给定字符序列中字符之一的最后字符。不指定准确的搜索算法。搜索只考虑区间 [0, pos] 。若区间中不存在这种字符,则返回 npos

1) 寻找等于 str 中字符之一的最后字符。
2) 寻找等于范围 [s, s+count) 中字符之一的最后字符。此范围能包含空字符。
3) 寻找等于 s 所指向的字符串中字符之一的最后字符。用 Traits::length(s) ,由首个空字符确定字符串长度。
4) 寻找等于 ch 的最后字符。
5) 如同用 std::basic_string_view<CharT, Traits> sv = t; 隐式转换 t 为 string_view sv ,然后寻找等于 sv 中字符之一的最后字符。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>>truestd::is_convertible_v<const T&, const CharT*>false 才参与重载决议。

所有情况下,均调用 Traits::eq 检查相等性。

参数

str - 鉴别要搜索的字符的 string
pos - 搜索结束的位置
count - 鉴别要搜索的字符的字符串长度
s - 指向鉴别要搜索的字符的字符串的指针
ch - 要搜索的字符
t - 鉴别要搜索的字符的对象(可转换为 std::basic_string_view

返回值

找到的字符位置,或若找不到这种字符则为 npos

异常

5)
noexcept 说明:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

示例

#include<string>
#include<iostream>
int main()
{
    const std::string path="/root/config";
    auto const pos=path.find_last_of('/');
    const auto leaf=path.substr(pos+1);
 
    std::cout << leaf << '\n';
}

输出:

config

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
LWG 2064 C++11 重载 (3)(4) 为 noexcept 移除
LWG 2946 C++17 string_view 重载在某些情况下导致歧义 通过使之为模板来避免
P1148R0 C++11
C++17
重载 (4)/(5) 的 noexcept 意外地被 LWG2064/LWG2946 丢弃 恢复

参阅

于字符串中寻找字符
(公开成员函数)
寻找子串的最后一次出现
(公开成员函数)
寻找字符的首次出现
(公开成员函数)
寻找字符的首次缺失
(公开成员函数)
寻找字符的最后一次缺失
(公开成员函数)