string类型中find和find_first_of

来源:互联网 发布:ext js 学多久 编辑:程序博客网 时间:2024/05/30 20:09

1、find_first_of()
参数:pos查找起始位置
n待查找字符串的前n个字符。
string str1(“Please, replace the vowels in this sentence by asterisks.”)
string str2(“ae”)
只要str1中含有str2中的任意字符,就会查找成功并返回位置。
int found=str1.find_first_of(str2);
返回的就是ae这两个字符任何一个首次出现在str1中的位置。

string (1)  size_t find_first_of (const string& str, size_t pos = 0) const noexcept;c-string (2)    size_t find_first_of (const char* s, size_t pos = 0) const;buffer (3)  size_t find_first_of (const char* s, size_t pos, size_t n) const;character (4)   size_t find_first_of (char c, size_t pos = 0) const noexcept;

The position of the first character that matches.

If no matches are found, the function returns string::npos.

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

类成员函数find_first_of()
通俗的讲就是在一个字符串str1中查找是否含有另一个字符串str2中的元素,只要含有的位置都能查找出来。如果没有符合条件的就返回npos
example:

// string::find_first_of#include <iostream>       // std::cout#include <string>         // std::string#include <cstddef>        // std::size_tint main(){    std::string str("Please, replace the vowels in this sentence by asterisks.");    std::size_t found = str.find_first_of("aeiou");    while (found != std::string::npos)    {        str[found] = '*';        found = str.find_first_of("aeiou", found + 1);    }    std::cout << str << '\n';    return 0;}

结果为返回了所有含有aeiou这几个元素的位置并替换为*:

Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks

2、find()

string str1(“Please, replace the vowels in this sentence by asterisks.”)
string str2(“the”)

str1.find(str2)//从串str1中查找str2,返回str2中首个字符在str1中的位置
str1.find(str2,5)//从str1 第5个字符开始查找str2
str1.find(“vowels”)//如果vowels在str1 中能找到,返回的是v在str1中的位置
str1.find(“replace the”,2,2)//从str1中的第二个字符开始查找replace the 的前两个字符在str1 中的位置。

string (1)  size_t find (const string& str, size_t pos = 0) const noexcept;c-string (2)    size_t find (const char* s, size_t pos = 0) const;buffer (3)  size_t find (const char* s, size_t pos, size_type n) const;character (4)   size_t find (char c, size_t pos = 0) const noexcept;

The position of the first character of the first match.

If no matches were found, the function returns string::npos.

size_t is an unsigned integral type (the same as member type string::size_type).
example:
要在str1中find str2,
返回的是查找到的字符串第一次出现在字符串中的位置。

// string::find#include <iostream>       // std::cout#include <string>         // std::stringint 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: 14second 'needle' found at: 44'haystack' also found at: 30Period found at: 51There are two prepositions in this haystack with needles.