将迭代器取消引用到 2D 向量并将其值分配给另一个 2D 向量
Derefrencing an iterator to 2D vector and assigning its value to another 2D vector
我有一个 2D 向量 vector<vector<int> > det;
,我想将最后一个值或最近 push_back
的值分配给另一个 2D 向量 v。
这就是我试过 v = *(det.rbegin());
但它显示错误。正确的做法是什么?
错误消息:请求从 'std::vector' 到非标量类型 'std::vector >' 的转换
您的二维数组的最后一个条目不是二维数组,即 vector < vector < int > > 类型,而是 vector < int > 类型。
因此它不能被分配给另一个类型的变量
vector < vector < int > >
这很好用:
#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
}
我有一个 2D 向量 vector<vector<int> > det;
,我想将最后一个值或最近 push_back
的值分配给另一个 2D 向量 v。
这就是我试过 v = *(det.rbegin());
但它显示错误。正确的做法是什么?
错误消息:请求从 'std::vector' 到非标量类型 'std::vector >' 的转换
您的二维数组的最后一个条目不是二维数组,即 vector < vector < int > > 类型,而是 vector < int > 类型。
因此它不能被分配给另一个类型的变量
vector < vector < int > >
这很好用:
#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
}