好文收藏_关于find_first_not_of函数

本文详细介绍了C++标准库中字符串类成员函数find_first_not_of和find_first_of的使用方法及示例代码。find_first_not_of用于查找与指定字符集不匹配的第一个字符的位置,而find_first_of则用于查找与指定字符集匹配的第一个字符的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://blog.youkuaiyun.com/ffjbq/article/details/7611255


 

这两个方法都是查找与()中指定的字符串中任意一个字符都不相符的字符的位置地址,而不是返回的是与()中制定的字符串完全匹配的字符串的首地址

find_first_not_of()

语法:

  size_type find_first_not_of( const basic_string &str, size_type index = 0 );
  size_type find_first_not_of( const char *str, size_type index = 0 );
  size_type find_first_not_of( const char *str, size_type index, size_type num );
  size_type find_first_not_of( char ch, size_type index = 0 );

find_first_not_of()函数:

  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
  • 在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_t find_first_not_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_not_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_not_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_not_of ( char c, size_t pos = 0 ) const;
Find absence of character in string

 

Searches for the first character in the object which is not part of eitherstr,s or c, and returns its position.

When pos is specified the search only includes characters on or after positionpos, ignoring any content in the previous character positions.

 

// string::find_first_not_of
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("look for non-alphabetic characters...");
  size_t found;

  found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
  if (found!=string::npos)
  {
    cout << "First non-alphabetic character is " << str[found];
    cout << " at position " << int(found) << endl;
  }

  return 0;
}
size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

Find character in string

Searches the string for any of the characters that are part of eitherstr,s or c, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after positionpos, ignoring any possible occurrences at previous character positions.

Notice that for a match to happen it is enough that one of the characters matches in the string (any of them). To search for an entire sequence of characters usefind instead.

 

Return Value

The position of the first occurrence in the string of any of the characters searched for.
If the content is not found, the member value npos is returned.

Example

// string::find_first_of
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

  found=str.find_first_of("aeiou");
// string::npos 表示string的最后一个位置的地址
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  cout << str << endl;

  return 0;
}

 

 

转载于:https://www.cnblogs.com/xf666/p/7087052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值