关于字串符的问题
相比较两个字串符中的值是否相同,写了个用for循环然后比较每个字串符中元素的程序 但是不对,麻烦给看一下,另外想问一个问题是vector<string> i 和string i 这两个对象从实质上来说有什么区别么,还有能不能把string对象看作是字符数组?谢谢啦
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2;
cout<<"Please enter words for s1:"<<endl;
getline(cin,s1);
cout<<"Please enter words for s2:"<<endl;
getline(cin,s2);
if (s1.size()==s2.size())
{
for ( i=0;i!=s1.size();++i)
{
if (s1[i]!=s2[i])
{
cout<<"s1 and s2 are different."<<endl;
}
}
cout<<"s1 and s2 are same."<<endl;
}
else
cout<<"s1 and s2 are different."<<endl;
return 0;
}
if (s1.size()==s2.size())
{
for ( i=0;(i!=s1.size()) && (s1[i] == s2[i]);++i)
//加一个循环退出条件,找到第一个不相等的字符
//其实非计数循环应该改用while实现,我是在你的代码上改,就还用for
{
}
if( i == s1.size() )//未找到不相等的字符
{
cout<<"s1 and s2 are same."<<endl;
}
else
{
cout<<"s1 and s2 are diffrent."<<endl;
}
}
else
cout<<"s1 and s2 are different."<<endl;