我有一个2D矢量vector<vector<int> > det;
,我想将最后一个值或最近的push_back
&#39; d值分配给另一个2D矢量v。这就是我尝试v = *(det.rbegin());
,但它显示错误。这样做的正确方法是什么?
错误信息:转换为&#39; std :: vector&#39;到非标量类型&#st; :: vector&gt;&#39;请求的
答案 0 :(得分:1)
2D阵列的最后一个条目不是2D数组,即类型为vector&lt;矢量&lt; int&gt; &gt;,但是类型为矢量&lt; int&gt;
因此无法将其分配给
类型的另一个变量vector&lt;矢量&lt; int&gt; &GT;
这很好用:
#include<iostream>
#include<vector>
int main(){
std::vector<std::vector<int> > det, det2;
std::vector<int> vector1, vector2, vector3;
vector1.push_back(3);
vector1.push_back(4);
vector1.push_back(5);
vector2.push_back(13);
vector2.push_back(14);
vector2.push_back(15);
vector3.push_back(113);
vector3.push_back(114);
vector3.push_back(115);
std::vector<int> member_of_2darray;
det.push_back(vector1);
det.push_back(vector2);
det.push_back(vector3);
det2.push_back(det.back());
std::cout<<det2[0][2]<<std::endl; // returns 115
//last:element
std::cout<<det.back().back()<<std::endl;
}
编辑:如果你不想使用迭代器:这是一个有效的例子:
#include<iostream>
#include<vector>
int main(){
std::vector<std::vector<int> > det, det2;
std::vector<int> vector1, vector2, vector3;
std::vector<std::vector<int> >::iterator it;
vector1.push_back(3);
vector1.push_back(4);
vector1.push_back(5);
vector2.push_back(13);
vector2.push_back(14);
vector2.push_back(15);
vector3.push_back(113);
vector3.push_back(114);
vector3.push_back(115);
std::vector<int> member_of_2darray;
det.push_back(vector1);
det.push_back(vector2);
det.push_back(vector3);
it = det.end();
det2.push_back(*(it-1));
std::cout<<det2[0][2]<<std::endl; //output: 115
}