简体   繁体   English

在课堂上传递矢量而不复制

[英]Passing vector in class without copying

I've got a class: 我上课了:

someclass header code: someclass标题代码:

class SomeClass{
    SomeClass();
    vector<int> somePointerVector;

    public:
    SomeClass(vector<int> &someVector);
};

Im a little confused about how to use vectors here. 我对这里如何使用矢量有点困惑。 I'd like to set somePointerVector = someVector, so that I can manipulate the data in someVector with other parts of the class using somePointerVector and I also don't want to copy all of someVectors data. 我想设置somePointerVector = someVector,这样我就可以使用somePointerVector操作someVector中的数据与类的其他部分,我也不想复制所有someVectors数据。

With an array I'd do it with: 使用数组我会用:

int* somePointerArray = someArray;

But how would I do this with vectors? 但是我如何用向量做到这一点?

Make somePointerVector a reference and put it in the initialization list of your class. somePointerVector作为引用并将其放在类的初始化列表中。 (And please give it a better name.) (请给它一个更好的名字。)

class SomeClass
{
    vector<int> &somePointerVector;

  public:
    SomeClass(vector<int> &someVector)
      : somePointerVector(someVector)
    {}
};

To understand the most-appropriate solution, you need to understand the lifetime and ownership of 'SomeClass' and the vector that it's passed. 要了解最合适的解决方案,您需要了解“SomeClass”的生命周期和所有权以及它传递的向量。 For example, if SomeClass takes the vector by reference, then the code that constructs SomeClass needs to make sure that the lifetime of the vector is greater than that of SomeClass - otherwise SomeClass could end-up with a reference that is no longer valid. 例如,如果SomeClass通过引用获取向量,那么构造SomeClass的代码需要确保向量的生命周期大于SomeClass的生命周期 - 否则SomeClass可能会以不再有效的引用结束。 By passing by reference, SomeClass isn't 'taking ownership' of the vector 通过引用传递,SomeClass不是“取得所有权”的向量

Alternatively, if the code that constructs SomeClass wants to 'hand-off' responsiblity of the vector (and you have a compiler and STL implementation that is 'r-value reference aware'), then you can pass an r-value reference to the vector; 或者,如果构造SomeClass的代码想要“切换”向量的责任(并且您有一个编译器和STL实现'r-value reference aware'),那么您可以将r值引用传递给向量;

class SomeClass { 
    vector<int> data_;

 public:
    SomeClass(vector<int>&& data) : data_(data) 
} 

The code that constructs SomeClass would then; 然后构造SomeClass的代码;

vector<int> data;
data.push_back(1);
data.push_back(2);
data.push_back(3);
data.push_back(4);

SomeClass someClass(std::move(data));

In this case, you've 'moved' the vector into SomeClass to avoid copying, and SomeClass has 'taken ownership' of the 'data'. 在这种情况下,您已经将向量“移动”到SomeClass中以避免复制,而SomeClass已经“获取”了“数据”的所有权。

How about 怎么样

class SomeClass {
    vector<int> myVector;

    void MyMethod(vector<int>* theVector);
}

and in your method 并在你的方法

void SomeClass::MyMethod(vector<int>* theVector)
{
    theVector->push_back(7);
}

Been ages since I worked with STL, but I think it's push_back? 自从我与STL合作以来已经很久了,但我认为这是推回?

Oh, and to use indices you could just (*theVector)[2]... 哦,并使用指数你可以(* theVector)[2] ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM