Copy std::vector into std::array(将 std::vector 复制到 std::array)
本文介绍了将 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>?
推荐答案
使用std::copy_n代码>
std::array<T, N> arr;
std::copy_n(vec.begin(), N, arr.begin());
我没有注意到您也询问过移动元素.要移动,请将源迭代器包装在 std::move_iterator 中.p>
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:将 std::vector 复制到 std::array
基础教程推荐
猜你喜欢
- 用指数格式表示浮点数 1970-01-01
- 向量<unique_ptr<A>>使用初始化列表 2022-10-23
- C语言数组 1970-01-01
- 总计将在节日礼物上花多少钱 1970-01-01
- C语言3个整数的数组 1970-01-01
- C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
- 明确指定任何或所有枚举数的整数值 1970-01-01
- 对 STL 容器的安全并行只读访问 2022-10-25
- 迭代std :: bitset中真实位的有效方法? 2022-10-18
- C++多态 1970-01-01
