abseil tips #1 string_view #231 algo #232 auto
条评论#1 string_view
string_view 仅由一个指针和长度组成,标识一个不属于 string_view 所有且无法被视图修改的字符数据段。
注意:
- 生命周期
- 不一定是
NULL结尾
#231 Overlooked Algorithms
std::clamp:
- 返回的是引用
1
const int& dangling = std::clamp(1, 3, 4);
- 用
<或者cmp比较
std::midpoint std::lerp:
线性插值用。
#232 When to use auto
Use type deduction only if it makes the code clearer to readers who aren’t familiar with the project, or if it makes the code safer. Do not use it merely to avoid the inconvenience of writing an explicit type.
仅在以下情况下使用类型推导:对于不熟悉项目的读者来说,它能使代码更清晰;或者它能使代码更安全。不要仅仅为了避免编写显式类型的麻烦而使用它。
难以正确、高效描述类型
1 | for (const std::pair<std::string, DogBreed>& name_and_breed : |
这里的 Key 类型其实是 const std::string,这就导致了拷贝。
冗长类型影响阅读
当容器类型在附近可见时,直接用 auto 是不错的选择:
1 | std::vector<std::string>::iterator name_it = names_.begin(); |
如果容器类型在附近不可见,建议把迭代器或者元素类型体现出来:
1 | auto name_it = names_.begin(); // 不知道 names_ 装的是啥 |
std::make_unique<T> 就返回 std::unique_ptr<T>,没必要再重复写了。
1 | auto my_type = std::make_unique<MyFavoriteType>(...); |
总结
考虑性能,小心意外的拷贝;可读性是依赖上下文的。
本文标题:abseil tips #1 string_view #231 algo #232 auto
文章作者:Henry Wu
发布时间:2025-09-04
最后更新:2025-11-13
原始链接:https://henrywu.netlify.app/2025/09/04/abseil-tips/
版权声明:转载请注明出处。CC BY-NC-SA 4.0
