欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++中string find函数的使用

程序员文章站 2022-07-12 14:37:56
...

http://www.cplusplus.com/reference/string/string/find/

string (1)从位置pos开始,找字符串str,返回匹配首字符的位置,没找到返回string::npos size_t find (const string& str, size_t pos = 0) const noexcept;
c-string (2)从位置pos开始,找字符指针对应的字符,返回匹配首字符的位置,没找到返回string::npos size_t find (const char* s, size_t pos = 0) const;
buffer (3)从位置pos开始,寻找长度为n的字符指针,返回匹配首字符的位置,没找到返回string::npos size_t find (const char* s, size_t pos, size_type n) const;
size_t find (const char* s, size_t pos, size_type n) const;
character (4)从位置pos开始,寻找字符c的位置,返回匹配字符的位置,没找到返回string::npos
size_t find (char c, size_t pos = 0) const noexcept;

Find content in string

Searches the string for the first occurrence of the sequence specified by its arguments.
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
 

find_first_of()查找子串中的某个字符最先出现的位置。find_first_of()不是全匹配,而find()是全匹配

Parameters

str:待寻找目标的字符串

pos:寻找的字符第一次在目标字符串str中出现的位置。如果pos>string length,函数将永远不会找到匹配;

注意:第一个字符的下标是0不是1;
Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.

s 一个字符数组指针

Pointer to an array of characters.
If argument n is specified (3), the sequence to match are the first n characters in the array.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character.

n 待匹配的字符串的长度

c 待寻找的单个字符

 


size_t is an unsigned integral type (the same as member type string::size_type).

 

Return Value返回值

返回第一次匹配上的字符的位置;

如果没有匹配则返回string::npos;

size_t是无符号的整形,相当于unsigned int (和成员类型string::size_type一样)

string::npos是size_t 的最大值,npos是一个静态成员常量。当使用在表示string类成员函数中是,表示string的末端,常常作为没有匹配时的指示值。

例子

// string::find
#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}

 

输出:

first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.

 

**还有一个std::find的函数**http://www.cplusplus.com/reference/algorithm/find/

**size_t**

size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.h的C++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。

例如:bitset的size操作返回bitset对象中二进制位中1的个数,返回值类型是size_t。例如:在用下标访问元素时,vector使用vector::size_type作为下标类型,而数组下标的正确类型则是size_t。vector使用的下标实际也是size_t,源码是typedef size_t size_type。

相关标签: find