Технология программирования. Базовые конструкции C/C++. Липачев Е.К. - 61 стр.

UptoLike

Составители: 

61
string str("abcdefgh");
cout<<"\n str="<<str;
char ch='g';
int ind_ch = str.find(ch); //
cout<<"\n ch="<<ch<<" index in str = "<<ind_ch;
string s="def";
int ind_s = str.find(s);
cout<<"\n s="<<s<<" index in str = "<<ind_s<<"\n";
Замечание. Класс string поддерживает несколько перегруженных
операций поиска find(s). Подробности см., напр., Прата С. Язык
программирования C++ (C++11). Лекции и упражнения.
Пример. Поиск вхождения символов в строку с помощью операции
find(). Поиск может оказаться безрезультатным – выяснить это можно с
помощью переменной npos, как показано в примере.
// Поиск в строке, npos
string str("abcdefgh");
cout<<"\n str="<<str;
char ch='z';
int ind_ch = str.find(ch);
if (ind_ch != string::npos)
cout<<"\n ch="<<ch<<" index in str = "<<ind_ch;
else cout<<"\n "<< ch<<" was not found ";
string s="klmn";
int ind_s = str.find(s);
if (ind_s != string::npos)
cout<<"\n s="<<s<<" index in str = "<<ind_s<<"\n";
else cout<<"\n "<<s<< " was not found ";
string str("abcdefgh");
cout<<"\n str="<