任何替代std :: dynarray目前可用? [英] Any alternative to std::dynarray presently available?
问题描述
C ++ 11给了我们很大的 std :: array
,这需要在编译时知道大小:
C++11 gave us great std::array
, which requires size to be known at compile time:
std::array<int, 3> myarray = {1, 2, 3};
现在,我碰巧有一些旧 short * $ c $
Now, I happen to have some old short*
buffers to wrap, whose size will be known (and it will be, of course) at runtime only.
C ++ 14将会定义 std :: dynarray
来覆盖这种情况,但是 dynarray
在GCC 4.7或者Clang 3.2中还不可用。
C++14 will define std::dynarray
to cover this case, but dynarray
is not available yet in GCC 4.7 nor in Clang 3.2.
所以,有没有人知道一个容器可以比较 std :: array
(在效率方面),但不需要指定大小在编译时?
So, does anyone know a container which is comparable to std::array
(in terms of efficiency) but does not require to specify size at compile time? I suspect Boost has something ready for me, although I couldn't find anything.
推荐答案
你可以(ab)使用一个 std :: valarray< short>
。
You could (ab)use a std::valarray<short>
.
int main() {
short* raw_array = (short*) malloc(12 * sizeof(short));
size_t length = 12;
for (size_t i = 0; i < length; ++ i) {
raw_array[i] = (short) i;
}
// ...
std::valarray<short> dyn_array (raw_array, length);
for (short elem : dyn_array) {
std::cout << elem << std::endl;
}
// ...
free(raw_array);
}
dynarray
的功能:
- 分配器
- 反向迭代器
-
.at()
> .data()
- allocator
- reverse iterator
.at()
.data()
请注意,标准(从n3690开始)不需要 valarray
存储是连续的,虽然没有理由不这样做)。
Note that the standard (as of n3690) does not require valarray
storage be continuous, although there's no reason not to do so :).
(对于一些实现细节,在libstdc ++实现为(长度,数据)对,在libc ++中实现为(begin,end)。)
(For some implementation detail, in libstdc++ it is implemented as a (length, data) pair, and in libc++ it is implemented as (begin, end).)
这篇关于任何替代std :: dynarray目前可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!