您的位置:首页 > 编程语言 > C语言/C++

C++代码笔记(1)string搜索操作——find()

2017-03-18 23:16 525 查看
Reference Book: 《C++ Primer》

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

P327练习9.47:编写程序,查找string中的每个数字字符,然后查找其中每个字母字符。第一个用find_first_of,第二个用find_first_not_of。

#include <iostream>
#include <string>

using namespace std;

int main()
{
string numbers("0123456789"), name("ab2c3d7R4E6");
string::size_type pos = 0;

while((pos = name.find_first_of(numbers, pos)) != string::npos)
{
cout << "Found first number at index: " << pos
<< " element is " << name[pos] << endl;
++pos;
}
pos = 0;
while((pos = name.find_first_not_of(numbers, pos)) != string::npos)
{
cout << "Found first character at index: " << pos
<< " element is " << name[pos] << endl;
++pos;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: