如何将Boost数组表示为类型的指针? [英] How to represent boost array as a pointer of a type?
问题描述
我将boost数组用作缓冲区,并希望将其作为参数传递给函数.我想要传递的方式是将大小确定为另一个参数,以便我可以传递大小不同的缓冲区.目前,我的代码如下:
I'm using a boost array as a buffer and wanted to pass it on as an argument to a function. The way I would like to pass it is so that the size is determined as another argument so that I can pass over buffers of varying sizes. Currently, my code is as follows:
void DataTransform(boost::array<char, 1024> data) {
//do something
}
使用普通数组我可以使用:
With a normal array I could have just used:
void DataTransform(char* data, uint_16 size) {
//do something
}
我将如何解开boost数组以将其表示为指针?
How would I go about unwrapping the boost array to represent it as a pointer?
推荐答案
boost::array
具有data()
方法,它返回数组中的元素数:
boost::array
has a data()
method that returns a pointer to the start of the array, and a size()
method that returns the number of elements in the array:
DataTransform(some_array.data(), some_array.size())
另一种选择是对DataTransform方法进行重做以在迭代器上工作:
Another option is to rework your DataTransform method to work on iterators:
template<typename T, typename U>
DataTransform(T begin, U end) {
for(; begin != end; ++begin) {
// do something with *begin
}
}
然后您可以这样称呼它:
And then you can call it like so:
DataTransform(std::begin(some_array), std::end(some_array))
DataTransform(some_c_array, some_c_array + some_c_array_size)
DataTransform(std::begin(some_list), std::end(some_list))
这篇关于如何将Boost数组表示为类型的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!