一个程序段两个小问题

来源:互联网 发布:js json格式化输出 编辑:程序博客网 时间:2024/05/17 00:11

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
}

原创粉丝点击