本文介绍了将std :: vector复制到std :: array中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 std :: vector< T>
的第一个 n
C ++ 11 std :: array< T,n>
?
How do I copy or move the first n
elements of a std::vector<T>
into a C++11 std::array<T, n>
?
推荐答案
p>使用 p>
Use std::copy_n
std::array<T, N> arr;
std::copy_n(vec.begin(), N, arr.begin());
编辑:我没有注意到你问过移动元素。要移动,请将来源迭代器包装在。
I didn't notice that you'd asked about moving the elements as well. To move, wrap the source iterator in std::move_iterator
.
std::copy_n(std::make_move_iterator(v.begin()), N, arr.begin());
这篇关于将std :: vector复制到std :: array中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!