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

string 比较有关问题

2012-02-12 
string 比较问题?C++有没有像javastring.IndexOf(string)这样的功能?能提供算法也行。例如:s1 111 s2

string 比较问题?
C++有没有像java   string.IndexOf(string)这样的功能?能提供算法也行。
例如:
s1= "111 ";
s2= "110 ";
s3= "101 ";
s4= "121 ";
s5= "211 ";
sc= "11 ";
通过对sc进行比较返回s1和s2


[解决办法]
#include <vector>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
vector <string> s;
s.push_back( "111 ");
s.push_back( "110 ");
s.push_back( "101 ");
s.push_back( "121 ");
s.push_back( "211 ");

string sc= "11 ";
vector <string> ::iterator it;
for(it = s.begin(); it != s.end(); ++it)
{
if(0 == (*it).find(sc.c_str(), 0, sc.length()))
cout < < *it < < endl;
}
system( "PAUSE ");
return 0;
}

热点排行