C++11 将嵌套std::vector的元素复制到std::vector
我在将嵌套的std::vector的元素复制到另一个std::vector时遇到问题 例1C++11 将嵌套std::vector的元素复制到std::vector,c++11,vector,C++11,Vector,我在将嵌套的std::vector的元素复制到另一个std::vector时遇到问题 例1 std::vector<std::vector<int>> foo; std::vector<int> temp; std::vector<int> goo; temp.push_back(12345); foo.push_back(temp); goo = foo[0]; //error std::vector foo; std::向量温度; std:
std::vector<std::vector<int>> foo;
std::vector<int> temp;
std::vector<int> goo;
temp.push_back(12345);
foo.push_back(temp);
goo = foo[0]; //error
std::vector foo;
std::向量温度;
std::载体粘液;
温度推回(12345);
食物推回(温度);
goo=foo[0]//错误
例2
temp.clear();
for(int i = 0; i<foo[0].size(); i++) {temp.push_back(foo[0][i])};
goo = temp; //error
temp.clear();
对于(int i=0;i
这将确保contour\u struct
的大小正确。没有foo[0]
在您的代码中,因为您还没有在foo
中插入任何向量对象。@nwp,ya,对不起,我弄错了,我已经编辑了snippet@nwp,对不起,我的错误,已经编辑过了。谢谢第一个示例现在很好,不会导致错误。请检查found\u contour.size()
以确保它不是空的。@nwp,谢谢,但我的轮廓结构[i].contour=find\u contour[i];
,有什么想法吗?非常感谢您的回答
std::vector<std::vector<cv::Point>> found_contour;
struct Contours
{
std::vector<cv::Point> contour;
cv::RotatedRect minRect;
cv::RotatedRect minEllipse;
}
cv::findContours(result,found_contour,found_hierachy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cv::Point(0,0));
std::vector<Contours> contour_struct;
contour_struct.reserve(found_contour.size());
for (size_t i = 0; i < found_contour.size(); i++)
{
contour_struct[i].contour = found_contour[i];
contour_struct[i].minRect = cv::minAreaRect(cv::Mat(found_contour[i]));
}
cv::findContours(result,found_contour,found_hierachy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cv::Point(0,0));
std::vector<Contours> contour_struct;
contour_struct.reserve(found_contour.size()); //<-----problem
for (size_t i = 0; i < found_contour.size(); i++)
{
contour_struct[i].contour = found_contour[i];
contour_struct[i].minRect = cv::minAreaRect(cv::Mat(found_contour[i]));
}
contour_struct.resize(found_contour.size());