字符串比较strcmp(s1,s2)和*s1==*s2区别和c++比较函数区别

#include
#include
#include
using namespace std;
int main(){
	char a[]="1235";
	char b[]="123";
	if(*a==*b)cout<<"yes"<<endl;//5没算去比较空间 只能比较长度相同的 
	else cout<<"No"<<endl;
	
	if(strcmp(a,b)==0)cout<<"yes"<<endl;
	else cout<<"No"<<endl;
	
	string s1="1134";
	string s2="113";
	if(s1==s2)cout<<"yes"<<endl;//效率低 
	else cout<<"No"<<endl;
	
	if(s1.compare(s2)==0)cout<<"yes"<<endl;
	else cout<<"No"<<endl;
} 

结果
yes
No
No
No
总结:
C/C++字符比较一律用比较函数进行比较。不要用==号

你可能感兴趣的:(字符串比较strcmp(s1,s2)和*s1==*s2区别和c++比较函数区别)