开发者

How understand data() method of boost::array, and return add length?

boost::array<ch开发者_如何学Pythonar,7>  buf = {'a','b','c','d','e','f','g'};
...
...
std::cout << buf.data() + 5;

It's display: fg

How to understand it?

buf.data() + 5

Thanks


buf.data() seems to return a pointer to the internal array buffer in question.

From there, standard pointer arithmetic applies, and you see the 6th character onwards in the std::cout.operator<< call.


buf.data() is defined to return a pointer to the first element of the array, and the elements in a Boost.Array are defined to be contiguous.

So buf.data() + 5 will be a pointer to the element (in this case, character) of the array. You could also write &buf[5] and get the same pointer.

Note that in the code above:

std::cout << buf.data() + 5;

you are attempting to print the value of the pointer, not the character it points to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜