--------------
我知道char是字符,string是字符串,既然在c++中string比char好用是不是完全把char替换掉? 另: #include<iostream> using namespace std; void main() { char ch[]="uname"; if(ch=="uname")cout<<"this's ok"<<endl; } //--------------- 上面这个程式用STRING定义判断就没问题,而用CHAR不行,难道CHAR只能进行单个字符判断,CHAR能不能进行多字符串判断?麻烦给出实例!!!谢谢大家.------------
C++不允许那样做的,根本就不支持直接比较,即使是单个的字符串判断也不行。 //反例 #include<iostream> using namespace std; void main() { char ch[]="u"; char ht[]="u"; if(ch==ht)cout<<"this's ok"<<endl; else cout<<"11"<<endl; } //必须如下 #include<iostream> #include<string> using namespace std; void main() { char ch[]="uname"; char ht[]="uname"; if(strcpy(ch,ht))cout<<"this's ok"<<endl; else cout<<"11"<<endl; } //或者直接用string函数 #include<iostream> #include<string> using namespace std; void main() { string ch="uname"; string ht="uname"; if(ch==ht)cout<<"this's ok"<<endl; else cout<<"11"<<endl; } //这里的CH与HT是任意类型的,可以直接赋值和比较,这样比上个例子方便的!!其实你的问题很正常,我以前也是总混淆的!!--------------------
<string.h> 是旧的C头文件,对应的是基于char*的字符串处理函数; <string> 是包装了std的C++头文件,对应的是新的strng类; <cstring> 是对应旧的C头文件的std版本。 如果使用后两种 要加上这一句 using namespace std;