跳到主要内容

std::string find_first_not_of() 方法

// (1) Const version only
constexpr size_type find_first_not_of( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find_first_not_of( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find_first_not_of( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find_first_not_of( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
constexpr size_type find_first_not_of( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

查找第一个不等于给定字符序列中任何字符的字符。
搜索仅考虑区间 [ pos, size() )

  • (1) 查找第一个不等于 str 中任何字符的字符。

  • (2) 查找第一个不等于范围 [ s, s + count ) 中任何字符的字符。
    此范围可以包含空字符。

  • (3) 查找第一个不等于 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>>truestd::is_convertible_v<const StringViewLike&, const CharT*>false 时参与重载决议。

参数

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

备注

Traits::eq() 用于比较。默认情况下,`Traits` 是 std::char_traits<CharT>

示例

#include <string>
#include <iostream>

int main() {
std::string to_search = "Some data with %MACROS to substitute";

std::cout << "Before: " << to_search << '\n';

auto pos = std::string::npos;
while ((pos = to_search.find('%')) != std::string::npos) {
// Permit uppercase letters, lowercase letters and numbers in macro names
const auto after = to_search.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789", pos + 1);

// Now to_search[pos] == '%' and to_search[after] == ' ' (after the 'S')

if(after != std::string::npos)
to_search.replace(pos, after - pos, "some very nice macros");
}

std::cout << "After: " << to_search << '\n';
}
输出
Before: Some data with %MACROS to substitute
After: Some data with some very nice macros to substitute
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::string find_first_not_of() 方法

// (1) Const version only
constexpr size_type find_first_not_of( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find_first_not_of( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find_first_not_of( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find_first_not_of( CharT ch, size_type pos = 0 ) const noexcept;

// (5) Const version only
template < class StringViewLike >
constexpr size_type find_first_not_of( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);

查找第一个不等于给定字符序列中任何字符的字符。
搜索仅考虑区间 [ pos, size() )

  • (1) 查找第一个不等于 str 中任何字符的字符。

  • (2) 查找第一个不等于范围 [ s, s + count ) 中任何字符的字符。
    此范围可以包含空字符。

  • (3) 查找第一个不等于 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>>truestd::is_convertible_v<const StringViewLike&, const CharT*>false 时参与重载决议。

参数

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

备注

Traits::eq() 用于比较。默认情况下,`Traits` 是 std::char_traits<CharT>

示例

#include <string>
#include <iostream>

int main() {
std::string to_search = "Some data with %MACROS to substitute";

std::cout << "Before: " << to_search << '\n';

auto pos = std::string::npos;
while ((pos = to_search.find('%')) != std::string::npos) {
// Permit uppercase letters, lowercase letters and numbers in macro names
const auto after = to_search.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789", pos + 1);

// Now to_search[pos] == '%' and to_search[after] == ' ' (after the 'S')

if(after != std::string::npos)
to_search.replace(pos, after - pos, "some very nice macros");
}

std::cout << "After: " << to_search << '\n';
}
输出
Before: Some data with %MACROS to substitute
After: Some data with some very nice macros to substitute
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。