C ++将向量向量的元素推入向量 [英] C++ pushing elements of a vector of vectors into a vector
本文介绍了C ++将向量向量的元素推入向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道是否有一个简单,快速是插入向量矢量内的矢量元素。例如:
std :: vector< std :: vector< double> > vals {
{1,2,3,4,5,6},
{1,2,3,4,5,6}
};
std :: vector< double> myvector;
因此 myvector
会包含: 1,2,3,4,5,6,1,2,3,4,5,6
I已尝试使用插入
和 push_back
,但没有运气。
编辑:
我已使用:
myvector.insert
解决方案 div>
试用
// c ++ 11
for(auto& v:vals ){
myvector.insert(myvector.end(),v.begin(),v.end());
}
//非c ++ 11
for(std :: vector< std :: vector< double>> :: const_iterator it = vals.begin ,end = vals.end(); it!= end; ++ it){
myvector.insert(myvector.end(),it-> begin(),it-> end
}
I'm just wondering whether there is a simple, quick was to insert the elements of a vector of vectors inside a vector. For example:
std::vector<std::vector<double> > vals {
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6}
};
std::vector<double> myvector;
so myvector
would then contain: 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6
I've tried using insert
and push_back
but had no luck.
Edit:
I have used:
myvector.insert(vals.begin(), vals.end());
- No matching member function to call to 'insert'
解决方案
Try
// c++11
for(auto && v : vals){
myvector.insert(myvector.end(), v.begin(), v.end());
}
// non c++11
for(std::vector<std::vector<double> >::const_iterator it = vals.begin(), end = vals.end(); it != end; ++it){
myvector.insert(myvector.end(), it->begin(), it->end());
}
这篇关于C ++将向量向量的元素推入向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文