我可以使用insert函数将元素添加到向量的向量中吗?

| 我想知道,我可以使用insert关键字将元素添加到
vector
vector
中吗? 例如,对于这个
vector
,我有一个
vector
vector<int> temp1;
,元素在内部
for
循环内添加。在
for
循环的结尾,我需要将元素添加到
temp1
vector<vector<int> >temp1;
for (int a;a<size(),a++){//...
  vector<int> temp2;
  for (int b=0;b<closer_points;b++){

       // some steps here...

       vector<int> pt_no=mydata.Find_End_point(my_list,s1,s2);
       temp2=pt_no;
       }
  temp1[a].insert(temp1[a].end(),temp2.begin(),temp2.end());
  }
然后,我尝试逐行打印
temp1
,因为
temp2
的元素出现在一行中。
for(int i=0;i<temp1.size();i++){
    for(int j=0;j<temp1[i].size();j++){
        cout<<\" t2 \"<<temp1[i][j];
        }
    cout<<endl;
    }
但是,这不起作用。任何人都可以纠正这一点,请...     
已邀请:
题:   temp1 [a] .insert(temp1 [a] .end(),temp2.​​begin(),temp2.​​end()); 恢复先前的答案: 要将值范围附加到现有向量:
std::copy(temp2.begin(),temp2.end(), std::back_inserter(temp1));
要将范围作为新向量附加到现有的vector_of_vectors中:
vector<vector<int> > vecvec;
vector<int> toappend;

// some steps :)
vecvec.push_back(toappend);

// or
vecvec.push_back(vector<int>(toappend.begin(), toappend.end());
如果您还没有14ѭ和
<algorithm>
更新资料   如果我这样说;我有一些要点。我想将这些点归为向量,然后将其放在另一个向量中。因此,我猜我的最终输出是向量的向量。例如,最终结果应类似于((1,3),(4,6,9,8,21),(5,7,12),..)。 好的,这里是:
// (
std::vector<std::vector<int> > vecvec;
// (1,3)
{
    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(3);

    vecvec.push_back(vec);
}
// (4,6,9,8,21)
{
    std::vector<int> vec;
    vec.push_back(4);
    vec.push_back(6);
    vec.push_back(9);
    vec.push_back(8);
    vec.push_back(21);

    vecvec.push_back(vec);
}
// (5,7,12)
{
    std::vector<int> vec;
    vec.push_back(5);
    vec.push_back(7);
    vec.push_back(12);

    vecvec.push_back(vec);
}
// )
//  ....
... // (use vecvec)
    
您需要使用
temp1.push_back(temp2);
    

要回复问题请先登录注册