嵌入式面向对象编程·string类

一、string类的字符操作

1、string定义:可变长的字符串

2、string字符串初始化,示例:

  1. string s = “hello world”;
  2. sting s1(“hello world2”);
  3. string *ps = new string(“hello world3”);
  4. string s4 = “”;   //空字符串

3、string字符访问方式:使用[]或at函数

  1. cout << s1[3] << endl;      //输出字符l
  2. cout << s1.at(4) << endl;  //输出字符o

     二者区别:越界情况[]不会报出异常,at函数会产生异常。

二、string类的特性(属性)

1、特性(*重点)

  1. int capacity()const;   //返回当前容量(即string中不必增加内存即可存放的元素数) *
  2. int max_size()const; //返回string对象中可存放的最大字符串的长度 *
  3. int size()const;          //返回当前字符串的大小 *
  4. int length()const;       //返回当前字符串的长度
  5. bool empty()const;    //当前字符串是否为空 *
  6. void resize(int len,char c); //把字符串当前大小置为len,并用字符c填充不足的部分

2、长度length、大小size(占有内存空间)此处相等,均不包含”\0”,常用size

      :在C语言中strlen不包含”\0”而sizeof包含”\0”。

3、capacity规则:返回值总比它的size要大。

4、使用示例

  1. cout << s1.size() << endl; //前四种
  2. if(s4.empty() == true) {cout << “s4 is null\n”;} //第五种

三、string类的输入/输出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值