c++ - 如何将数据从 `boost::scoped_array`复制到 `std::vector`

标签 c++ boost stl std

vector<int> vec;
boost::scoped_array<int> scpaInts;

scpaInts.reset(new int[10]);

for (int i=0; i<10; i++)
    scpaInts[i] = i*2;

vec.assign(&scpaInts[0], &scpaInts[9]+1);      // => method one
vec.assign(scpaInts.get(), scpaInts.get()+10); // => method two

问题一> 我想出了两种方法。但我不确定它们是否正确,或者是否有更好的方法来做到这一点。

问题2> boost::scoped_array 不能获取有效长度是真的吗?

谢谢

最佳答案

问题一:两种方法都可以。指向数组元素的指针可以起到随机访问迭代器的作用。这个也不错

vec.assign(&scpaInts[0], &scpaInts[10]);

问题 2:这与您无法获取传递给函数的 C 样式数组的长度的原因相同。

关于c++ - 如何将数据从 `boost::scoped_array`复制到 `std::vector`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13089030/

相关文章:

c++ - 返回一个数组作为指针

c++ - 如何检查指针何时被删除?

c++ - 将窄 (char) 输入流重新解释为宽 (wchar_t) 流

c++ - std::begin 和 std::end 不能使用指针和引用,为什么?

c++ - 上下控件不显示其在伙伴窗口中的位置

c++ - 错误 C2062 : type 'void' unexpected in signal declaration in QT

c++ - 使用enable_if仅匹配具有特定静态数据成员且仅具有特定值的类

c++ - 如何从 boost 线程访问我的类实例?

c++ - C++ STL Map 在创建后是否会移动值的位置?

c++ - 我可以在 C++ 中使用具有值语义的多态容器吗?