string c++详解 find_first_not_of() find_first_of()

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

 

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.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// string::find_first_not_of
#include 
#include 
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;
}



 

First non-alphabetic character is - at position 12


 

 

 

 

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 use
find 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// string::find_first_of
#include 
#include 
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;
}



 

R*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.
 
string::npos:
 
 
       
static const size_t npos = -1;
Maximum value for size_t
npos is a static member constant value with the greatest possible value for an element of type size_t.

This value, when used as the value for a count parameter n in
string's member functions, roughly indicates "as many as possible".

When used in some pos parameters that allow for out-of-range values, npos indicates the end of the string.

As a return value it is usually used to indicate failure.

This constant is actually defined with a value of -1 (for any
trait), which because size_t is an unsigned integral type, becomes the largest possible representable value for this type.


 

 

 

你可能感兴趣的:(string,c++详解)