#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]) { string str1("Heartbeat"); string str2("abcde"); int iPos = 0; cout << "The string to search is '" << str1.c_str() << "'" << endl; //find the first instance in str1 of any characters in str2 iPos = str1.find_first_of(str2, 0); cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl; //start looking in the third position iPos = str1.find_first_of(str2, 2); cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl; //use an array of the element type as the set of elements to search for; //look for anything after the fourth position char achVowels[] = {'a', 'e', 'i', 'o', 'u'}; iPos = str1.find_first_of(achVowels, 4, sizeof(achVowels)); cout << "Element in '"; for (int i = 0; i < sizeof(achVowels); ++i) { cout << achVowels[i]; } cout << "' found at position " << iPos << endl; //use a string literal to specify the set of elements char szVowels[] = "aeiou"; iPos = str1.find_first_of(szVowels, 0); cout << "Element in '" << szVowels << "' found at position " << iPos << endl; //look for a specific character beginning in the third position iPos = str1.find_first_of('e', 2); cout << "'e' found at position " << iPos << endl; return 0; }
运行结果为:C++ string类中的字符串查找(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://127.0.0.1/wyypgz.html