C++Primer 的string相关函数

10.8/9 这两天看的是关于string的一些函数,写在这里:
  1. 今天在小程序里用错了的substr()函数;
string s="hello world";
string s2 = s.substr(6, 5);// 6:子串的起始位置,5:子串的长度;
cout << s2 << endl;

不知道在C++中数字标示的起始位置,其第二个参数就是长度?而迭代器就是[begin,end) 这种区间模式;不知道是不是容器通用支持迭代器,只有vector, deque才支持下标模式,从目前接触到的容器来说;


2. find函数;

string s="hello world";
string subStr = "wor";
string::size_type pos = s.find(subStr);

find_first_of(); //子串中任意一个字符第一次出现的位置
find_first_not_of(); //不是子串中任意一个字符第一次出现的位置
find_last_of();  //顺序从右往左
find_last_not_of();//顺序从右往左
反向搜索:rfind()
string s="hello world, we love the world";
string subStr = "world";
string::size_type pos = s.rfind(subStr);

函数返回值: string::npos 比任意一个正常下标位置都大的一个值,标示没有找到


3. append(), replace();
string s="hello world";
string s2 = "everyone";
s.replace(6, 5, s2);  // 从下标6开始,删除5个字符,以s2中字符填补;

原来是world,被替换成了everyone;这是它有意思的地方;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值