代码示例:
#include
#include
using namespace std;
int main ()
{
string str1("one two three four one two three four");
cout<<"size_t find (const string& str, size_t pos = npos) const noexcept;"<string str2("one");
size_t found1 = str1.find(str2);
if (found1 != string::npos)
{
str1.replace(found1, str2.size(), "ONE");
}
cout<cout<cout<<"size_t find (const char* s, size_t pos = npos) const;"<"two");
if (found2 != string::npos)
{
str1.replace(found2, strlen("two"), "TWO");
}
cout<cout<cout<<"size_t find (const char* s, size_t pos, size_t n) const;"<"three", 0, 5);
if (found3 != string::npos)
{
str1.replace(found3, strlen("three"), "THREE");
}
cout<cout<cout<<"size_t find (char c, size_t pos = npos) const noexcept;"<'f');
if (found4 != string::npos)
{
str1.replace(found4, sizeof('f'), "F");
}
cout<cout<"pause");
return 0;
}
=>size_t find (const string& str, size_t pos = npos) const noexcept;
ONE two three four one two three four
size_t find (const char* s, size_t pos = npos) const;
ONE TWO three four one two three four
size_t find (const char* s, size_t pos, size_t n) const;
ONE TWO THREE four one two three four
size_t find (char c, size_t pos = npos) const noexcept;
ONE TWO THREE Four one two three four
代码示例:
(1)代码示例一:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz");
cout<<"size_t find_first_of (const string& str, size_t pos = 0) const;"<string str2("aeimqu");
size_t found1 = str1.find_first_of(str2);
while (found1 != string::npos)
{
str1[found1]='*';
found1 = str1.find_first_of(str2, found1+1);
}
cout<cout<cout<<"size_t find_first_of (const char* s, size_t pos = 0) const;"<"dhlptxz");
while (found2 != string::npos)
{
str1[found2]='-';
found2 = str1.find_first_of("dhlptx", found2+1);
}
cout<cout<cout<<"size_t find_first_of (const char* s, size_t pos, size_t n) const;"<"yz", 0, 1);
while (found3 != string::npos)
{
str1[found3]='Y';
found3 = str1.find_first_of("yz", found3+1, 1);
}
cout<cout<cout<<"size_t find_first_of (char c, size_t pos = 0) const;"<'z');
while (found4 != string::npos)
{
str1[found4]='2';
found4 = str1.find_first_of("z", found4+1);
}
cout<cout<"pause");
return 0;
}
=>size_t find_first_of (const string& str, size_t pos = 0) const;
*bcd *fgh *jkl *nop *rst *vwx yz
size_t find_first_of (const char* s, size_t pos = 0) const;
*bc- *fg- *jk- *no- *rs- *vw- yz
size_t find_first_of (const char* s, size_t pos, size_t n) const;
*bc- *fg- *jk- *no- *rs- *vw- Yz
size_t find_first_of (char c, size_t pos = 0) const;
*bc- *fg- *jk- *no- *rs- *vw- Y2
(2)代码示例二:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
cout<<"size_t find_first_of (const string& str, size_t pos = 0) const;"<string str2("aeimqu");
size_t found1 = str1.find_first_of(str2);
if (found1 != string::npos)
{
str1[found1]='*';
//found1 = str1.find_first_of(str2, found1+1);
}
cout<cout<cout<<"size_t find_first_of (const char* s, size_t pos = 0) const;"<"dhlptxz");
if (found2 != string::npos)
{
str1[found2]='-';
//found2 = str1.find_first_of("dhlptx", found2+1);
}
cout<cout<cout<<"size_t find_first_of (const char* s, size_t pos, size_t n) const;"<"yz", 0, 1);
if (found3 != string::npos)
{
str1[found3]='Y';
//found3 = str1.find_first_of("yz", found3+1, 1);
}
cout<cout<cout<<"size_t find_first_of (char c, size_t pos = 0) const;"<'z');
if (found4 != string::npos)
{
str1[found4]='2';
//found4 = str1.find_first_of("z", found4+1);
}
cout<cout<"pause");
return 0;
}
=>size_t find_first_of (const string& str, size_t pos = 0) const;
*bcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_of (const char* s, size_t pos = 0) const;
*bc- efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_of (const char* s, size_t pos, size_t n) const;
*bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_of (char c, size_t pos = 0) const;
*bc- efgh ijkl mnop qrst uvwx Y2 zy xwvu tsrq ponm lkji hgfe dcba
代码示例:
(1)示例代码一:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz");
cout<<"size_t find_first_not_of (const string& str, size_t pos = 0) const;"<string str2("bcd fgh jkl nop rst vwx yz");
size_t found1 = str1.find_first_not_of(str2);
while (found1 != string::npos)
{
str1[found1]='*';
found1 = str1.find_first_not_of(str2, found1+1);
}
cout<cout<cout<<"size_t find_first_not_of (const char* s, size_t pos = 0) const;"<"abc efg ijk mno qrs uvw yz *");
while (found2 != string::npos)
{
str1[found2]='-';
found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *", found2+1);
}
cout<cout<cout<<"size_t find_first_not_of (const char* s, size_t pos, size_t n) const;"<"abcd efgh ijkl mnop qrst uvwx z * -", 0, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
while (found3 != string::npos)
{
str1[found3]='Y';
found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", found3+1, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
}
cout<cout<cout<<"size_t find_first_not_of (char c, size_t pos = 0) const;"<'z', str1.size()-1);
if (found4 == string::npos)
{
str1[str1.size()-1]='2';
}
cout<cout<"pause");
return 0;
}
=>size_t find_first_of (const string& str, size_t pos = 0) const;
*bcd *fgh *jkl *nop *rst *vwx yz
size_t find_first_of (const char* s, size_t pos = 0) const;
*bc- *fg- *jk- *no- *rs- *vw- yz
size_t find_first_of (const char* s, size_t pos, size_t n) const;
*bc- *fg- *jk- *no- *rs- *vw- Yz
size_t find_first_of (char c, size_t pos = 0) const;
*bc- *fg- *jk- *no- *rs- *vw- Y2
(2)示例代码二:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
cout<<"size_t find_first_not_of (const string& str, size_t pos = 0) const;"<string str2("bcd fgh jkl nop rst vwx yz");
size_t found1 = str1.find_first_not_of(str2);
if (found1 != string::npos)
{
str1[found1]='*';
//found1 = str1.find_first_not_of(str2, found1+1);
}
cout<cout<cout<<"size_t find_first_not_of (const char* s, size_t pos = 0) const;"<"abc efg ijk mno qrs uvw yz *");
if (found2 != string::npos)
{
str1[found2]='-';
//found2 = str1.find_first_not_of("abc efg ijk mno qrs uvw yz *", found2+1);
}
cout<cout<cout<<"size_t find_first_not_of (const char* s, size_t pos, size_t n) const;"<"abcd efgh ijkl mnop qrst uvwx z * -", 0, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
if (found3 != string::npos)
{
str1[found3]='Y';
//found3 = str1.find_first_not_of("abcd efgh ijkl mnop qrst uvwx z * -", found3+1, strlen("abcd efgh ijkl mnop qrst uvwx z * -"));
}
cout<cout<cout<<"size_t find_first_not_of (char c, size_t pos = 0) const;"<'a', str1.size()-1);
if (found4 == string::npos)
{
str1[str1.size()-1]='A';
}
cout<cout<"pause");
return 0;
}
=>size_t find_first_not_of (const string& str, size_t pos = 0) const;
*bcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_not_of (const char* s, size_t pos = 0) const;
*bc- efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
*bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcba
size_t find_first_not_of (char c, size_t pos = 0) const;
*bc- efgh ijkl mnop qrst uvwx Yz zy xwvu tsrq ponm lkji hgfe dcbA
代码示例:
#include
#include
using namespace std;
int main ()
{
string str1("one two three four one two three four");
cout<<"size_t rfind (const string& str, size_t pos = npos) const noexcept;"<string str2("one");
size_t found1 = str1.rfind(str2);
if (found1 != string::npos)
{
str1.replace(found1, str2.size(), "ONE");
}
cout<cout<cout<<"size_t rfind (const char* s, size_t pos = npos) const;"<"two");
if (found2 != string::npos)
{
str1.replace(found2, strlen("two"), "TWO");
}
cout<cout<cout<<"size_t rfind (const char* s, size_t pos, size_t n) const;"<"three", string::npos, 5);
if (found3 != string::npos)
{
str1.replace(found3, strlen("three"), "THREE");
}
cout<cout<cout<<"size_t rfind (char c, size_t pos = npos) const noexcept;"<'f');
if (found4 != string::npos)
{
str1.replace(found4, sizeof('f'), "F");
}
cout<cout<"pause");
return 0;
}
=>size_t rfind (const string& str, size_t pos = npos) const noexcept;
one two three four ONE two three four
size_t rfind (const char* s, size_t pos = npos) const;
one two three four ONE TWO three four
size_t rfind (const char* s, size_t pos, size_t n) const;
one two three four ONE TWO THREE four
size_t rfind (char c, size_t pos = npos) const noexcept;
one two three four ONE TWO THREE Four
代码示例:
(1)代码示例一:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz");
cout<<"size_t find_last_of (const string& str, size_t pos = npos) const;"<string str2("aeimqu");
size_t found1 = str1.find_last_of(str2);
while (found1 != string::npos)
{
str1[found1]='*';
found1 = str1.find_last_of(str2, found1-1);
}
cout<cout<cout<<"size_t find_last_of (const char* s, size_t pos = npos) const;"<"dhlptx");
while (found2 != string::npos)
{
str1[found2]='-';
found2 = str1.find_last_of("dhlptx", found2-1);
}
cout<cout<cout<<"size_t find_last_of (const char* s, size_t pos, size_t n) const;"<"y", string::npos, 1);
while (found3 != string::npos)
{
str1[found3]='Y';
found3 = str1.find_last_of("y", found3-1, 1);
}
cout<cout<cout<<"size_t find_last_of (char c, size_t pos = npos) const;"<'z');
while (found4 != string::npos)
{
str1[found4]='2';
found4 = str1.find_last_of("z", found4-1);
}
cout<cout<"pause");
return 0;
}
=>size_t find_last_of (const string& str, size_t pos = npos) const;
*bcd *fgh *jkl *nop *rst *vwx yz
size_t find_last_of (const char* s, size_t pos = npos) const;
*bc- *fg- *jk- *no- *rs- *vw- yz
size_t find_last_of (const char* s, size_t pos, size_t n) const;
*bc- *fg- *jk- *no- *rs- *vw- Yz
size_t find_last_of (char c, size_t pos = npos) const;
*bc- *fg- *jk- *no- *rs- *vw- Y2
(2)代码示例二:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
cout<<"size_t find_last_of (const string& str, size_t pos = string::npos) const;"<string str2("aeimqu");
size_t found1 = str1.find_last_of(str2);
if (found1 != string::npos)
{
str1[found1]='*';
//found1 = str1.find_last_of(str2, found1-1);
}
cout<cout<cout<<"size_t find_last_of (const char* s, size_t pos = string::npos) const;"<"dhlptxz");
if (found2 != string::npos)
{
str1[found2]='-';
//found2 = str1.find_last_of("dhlptx", found2-1);
}
cout<cout<cout<<"size_t find_last_of (const char* s, size_t pos, size_t n) const;"<"yz", string::npos, 1);
if (found3 != string::npos)
{
str1[found3]='Y';
//found3 = str1.find_last_of("yz", found3-1, 1);
}
cout<cout<cout<<"size_t find_last_of (char c, size_t pos = string::npos) const;"<'z');
if (found4 != string::npos)
{
str1[found4]='2';
//found4 = str1.find_last_of("z", found4-1);
}
cout<cout<"pause");
return 0;
}
=>size_t find_last_of (const string& str, size_t pos = string::npos) const;
abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcb*
size_t find_last_of (const char* s, size_t pos = string::npos) const;
abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe -cb*
size_t find_last_of (const char* s, size_t pos, size_t n) const;
abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*
size_t find_last_of (char c, size_t pos = string::npos) const;
abcd efgh ijkl mnop qrst uvwx yz 2Y xwvu tsrq ponm lkji hgfe -cb*
代码示例:
(1)示例代码一:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz");
cout<<"size_t find_last_not_of (const string& str, size_t pos = npos) const;"<string str2("bcd fgh jkl nop rst vwx yz *");
size_t found1 = str1.find_last_not_of(str2);
while (found1 != string::npos)
{
str1[found1]='*';
found1 = str1.find_last_not_of(str2, found1-1);
}
cout<cout<cout<<"size_t find_last_not_of (const char* s, size_t pos = npos) const;"<"abc efg ijk mno qrs uvw yz * -");
while (found2 != string::npos)
{
str1[found2]='-';
found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -", found2-1);
}
cout<cout<cout<<"size_t find_last_not_of (const char* s, size_t pos, size_t n) const;"<"abcd efgh ijkl mnop qrst uvwx z * - Y", string::npos, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
while (found3 != string::npos)
{
str1[found3]='Y';
found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", found3-1, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
}
cout<cout<cout<<"size_t find_last_not_of (char c, size_t pos = npos) const;"<'2');
if (found4 != string::npos)
{
str1[found4]='2';
//found4 = str1.find_last_not_of('2', found4-1);
}
cout<cout<"pause");
return 0;
}
=>size_t find_last_not_of (const string& str, size_t pos = npos) const;
*bcd *fgh *jkl *nop *rst *vwx yz
size_t find_last_not_of (const char* s, size_t pos = npos) const;
*bc- *fg- *jk- *no- *rs- *vw- yz
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
*bc- *fg- *jk- *no- *rs- *vw- Yz
size_t find_last_not_of (char c, size_t pos = npos) const;
*bc- *fg- *jk- *no- *rs- *vw- Y2
(2)示例代码二:
#include
#include
using namespace std;
int main ()
{
string str1("abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcba");
cout<<"size_t find_last_not_of (const string& str, size_t pos = npos) const;"<string str2("bcd fgh jkl nop rst vwx yz *");
size_t found1 = str1.find_last_not_of(str2);
if (found1 != string::npos)
{
str1[found1]='*';
//found1 = str1.find_last_not_of(str2, found1-1);
}
cout<cout<cout<<"size_t find_last_not_of (const char* s, size_t pos = npos) const;"<"abc efg ijk mno qrs uvw yz * -");
if (found2 != string::npos)
{
str1[found2]='-';
//found2 = str1.find_last_not_of("abc efg ijk mno qrs uvw yz * -", found2-1);
}
cout<cout<cout<<"size_t find_last_not_of (const char* s, size_t pos, size_t n) const;"<"abcd efgh ijkl mnop qrst uvwx z * - Y", string::npos, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
if (found3 != string::npos)
{
str1[found3]='Y';
//found3 = str1.find_last_not_of("abcd efgh ijkl mnop qrst uvwx z * - Y", found3-1, strlen("abcd efgh ijkl mnop qrst uvwx z * - Y"));
}
cout<cout<cout<<"size_t find_last_not_of (char c, size_t pos = npos) const;"<'a', 0);
if (found4 == string::npos)
{
str1[0]='A';
}
cout<cout<"pause");
return 0;
}
=>size_t find_last_not_of (const string& str, size_t pos = npos) const;
abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe dcb*
size_t find_last_not_of (const char* s, size_t pos = npos) const;
abcd efgh ijkl mnop qrst uvwx yz zy xwvu tsrq ponm lkji hgfe -cb*
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*
size_t find_last_not_of (char c, size_t pos = npos) const;
Abcd efgh ijkl mnop qrst uvwx yz zY xwvu tsrq ponm lkji hgfe -cb*
参考文献:
[1] 网络资源:
http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/find_first_not_of/
http://www.cplusplus.com/reference/string/string/rfind/
http://www.cplusplus.com/reference/string/string/find_last_of/
http://www.cplusplus.com/reference/string/string/find_last_not_of/