如何返回类成员向量在c ++ 11 [英] How to return a class member vector in c++11
问题描述
我读了几个关于如何从一个方法返回向量的帖子,包括这些:
我仍然困惑于如何在VS2013中正确的方式传递一个向量和在这段代码中的以下方法之间的区别是什么(问题被标记在注释中):
class Foo {
private:
std :: vector< int& vect;
public:
// 1 - 经典的方式?
void GetVect(std :: vector< int>& v)
{
v = vect;
}
// 2 - RVO?
std :: vector< int> GetVect()
{
return vect;
}
// 3 - RVO with const?
std :: vector< int> GetVect()const
{
return vect;
}
// 4 - 只是移动?
std :: vector< int> GetVect()
{
return std :: move(vect);
}
// 5 - 用{}移动?
std :: vector< int> GetVect()
{
return {std :: move(vect)};
}
}
所以我不知道// 1是否显式形式// 2,不确定如果3工作。 4和5之间有什么区别?如何测试它是否RVO工作为向量在VS2013?
// 1 -
void GetVect(std :: vector< int>& v)
{
v = vect;
}
这只是简单的丑陋,你仍然需要一个副本,太复杂了。
// 2 - RVO?
不会改变你的类状态,所以
std :: vector< int> GetVect()
{
return vect;
}
// 3 - RVO with const?
std :: vector< int> GetVect()const
{
return vect;功能上相同,但你可能想要3表示<$ c
$ b $ c> getVectconst
语义可以正确应用。// 4 - 只是移动?
std :: vector< int> GetVect()
{
return std :: move(vect);
}
调用
GetVect
内部vect
将不再包含任何元素。code> // 5 - move with {}?
std :: vector< int> GetVect()
{
return {std :: move(vect)};
}
这应该和4一样,移动构造函数。
对于性能你可能实际想要的是:
const std :: vector< int>& getVect()const
{
return vect;
}
这种方式可以读取对象而不需要复制。如果要写入返回的向量,请明确创建副本。有关详情,请参阅此问题
I read a couple of posts on how to return a vector from a method include these ones:
and I'm still confused on how to pass a vector the right way in VS2013 and what are the differences between the following methods in this code(the questions are marked in the comments):
class Foo{ private: std::vector<int> vect; public: //1 - classic way? void GetVect(std::vector<int>& v) { v = vect;// assignment with swap? } //2 - RVO? std::vector<int> GetVect() { return vect; } //3 - RVO with const? std::vector<int> GetVect() const { return vect; } //4 - just move? std::vector<int> GetVect() { return std::move(vect); } //5 - move with {}? std::vector<int> GetVect() { return { std::move(vect) }; } }
So I m not sure if //1 is an explicit form of //2, not sure if 3 works. What are the differences between 4 and 5? How to test it if RVO works for vectors in VS2013?
解决方案//1 - classic way? void GetVect(std::vector<int>& v) { v = vect;// assignment with swap? }
This is just plain ugly, you still need a copy and you make your interface too complex.
//2 - RVO? std::vector<int> GetVect() { return vect; } //3 - RVO with const? std::vector<int> GetVect() const { return vect; }
Functionally the same, but you likely want 3 to indicate that
getVect
does not change your class state soconst
semantics can be applied correctly.//4 - just move? std::vector<int> GetVect() { return std::move(vect); }
It seems very unlikely you want this, after calling
GetVect
the internalvect
will no longer contain any elements.//5 - move with {}? std::vector<int> GetVect() { return { std::move(vect) }; }
This should end up being the same as 4, you just call the return object's move constructor more explicitly.
For performance what you might actually want is this:
const std::vector<int>& GetVect() const { return vect; }
This way you can read the object without the need for copying. If you want to write to the returned vector, create a copy explicitly. More details can be found in this question
这篇关于如何返回类成员向量在c ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!