如何返回向量的常量引用?
How to return const reference of a vector?
我有以下设置:
啊
class A
{
public:
...
const std::vector<int>& getSomeData() const;
private:
std::map<SomeIdType, std::shared_ptr<SomeDataType> > m_someDataCollection;
}
答.cpp
const std::vector<int>& getSomeData(SomeIdType id) const
{
std::vector<int> rVal;
auto it = m_someDataCollection.find(id);
if (it != m_someDataCollection.end())
{
std::shared_ptr<SomeDataType> someData = it->second;
rVal = someData->getSomeDataVector(); // returns std::vector<int>
}
return rVal;
}
int main()
{
SomeIdType id;
A a;
...
const std::vector<int>& data = a.getSomeData(id);
}
有一个对A.cpp中返回的局部变量的引用。
我不希望A::getSomeData()
返回的向量在外面纵。
如何实现此目标,而不必返回对A.cpp中局部变量的引用?
如何返回向量的常量引用?
- 通过在函数以外的某个位置创建向量。由于在本例中它是一个成员函数,因此可以选择返回对成员的引用。或者,向量可以通过参数传递到函数中。
- 或者使用静态局部变量。在这种情况下,函数的所有入侵都将返回相同的向量。
任何非静态局部变量都不希望 A::getSomeData(( 返回的向量在外部作.e 具有自动存储,因此仅在函数结束之前存在。返回的对此类变量的引用将引用已销毁的向量。
也就是说,从您的示例中可以看出,您可能应该按值返回向量,而不是尝试返回引用。
我不希望 A::getSomeData(( 返回的向量在外面纵。
你还没有给出你想要那个的理由。你为什么要关心调用方如何处理你返回的向量?
我假设当在数据中找到 id 时,您希望返回对该 id 向量的引用,而不是复制整个向量。
对于空情况,您可以返回对始终为空的static
局部变量的引用:
const std::vector<int>& getSomeData(SomeIdType id) const
{
auto it = m_someDataCollection.find(id);
if (it != m_someDataCollection.end())
{
return it->second->getSomeDataVector(); // returns const std::vector<int>&
}
else
{
static const std::vector<int> emptyVector;
return emptyVector;
}
}
将值转换为int
后,它们不再引用原始数据,因此调用方无法对其进行修改。按值返回向量是安全的。如果向量包含指针或引用,则不安全。
std::vector<int> getSomeData(SomeIdType id) const
{
auto it = m_someDataCollection.find(id);
if (it != m_someDataCollection.end())
{
std::shared_ptr<SomeDataType> someData = it->second;
return someData->getSomeDataVector(); // returns std::vector<int>
}
return {};
}
const auto data = a.getSomeData(id);
如果您使用的是C++17,其中一种可能性也是使用std::optional
。如果您找到了具有指定 ID 的元素,您将返回std::vector
.否则,您将返回std::nullopt
.在这种情况下,您的代码如下所示:
std::optional<std::vector<int>> getSomeData(SomeIdType id) const {
auto it = m_someDataCollection.find(id);
if (it != m_someDataCollection.end()) {
return it->second->getSomeDataVector();
}
else {
return {}; // or return std::nullopt;
}
}
然后你可以像这样使用它:
int main() {
SomeIdType id;
A a;
// ...
auto data = a.getSomeData(id);
if (data) {
// there is an item with the specified ID
// example of accessing the std::vector
for (auto const i : data.value().get()) {
// more code here ....
}
} else {
// there is NO item with the specified ID
}
}
相关文章:
- 什么时候在C++中返回常量引用是个好主意
- 通过常量引用传递参数的矩阵模板类
- 在C++中使用非常量引用作为常量
- 具有常量引用参数的函数模板专用化
- 多个"常量引用"变量可以共享同一个内存吗?
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 为什么常量方法可以采用非常量引用?
- 为什么当我们有常量引用时创建临时对象?
- 如何返回向量的常量引用?
- C++:常量引用参数
- 不同于按值传递和常量引用传递的程序集
- 为什么const_cast和static_cast常量引用没有效果?
- C++ 获取函数在常量引用中按值返回的结果
- 从 BubbleSort* 类型的右值初始化 'AssortedSorter&' 类型的非常量引用无效"
- C++ 在类中使用常量引用文本时 O2 内存泄漏
- 是否可以跨 dll 边界返回常量引用/指向 std::vectors?
- C++中大多数/所有 setter 函数的参数是否应该写为常量引用?
- 通过非常量引用参数修改常量引用参数
- 将常量引用传递给线程
- 为什么C++中没有常量引用,就像常量指针一样?