c++11 标准模板(STL)string(十三)

string 查找          find_first_of         find_last_of


寻找字符的首次出现

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

寻找等于给定字符序列中字符之一的首个字符。搜索只考虑区间 [pos, size()) 。若区间中不存在字符,则返回 npos 。

1) 寻找等于 str 中字符之一的首个字符。

size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;
  (C++11 前) 

size_type find_first_of( const basic_string& str, size_type pos = 0 ) const noexcept;
  (C++11 起) 

调用示例

    // 1) 寻找等于 str 中字符之一的首个字符。
    size_t p = str1.find_first_of(str2);
    std::cout << "find_first_of str2 : " << p << std::endl;
    std::cout << "find_first_of str3 : " << str1.find_first_of(str3, 2)
              << std::endl << std::endl;

 2) 寻找等于范围 [s, s+count) 中字符中字符之一的首个字符。此范围能包含空字符。

size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;

调用示例

    // 2) 寻找等于范围 [s, s+count) 中字符中字符之一的首个字符。此范围能包含空字符。
    p = str1.find_first_of(str2.c_str(), 0, 3);
    std::cout << "find_first_of str2 : " << p << std::endl << std::endl;

 3) 寻找等于 s 所指向的字符串中字符之一的首个字符。

由首个空字符,用 Traits::length(s) 确定字符串长度。

size_type find_first_of( const CharT* s, size_type pos = 0 ) const;

调用示例

    // 3) 寻找等于 s 所指向的字符串中字符之一的首个字符。
    // 由首个空字符,用 Traits::length(s) 确定字符串长度。
    p = str1.find_first_of(str2.c_str());
    std::cout << "find_first_of str2 : " << p << std::endl;
    std::cout << "find_first_of str3 : " << str1.find_first_of(str3.c_str(), 2)
              << std::endl << std::endl;

 4) 寻找等于 ch 的首个字符。

size_type find_first_of( CharT ch, size_type pos = 0 ) const;
  (C++11 前) 

size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
  (C++11 起) 

调用示例

    // 4) 寻找等于 ch 的首个字符。
    p = str1.find_first_of('i');
    std::cout << "find_first_of 'i' : " << p << std::endl;
    std::cout << "find_first_of 'j' : " << str1.find_first_of('j', 2)
              << std::endl << std::endl;

参数

str-鉴别要搜索的字符的 string
pos-搜索开始的位置
count-鉴别要搜索的字符的字符串长度
s-指向鉴别要搜索的字符的字符串的指针
ch-鉴别要搜索的字符的字符

返回值

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

寻找字符的最后一次出现

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

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

1) 寻找等于 str 中字符之一的最后字符。

size_type find_last_of( const basic_string& str, size_type pos = npos ) const;
  (C++11 前) 

size_type find_last_of( const basic_string& str, size_type pos = npos ) const noexcept;
  (C++11 起) 

调用示例

    p = str1.find_last_of(str2);
    std::cout << "find_last_of str2 : " << p << std::endl;
    std::cout << "find_last_of str3 : " << str1.find_last_of(str3, str1.size() - 2)
              << std::endl << std::endl;

 2) 寻找等于范围 [s, s+count) 中字符之一的最后字符。此范围能包含空字符。

size_type find_last_of( const CharT* s, size_type pos, size_type count ) const;

调用示例

    // 2) 寻找等于范围 [s, s+count) 中字符之一的最后字符。此范围能包含空字符。
    p = str1.find_last_of(str2.c_str(), string::npos, 3);
    std::cout << "find_last_of str2 : " << p << std::endl << std::endl;

 3) 寻找等于 s 所指向的字符串中字符之一的最后字符。

Traits::length(s) ,由首个空字符确定字符串长度。

size_type find_last_of( const CharT* s, size_type pos = npos ) const;

调用示例

    // 3) 寻找等于 s 所指向的字符串中字符之一的最后字符。
    // 由首个空字符,用 Traits::length(s) 确定字符串长度。
    p = str1.find_last_of(str2.c_str());
    std::cout << "find_last_of str2 : " << p << std::endl;
    std::cout << "find_last_of str3 : "
              << str1.find_last_of(str3.c_str(), str1.size() - 2)
              << std::endl << std::endl;

 4) 寻找等于 ch 的最后字符。

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

调用示例

    // 4) 寻找等于 ch 的最后字符。
    p = str1.find_last_of('i');
    std::cout << "find_last_of 'i' : " << p << std::endl;
    std::cout << "find_last_of 'j' : " << str1.find_last_of('j', str1.size())
              << std::endl << std::endl;

参数

str-鉴别要搜索的字符的 string
pos-搜索结束的位置
count-鉴别要搜索的字符的字符串长度
s-指向鉴别要搜索的字符的字符串的指针
ch-要搜索的字符

返回值

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

调用示例

    // 寻找字符的首次出现
    string str1 = "abcefijklmnnmlkjifecijkfij";
    std::cout << "str1 : " << str1 << ",size:" << str1.size() << std::endl;
    string str2 = "aijk";
    string str3 = "kij";

    // 1) 寻找等于 str 中字符之一的首个字符。
    size_t p = str1.find_first_of(str2);
    std::cout << "find_first_of str2 : " << p << std::endl;
    std::cout << "find_first_of str3 : " << str1.find_first_of(str3, 2)
              << std::endl << std::endl;

    // 2) 寻找等于范围 [s, s+count) 中字符中字符之一的首个字符。此范围能包含空字符。
    p = str1.find_first_of(str2.c_str(), 0, 3);
    std::cout << "find_first_of str2 : " << p << std::endl << std::endl;

    // 3) 寻找等于 s 所指向的字符串中字符之一的首个字符。
    // 由首个空字符,用 Traits::length(s) 确定字符串长度。
    p = str1.find_first_of(str2.c_str());
    std::cout << "find_first_of str2 : " << p << std::endl;
    std::cout << "find_first_of str3 : " << str1.find_first_of(str3.c_str(), 2)
              << std::endl << std::endl;

    // 4) 寻找等于 ch 的首个字符。
    p = str1.find_first_of('i');
    std::cout << "find_first_of 'i' : " << p << std::endl;
    std::cout << "find_first_of 'j' : " << str1.find_first_of('j', 2)
              << std::endl << std::endl;

    // 寻找字符的最后一次出现
    // 1) 寻找等于 str 中字符之一的最后字符。
    p = str1.find_last_of(str2);
    std::cout << "find_last_of str2 : " << p << std::endl;
    std::cout << "find_last_of str3 : " << str1.find_last_of(str3, str1.size() - 2)
              << std::endl << std::endl;

    // 2) 寻找等于范围 [s, s+count) 中字符之一的最后字符。此范围能包含空字符。
    p = str1.find_last_of(str2.c_str(), string::npos, 3);
    std::cout << "find_last_of str2 : " << p << std::endl << std::endl;

    // 3) 寻找等于 s 所指向的字符串中字符之一的最后字符。
    // 由首个空字符,用 Traits::length(s) 确定字符串长度。
    p = str1.find_last_of(str2.c_str());
    std::cout << "find_last_of str2 : " << p << std::endl;
    std::cout << "find_last_of str3 : "
              << str1.find_last_of(str3.c_str(), str1.size() - 2)
              << std::endl << std::endl;

    // 4) 寻找等于 ch 的最后字符。
    p = str1.find_last_of('i');
    std::cout << "find_last_of 'i' : " << p << std::endl;
    std::cout << "find_last_of 'j' : " << str1.find_last_of('j', str1.size())
              << std::endl << std::endl;

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值