引用在 c++ 中总是更好的选择
Is reference always a better choice in c++
std::set<BigObject> set;
auto it = set.begin();
auto& it = set.begin();
同样地
std::string str("xxxxxxxxx");
std::string sub = str.substr(xx);
std::string& sub = str.substr(xx);
使用引用调用的时间分解器会减少吗?为什么呢?
当适用且您可以负担得起复制时,按值是最佳选择。
原因是值语义很容易理解(行为像int
(,并且比引用和指针更不容易出错。
器在处理指针和引用时很难,因为它们可以别名(指向相同的对象(,或者可以从其他线程引用这些对象,因此无法应用一些重要的优化。参见Chandler Carruth:优化C++的涌现结构了解更多细节。
相关文章:
- C++:将控制台输出存储在宏中更好吗
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 初始化具有非默认构造函数的std::数组项的更好方法
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 对于这个问题,是否有更好的数据结构和算法选择
- 对于树结构"widget"比"shared_ptr"更好的选择?
- C++/pimpl:原始指针还是unique_ptr?什么是更好的选择?
- 选择正确功能的更好方法
- 在 c++ 中选择有序映射的最后一个条目时,哪一个更好
- 引用在 c++ 中总是更好的选择
- 哪个是用于将整数除以 2 的更好选择
- 从 std::istream 读取时,有没有比 reinterpret_cast<char*>更好的选择?
- C++:引用std::map中的计数值;std::multimap是更好的选择吗
- 对于c++中的单个静态变量,有没有比类更好的选择
- 有没有更好的方法在C++中使用异步TCP套接字,而不是轮询或选择
- 对象的存储类型,选择什么更好
- 更好的可读性和简单性vs更高的复杂性和编程速度,选择什么
- 为什么单例是比全局对象更好的选择
- 有没有比这更好的方法从可变大小的数组中选择随机元素
- Delphi与C++Builder之比较——对于做Win32的Java程序员来说,这是更好的选择