C++ Primer-string和char

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 }
View Code

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 }
View Code

 

转载于:https://www.cnblogs.com/archerzon/p/9613850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值