简体   繁体   English

推向量 <double> 到矢量 <vector<double> &gt;

[英]Pushing vector<double> onto vector<vector<double>>

So, I have this situation: 所以,我有这种情况:

vector<vector<double>> myVector; 
myVector.resize(somePreviousObject.size());
for(int i = 0; i < myVector.size(); i++)
{
    vector<double> tempVector;
    //Do some stuff that fills tempVector, in a loop
    //After loop:
    myVector[i].push_back(tempVector);
}

However, this produces a compile error, stating that: 但是,这会产生一个编译错误,指出:

no matching function for call to 'std:;vector<double, std::allocator<double > >::push_back(std::vector<double, std::allocator<double> >&)'
...stl_vector.h:733 note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = double, _Alloc = std::allocator<double>]

I realize that using a vector of vectors is not performance-friendly, but it still should compile and execute, correct? 我意识到使用向量载体并不友好,但是仍然应该编译并执行,对吗? Why doesn't it? 为什么不呢?

Thanks for your help. 谢谢你的帮助。

In the expression: 在表达式中:

myVector[i].push_back(tempVector);

The subexpression myVector is a std::vector<std::vector<double>> , myVector[i] is a std::vector<double>& , and push_back on that vector requires a double , not a std::vector<double> . 子表达式myVectorstd::vector<std::vector<double>>myVector[i]std::vector<double>& ,该向量上的push_back需要double ,而不是std::vector<double>

It looks like you want to set the i -th element to be tempVector , if that is the case you should do (on the assumption that you don't need tempVector after this statement): 看来您想将第i个元素设置为tempVector ,如果是这种情况,您应该这样做(假设您在此语句之后不需要tempVector ):

myVector[i].swap(tempVector);

(While myVector[i] = tempVector; will have the same net effect, it will be more expensive as it will allocate and copy all of the contents. The swap will move the contents of tempVector into myVector[i] at almost no cost (three pointer swaps)) (虽然myVector[i] = tempVector;将具有相同的净效果,这将是更昂贵的,因为它会分配和复制所有的内容的。 swap会的内容移动 tempVectormyVector[i]在几乎没有成本(三个指针交换))

I know someone will come along and suggest using myVector[i] = std::move(tempVector) , which is also fine in C++11, but not available in C++03. 我知道有人会提出建议使用myVector[i] = std::move(tempVector) ,这在C ++ 11中也很好,但在C ++ 03中不可用。 The difference between the two is that in the case of swap the original contents of myVector[i] are guaranteed to be inside tempVector after the operation (in this case myVector[i] was empty, so it won't matter). 两者之间的区别在于,在swap之后, tempVector myVector[i]的原始内容保证在操作后位于tempVector内部(在这种情况下, myVector[i]为空,所以没有关系)。 On the other hand, the state of tempVector after myVector[i] = std::move(tempVector); 另一方面, tempVector myVector[i] = std::move(tempVector);之后的tempVector状态myVector[i] = std::move(tempVector); is undefined in the standard, it could leave the vector empty, or it could swap it or... the only guarantee is that the tempVector object can be destroyed after the move. 在标准中是未定义的,它可以将向量留空,或者可以将其交换或......唯一的保证是tempVector对象可以在移动后销毁。

It's either 要么

myVector[i] = tempVector;

or 要么

myVector.push_back(tempVector);

Since you resized your vector beforehand, I'm assuming you want the first version. 由于您预先调整了向量的大小,因此我假设您需要第一个版本。

In your code, remember that myVector[i] is itself a vector<double> , that's why it doesn't work. 在您的代码中,请记住myVector[i]本身就是vector<double> ,这就是为什么它不起作用的原因。

vector<vector<double>> myVector;  

should be: 应该:

vector<vector<double> > myVector;  

and then 接着

myVector[i].push_back(tempVector); 

should be 应该

myVector.push_back(tempVector); 

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

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