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

q09*_*987 7 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
Run Code Online (Sandbox Code Playgroud)

问题1>我找到了两种方法.但我不确定它们是否正确或有更好的方法来做到这一点.

问题2>我们无法从boost :: scoped_array获取有效长度吗?

谢谢

And*_*rey 3

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

vec.assign(&scpaInts[0], &scpaInts[10]);
Run Code Online (Sandbox Code Playgroud)

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

  • @q0987:http://stackoverflow.com/q/7346634/560648 http://stackoverflow.com/q/988158/560648 (2认同)