string是类,是可变长的字符序列,是容器。
char是字符,是内置类型。
大小比较:
string:


1 #include <iostream> 2 #include <string> 3 using namespace std; 4 int main() 5 { 6 string str1 = "A string example"; 7 string str2 = "A different string"; 8 if (str1 < str2) 9 cout << "str1 is smaller than str2" << endl; 10 else if (str1 > str2) 11 cout << "str1 is bigger than str2" << endl; 12 else 13 cout << "str1 is equal to str2" << endl; 14 system("pause"); 15 return 0; 16 }
char:


1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 int main() 7 { 8 const char ca1[] = "A string exmaple"; 9 const char ca2[] = "A different string"; 10 if (strcmp(ca1, ca2) < 0) 11 cout << "ca1 is smaller than ca2" << endl; 12 else if (strcmp(ca1, ca2) == 0) 13 cout << "ca1 is equal to ca2" << endl; 14 else 15 cout << "ca1 is bigger than ca2" << endl; 16 system("pause"); 17 return 0; 18 }