chapter2-22.cpp

来自「C++STL程序员开发指南」· C++ 代码 · 共 34 行

CPP
34
字号
//文件名:CHAPTER2-22.cpp
#include <string>
#include <iostream>
using namespace std ;
void main()
{
    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);  // 0 is default position
    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);  // 0 is default position
    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;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?