四、字符串的区间有效性
对串的索引访问在超过字符串的有效区间时,因为串的在实现上对内置的字符缓冲区执行下标访问,所以不会导致异常,但是将得到不可预知的结果,通常是不可用的。
将其他字符串作为右值输入时,对该串取出计数大于串大小时按串大小计算。
std::basic_string::size_type 的实际类型为 size_t,在 Visual C++ 7.1 中实现为 unsigned,std::basic_string::npos 被静态设定为
(basic_string<_Elem, _Traits, _Alloc>::size_type)(-1);
在查找子字符串等操作时,函数返回 npos 的值表示非法索引。
五、比较字符串
允许的比较对象
1)compare(s2) 其他同类型字符串
2)compare(p) C 风格字符串
3)compare(off, cnt, s2) [off, off + cnt) 同 s2 执行比较
4)compare(off, cnt, s2, off2, cnt2) [off, off + cnt) 同 s2 [off2, cnt2) 执行比较
5)compare(off, cnt, p) [off, off + cnt) 同 [p , <null>) 执行比较
6)compare(off, cnt, p, cnt2) [off, off + cnt) 同 [p, p + cnt2) 执行比较
返回 -1, 0, 1 作为小于、等于和大于的比较结果。
六、附加数据
1)使用 operator += 接受其他字符串,C 风格字符串和字符
2)使用 push_back() 在尾部附加字符,并使得通过字符串构造的 back_iterator 可以访问
3)append() 附加
1、append(s) 追加字符串
2、append(s, off, cnt) 追加字符串 s [off, off + cnt)
3、append(p) 追加字符串 [p, <null>)
4、append(p, cnt) 追加字符串 [p, p + cnt)
5、append(n, c) 填充 n * c
6、append(InF, InL) 追加输入流 [InF, InL)
4)insert() 插入
1、insert(off, s2) 插入字符串
2、insert(off, s2, off2, cnt2) 插入字符串 s [off2, off2 + cnt2)
3、insert(off, p) 插入字符串 [p, <null>)
4、insert(off, p, cnt) 插入字符串 [p, p + cnt)
5、insert(off, n, c) 插入 n * c
6、insert(iter) 元素默认值填充
7、insert(iter, c) 插入特定元素
8、insert(iter, n, c) 插入 n*c
9、insert(iter, InF, InL) 插入 [InF, InL)
5)operator +(a, b)
字符串关联运算符重载中支持 operator + 的形式
1、s + s
2、s + p
3、s + c
4、p + s
5、c + s
七、查找、替换和清除
1)find() 查找
1、find(c, off) 在 s [off, npos) 中查找 c
2、find(p, off, n) 在 s [off, npos) 中查找 [p, p + n)
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-34103-15.html
#吴亦凡#
老美不跑等着喂鱼吗