C++基础-string类型-操作符

  • 操作符
  ==	// 关系操作符,从两个字符串的首字符根据字符的ASCII值开始比较
  >
  <
  >=
  <=
  !=
  + 	//s1 + s2,把 s1 和 s2 连接成一个新字符串, 返回新生成的字符串
  +=	// s1 += s2, 把 s2追加到s1中
  []	// str[n], 返回 str 中位置为 n 的字符,位置从 0 开始计数
  • 举例
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main(void)
{
        string str1("Hello, ");
        string str2 = "world!";

        string str3 = str1 + str2;

        string str4("Hello, world!");

        cout << "str1: " << str1 << endl;
        cout << "str5[1]: " << str5[1] << endl;
        if(str1 > str2)

        cout << "str2: " << str2 << endl;


        cout << "str3: " << str3 << endl;

        cout << "str4: " << str4 << endl;

        if(str3 == str4)
        {
                cout << "str3 == str4" << endl;
        }

        string str5(str1);
        str5 += str2;

        cout << "str5: " << str5 << endl;

        cout << "str5[1]: " << str5[1] << endl;
        
        if(str1 > str2)
        {
                cout << str1 << "大于" << str2 << endl;
        }
        else if(str1 == str2)
        {
                cout << str1 << "等于" << str2 << endl;
        }
        else
        {
                cout << str1 << "小于" << str2 << endl;
        }
        
        return 0;
}
PC:~/WorkSpace/CPP/string/3.2.3$ ./str_operators.exe 
str1: Hello, 
str2: world!
str3: Hello, world!
str4: Hello, world!
str3 == str4
str5: Hello, world!
str5[1]: e
Hello, 小于world!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值