我的函数应该返回一个指针std :: vector,还是std :: vector的引用? [英] Should my function return a pointer to std::vector, or a reference to std::vector?

查看:853
本文介绍了我的函数应该返回一个指针std :: vector,还是std :: vector的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: map< int,std :: vector< SomeStruct>>

,并提供 std :: vector< SomeStruct> FindData(int key)



为了防止复制整个数据,我将其修改为 std :: vector< SomeStruct>& FindData(int key)

但是,对于某些没有数据,返回。

在这种情况下,我声明一个为空的 std :: vector< SomeStruct> 的文件作用域变量并返回。 >

但是如果我选择向量的指针,那就是 std :: vector< SomeStruct> * FindData(int key)我可以为不存在的键返回 NULL



哪一个更好?

我知道指向 std :: vector 的指针在问题中是错误的(或奇怪的?不确定) a href =http://stackoverflow.com/questions/15703842/is-there-other-syntax-for-this-pointer-operation/15703927?noredirect=1#comment22312516_15703927>这个指针操作有其他语法吗? )

我也喜欢引用 std :: vector ,以便我可以使用 operator [ ] 更容易,但缺点是我必须为它声明一个额外的空变量。



代码示例如下:In SomeClass.h

  typedef std :: vector< SomeStruct> DataVec; 
typedef std :: map< int,DataVec> DataMap;
DataMap m_DataMap;

现在在 SomeClass.cpp

案例1:

 命名空间
{
DataVec EmptyVector;
}

DataVec& FindDatas(int key)
{
DataMap :: iterator It = m_DataMap.find(key);

if(It == m_DataMap.end())return EmptyVec;

return It-> second;案例2:



<$ p









  DataVec * FindDatas(int key)
{
DataMap :: iterator It = m_DataMap.find(key);

if(It == m_DataMap.end())return NULL;

return&(It-> second);
}

参考:

优点: c $ c> std :: vector


缺点:声明了附加变量。



指针:

优点:更短的查询函数,不需要其他变量。

缺点:看起来很奇怪(?!),你不能juse p [i] ,你必须(* p)[i] ,这很烦人。



更好?

解决方案

这取决于您的设计要求。如果使用没有相应元素的索引调用此函数是编程错误,则代码应该中止。如果是用户错误,它应该抛出异常。如果它是预期使用的一部分,那么你有三个选择,再次取决于你的设计。您可以标记问题,通常通过返回一个空指针或返回一个布尔值从一个函数,为结果的引用。你可以静默地返回一个新创建的有效对象,如 std :: set 。您可以返回不是容器一部分的sentinel对象,用户必须在使用返回值之前检查这些对象是否是它们。


I have a std::map<int, std::vector<SomeStruct>>,
and provide a query like std::vector<SomeStruct> FindData(int key).

To prevent copying the whole data, I modify it to be std::vector<SomeStruct>& FindData(int key).
But, there will be no data for certain key, so sometimes I have nothing to return.
In that case, I declare a file scope variable that is an empty std::vector<SomeStruct> and return it.

But if I choose the pointer to vector, that is std::vector<SomeStruct>* FindData(int key) then I can just return NULL for non-existing key.

Which one is better?
I learned that pointer to std::vector is bad(or weird? not sure) in the question (Is there other syntax for this pointer operation?)
And I personally like reference to std::vector too, so that I can use operator[] easier, but the drawback is I have to declare an additional empty variable for it.

Code example are like: In SomeClass.h

typedef std::vector<SomeStruct> DataVec;
typedef std::map<int, DataVec> DataMap;
DataMap m_DataMap;

Now In SomeClass.cpp:

Case 1:

namespace
{
    DataVec EmptyVector;
}

DataVec& FindDatas(int key)
{
    DataMap::iterator It = m_DataMap.find(key);

    if (It == m_DataMap.end()) return EmptyVec;

    return It->second;
}

Case 2:

DataVec* FindDatas(int key)
{
    DataMap::iterator It = m_DataMap.find(key);

    if (It == m_DataMap.end()) return NULL;

    return &(It->second);
}

Reference:
Pros: looks like normal std::vector.
Cons: Additional variable declared.

Pointer:
Pros: Shorter query function and no need other variable.
Cons: looks weird(?!), and you can't juse p[i], you have to (*p)[i], which is annoying.

Which one is better?

解决方案

It depends on your design requirements. If calling this function with an index that doesn't have a corresponding element is a programming error, then the code should abort. If it's a user error, it should throw an exception. If it's part of the expected usage, then you have three alternatives, again depending on your design. You can flag the problem, typically by returning a null pointer or returning a Boolean value from a function that takes a reference for the result. You can quietly return a newly created valid object, as std::set does. You can return a sentinel object that isn't part of your container, and users will have to check whether that's what they got before they use the returned value.

这篇关于我的函数应该返回一个指针std :: vector,还是std :: vector的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆