包装动态数组STL /升压容器? [英] Wrapping dynamic array into STL/Boost container?

查看:220
本文介绍了包装动态数组STL /升压容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要换一个动态分配的数组(从=新的双[100])成的std ::向量(preferably)不复制数组。
这种限制是由强加我想换行是从文件mmaped数组,所以只是做载体(A,A +大小)将增加一倍的内存使用情况。

I need to wrap a dynamically allocated array(from a = new double[100] for example) into std::vector(preferably) without copying the array. This restriction is imposed by that the array I want to wrap is mmaped from a file, so just doing vector(a, a+size) will double the memory usage.

时的任何技巧来做到这一点?

Is any tricks to do that?

推荐答案

这方面一个最好的解决方案是像的 STLSoft的array_proxy<> 模板。不幸的是,由doxygen的源$ C ​​$ C产生的文档页面是不是一大堆的帮助下认识的模板。该人士$ ​​C $ C实际上可能会好一点:

One of the best solutions for this is something like STLSoft's array_proxy<> template. Unfortunately, the doc page generated from the source code by doxygen isn't a whole lot of help understanding the template. The source code might actually be a bit better:

array_proxy&LT;&GT; 模板在马修很好描述威尔逊的书,不完美的C ++ 。我使用的版本是对STLSoft网站什么,所以我没有在全库拉一个删节版。我的版本不便携,但是这使得它比STLSoft(它通过了一大堆的便携箍跳转)。

The array_proxy<> template is described nicely in Matthew Wilson's book, Imperfect C++. The version I've used is a cut-down version of what's on the STLSoft site so I didn't have to pull in the whole library. My version's not as portable, but that makes it much simpler than what's on STLSoft (which jumps through a whole lot of portability hoops).

如果你设置了一个变量,像这样:

If you set up a variable like so:

int myArray[100];

array_proxy<int> myArrayProx( myArray);

变量 myArrayProx 有很多的STL接口 - 开始()结束()尺寸(),迭代器等。

The variable myArrayProx has many of the STL interfaces - begin(), end(), size(), iterators, etc.

因此​​,在许多方面, array_proxy&LT;&GT; 对象的行为就像一个载体(虽然的push_back()是不是有因为 array_proxy&LT;&GT; 不能生长 - 它不管理阵列的存储,它只是把它包装的东西更接近矢量一点)

So in many ways, the array_proxy<> object behaves just like a vector (though push_back() isn't there since the array_proxy<> can't grow - it doesn't manage the array's memory, it just wraps it in something a little closer to a vector).

有一个非常好的事情 array_proxy&LT;&GT; 是,如果你使用它们作为函数的参数类型,函数能够确定传入的数组,ISN的大小T本地阵列也是如此。和包装的阵列的大小不是模板的类型的一部分​​,因此它是相当灵活的使用。

One really nice thing with array_proxy<> is that if you use them as function parameter types, the function can determine the size of the array passed in, which isn't true of native arrays. And the size of the wrapped array isn't part of the template's type, so it's quite flexible to use.

这篇关于包装动态数组STL /升压容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆