- <string.h> 是C語言標准庫的頭文件之一,包含了一些字符串/內存處理相關的函數(如 strcpy,memcpy 等)。
- <cstring> 是C++語言標准庫的頭文件之一,基本上就是 <string.h> 的C++版本,當編寫C++程序時如果需要使用 <string.h>,則應當用 <cstring> 代替,並加上 std:: 前綴(如 std::strcpy,std::memcpy 等)。
- <string> 是C++語言標准庫的頭文件之一,主要包含了 std::basic_string 模板及其相關函數
即:
cstring,是兼容C的頭文件.里面的函數是基於C或者說C風格的字符串處理函數或定義或宏。
string是C++標准庫函數.提供在std名字空間中的C++字符串處理類string
<string>並非<cstring>的“升級版本”,他們是毫無關系的兩個頭文件。
鏈接:https://www.zhihu.com/question/274881112/answer/376939116
#include<cstdio>
#include <cstring>
//#include<string>
using namespace std;
int main(){
// string
char s[50];
scanf("%s",s);
printf("%s\n",(strcmp(s,"abc")) ? "!=" : "==");//strcmp函數是string compare(字符串比較)的縮寫,用於比較兩個字符串並根據比較結果返回整數。基本形式為strcmp(str1,str2),
//若str1=str2,則返回零;若str1<str2,則返回負數;若str1>str2,則返回正數。
}
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
s="adfaf";
string s2="adfafaf";
if(s==s2) cout<<1;
else cout<<0;
}