关于string::find()的返回值
程序测试结果:当不存在匹配的字串的时候,一会返回-1,一会又是一个非常大的正整数。到底应该是什么呢?
MSDN里说:“it serves as either a very large value or as a special code.”
[解决办法]
find
Syntax:
#include <string>
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );
The function find() either:
* returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found,
* returns the first occurrence of str within the current string and within length characters, starting at index, string::npos if nothing is found,
* or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found.
For example:
string str1( "Alpha Beta Gamma Delta " );
string::size_type loc = str1.find( "Omega ", 0 );
if( loc != string::npos )
cout < < "Found Omega at " < < loc < < endl;
else
cout < < "Didn 't find Omega " < < endl;
[解决办法]
楼主,你自己没用string::size_type定义变量吧。
[解决办法]
string str1( "Alpha Beta Gamma Delta " );
string::size_type loc = str1.find( "Omega ", 0 );
if( loc != string::npos )
cout < < "Found Omega at " < < loc < < endl;
else
cout < < "Didn 't find Omega " < < endl;
[解决办法]
管它是多少呢,把它当成一个自定义的类型来使用。size_type可能是一个unsigned int。