9.5 建议
[1] 使用std::string来获得字符序列;9.2节;[CG:SL.str.1]。
[2] 优先选择string操作而不是C风格字符串函数;9.1节。
[3] 使用string声明变量和成员,但不要将它作为基类;9.2节。
[4] 返回string应采用传值方式(依赖移动语义);9.2节、9.2.1节。
[5] 直接或间接使用substr()读取子字符串,使用replace()写入子字符串;9.2节。
[6] 如需要,我们可以扩展或收缩一个string;9.2节。
[7] 当需要范围检查时,应使用at()而不是迭代器或[];9.2节。
[8] 当需要优化速度时,应使用迭代器或[]而不是at();9.2节。
[9] string输入不会溢出;9.2节、10.3节。
[10] 只有迫不得已时,才使用c_str()获得一个string的C风格字符串表示;9.2节。
[11] 使用stringstream或通用的值提取函数(如to<X>)将字符串转换为数值;10.8节。
[12] 可用basic_string构造任意类型字符的字符串;9.2.1节。
[13] 对字符串字面值使用后缀s来指明是标准库string类型;9.3节;[CG:SL.str.12]。
[14] 如果函数需要读取以不同方式保存的字符序列,使用string_view作为其实参;9.3节;[CG:SL.str.2]。
[15] 如果函数需要写入以不同方式保存的字符序列,使用gsl::string_span作为其实参;9.3节;[CG:SL.str.2][CG:SL.str.11]。
[16] 将string_view看作一种附带大小的指针,它并不拥有字符;9.3节。
[17] 对字符串字面值使用后缀sv来指明是标准库string_view类型;9.3节。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access