#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
string str1 = "abc", str2 = "abc";
if ( strcmp( str1.c_str(), str2.c_str() ) == 0 )
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
string t1 = "helloWorld";
string t2 = "helloWorld";
int main(){
if (t1 == t2)
{
cout<<"***t1 ,t2 是一样的\n";
cout<<"这是正确的\n";
}
// error
if (t1.c_str() == t2.c_str())
{
cout<<"@@@t1 ,t2 是一样的\n";
}
// error
if (t1.c_str() == "helloWorld")
{
cout<<"===t1 ,t2 是一样的\n";
}
if (strcmp(t1.c_str(),t2.c_str()) == 0)
{
cout<<"###t1 ,t2 是一样的\n";
cout<<"这是正确的\n";
}
return 0;
}
输出:
***t1 ,t2 是一样的
这是正确的
###t1 ,t2 是一样的
这是正确的