一个程序段两个小问题

1,通过这个程序段记住string的erase、replace用法,erase删除迭代器处的元素同时更新size值,replace可以用指定个数的字符或者c字符串或者string对象来替换指定位置开始的指定个数的字符,第一个参数指定位置p0,第二个参数指定长度n0,第三个指定字符的个数n1,第四个指定字符ch,或者第三个指定string对象/c字符串。

2,×=和先乘后等计算顺序不同。

#include <iostream>
#include <string>
using namespace std;

void main()
{
 string str;
 str = "abcd";
 cout << str << ' ' << str.size() << endl;
 str.erase(str.end()-1);
 cout << str << ' ' << str.size() << endl;

 str.replace(0,4,"1234");
 cout << str << ' ' << str.size() << endl;

 str.replace(0,2,1,'A');
 cout << str << ' ' << str.size() << endl;

 str.replace(0,3,"3122",4);
 cout << str << ' ' << str.size() << endl;

 int a,b;
 a = b = 6;
 a = a * 3 / 9;
 b *= 3 / 9;
 cout << a << endl;//输出2
 cout << b << endl;//输出0
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值