find()函数的奇怪结果
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string name( "AnnaBelle" );
for( int i = 0; i < name.size(); ++ i )
{
cout << i << endl;
string::size_type pos = name.find( "Belle", 0, i );
if( pos == string::npos )
cout << "Belle not found!\n";
else
cout << "Belle found at pos: " << pos << endl;
}
return 0;
}
/*
// 输出结果
1
Belle found at pos: 4
2
Belle found at pos: 4
3
Belle found at pos: 4
4
Belle found at pos: 4
5
Belle found at pos: 4
6
Belle not found!
7
Belle not found!
8
Belle not found!
Press any key to continue
*/
这个结果完全和我想象的不同,为什么会有这样的结果?