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/