首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

string部类用法中有类似C语言里strstr的用法吗

2013-06-25 
string类型用法中有类似C语言里strstr的用法吗?最近写一个电话本作业,需要用到查找,当时想用strstr来判断

string类型用法中有类似C语言里strstr的用法吗?
最近写一个电话本作业,需要用到查找,当时想用strstr来判断是否是其子字符串,但是strstr只能作用于char,请问string类型里有类似的用法吗?
[解决办法]
把它变成c语言的字符串,再用strstr.
const char* ptr = string.c_str();
str = strstr(ptr)
[解决办法]
http://www.cplusplus.com/reference/string/string/find/
[解决办法]
string有find之类的方法搜索子串。
[解决办法]
查找(find)
语法: 
  size_type find( const basic_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 );

 

find()函数: 

返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos, 
返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos, 
返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos 
例如, 

    string str1( "Alpha Beta Gamma Delta" );
    unsigned int loc = str1.find( "Omega", 0 );
    if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
    else
      cout << "Didn't find Omega" << endl;
    


[解决办法]
用find做个函数让他像strstr一样即可

热点排行