数组与矢量帮助 [英] Arrays vs Vector Help
问题描述
我有一个使用大量内存的程序。
目前我在std :: vector中存储了很多指向对象的指针。
(数百万)
我有三个问题:
1)让我们说平均Vector的大小为2。我可以通过将指针存储在c ++数组而不是向量中来节省内存。
2)我研究了使用boost :: array来节省内存,但是你需要在编译时知道
的大小,而不是运行时间。我希望在运行时可以配置数组中的元素数量,但是我不需要能够调整我的向量。这似乎统治了我们的
boost :: array,因为它在大小上模板化了。是否有任何其他
想法的数组分类我可以用来节省std :: vector的内存?
3)如果我只是使用标准的C ++数组,我如何动态创建一个
指针数组。一个对象数组很简单:
array = new Object [size];
如何对指向对象的指针数组做同样的事情? br />
谢谢,
ben
* Ben:我有一个使用大量内存的程序。
目前我在std :: vector中存储了很多指向对象的指针。
(我有三个问题)
我有三个问题:
1)让我们说平均Vector的大小为2.通过存储我可以节省多少内存c ++数组中的指针,而不是向量。
取决于(A)你在做什么向量,(B)C ++实现
和(C)你是怎么做的实现原始数组的使用。
关于(1),如果你使用的任何矢量大小不同,你的问题2
以下表示_may_就是这种情况,它可能会吞噬内存。
使用类似
模板< typename T>
void clearAll(typename std :: vector< T>& v)
{
std :: vector< T> ;空的;
empty.swap(v);
}
摆脱过大的缓冲区(它们只会增加,内部
向量。
2)我研究了使用boost :: array来节省内存,但是你需要知道编译时的大小时间,而不是运行时间。我想在运行时可以配置数组中的元素数量,但是我不需要能够调整我的向量。这似乎统治了我们的
boost :: array,因为它在大小上被模板化了。对于std :: vector,我可以用来节省内存的数组的任何其他想法吗?
不知道;我只是自己动手,但仔细考虑之后。
3)如果我只使用标准C ++数组,我该如何创建一个指针数组?苍蝇一个对象数组很简单:
array = new Object [size];
如何对指向对象的指针数组做同样的事情?
我不明白你怎么也不知道这个,但无论如何:
typedef Object * ObjectPtr;
ObjectPtr * array = new ObjectPtr [size];
-
答:因为它弄乱了人们通常阅读文本的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet上最烦人的事情是什么?在电子邮件中?
Ben写道:我有一个使用大量内存的程序。
>目前我在std :: vector中存储了很多指向对象的指针。
(数百万)
我有三个问题:
1)让我们说平均Vector的大小为2.我可以通过将指针存储在c ++数组而不是向量中来节省多少内存。
典型的矢量大小为12到16个字节。
典型的''数组''大小(我假设你的意思是动态数组)会是(比如说)6
字节,4个字节用于指针,两个字节用于保存大小。
在这两种情况下,这些尺寸都不包括实际尺寸存储的数据。
从上面我假设你有'数百万'/ 2个数组或向量。哪个
意味着你可以节省3到5'''百万''字节。
2)我研究了使用boost :: array来节省内存,但你需要
知道编译时的大小,而不是运行时。我想在运行时可以配置数组中的元素数量,但是我不需要能够调整我的向量。这似乎统治了我们的
boost :: array,因为它在大小上被模板化了。对于std :: vector,我可以用来节省内存的数组的任何其他想法吗?
所以你的所有矢量或数组都是固定大小的,但是这个大小不是直到运行时才知道的。那是对的吗?将所有数百万个b $ b矢量/数组合并到一个大型矢量中似乎是显而易见的
解决方案。将单个大型向量包装到一个方便的类中,然后用鲍勃的'b $ b b>你的叔叔。
3)如果我只使用标准的C ++数组,我该如何创建一个数组
指针在飞行中。一个对象数组很简单:
array = new Object [size];
如何对指向对象的指针数组做同样的事情?
完全相同的语法,只需用Object *代替Object。
array = new Object * [size];
当然这只是创建了指针数组,它不会自动生成任何指针
,因为你需要
for(int i = 0; i< size; ++ i)
array [i] = new Object;
谢谢,
ben
john
Ben写道:我有一个使用大量内存的程序。
目前我在std :: vector中存储了很多指向对象的指针。
(数百万)
单个std数百万::向量或数以百万计的std ::向量数百万?
我有三个问题:
1)让我们说平均向量大小为2.
什么是矢量?你的意思是std :: vector?
我可以通过将指针存储在c ++数组而不是向量中来节省多少内存。
未知。什么是向量?
2)我研究了使用boost :: array来节省内存,但是你需要在编译时知道大小,而不是运行时。我想在运行时可以配置数组中的元素数量,但是我不需要能够调整我的向量。这似乎统治了我们的
boost :: array,因为它在大小上被模板化了。对于std :: vector,我可以用来节省内存的数组的任何其他想法吗?
编号std :: vector是你参数的最佳选择:在运行时大小已知
,但不会改变。确保在构造时使用
定义矢量,或者立即保留。
3)如果我只使用标准C ++数组,怎么做我在运行中创建了一个
指针数组。一个对象数组很简单:
array = new Object [size];
如何对指向对象的指针数组做同样的事情?
对象**数组=新对象* [大小];
为什么需要保留指针? 对象是一类
层次结构的基类吗?你如何创建单个对象?有没有
a有理由收藏指针而不是收藏品
的物品?
V
I have a program which is using a lot of memory.
At the moment I store a lot of pointers to objects in std::vector.
(millions of them)
I have three questions:
1) Lets say the average Vector is of size 2. How much memory can I
save by storing my pointers in c++ arrays, rather than vectors.
2) I looked into using boost::array to save memory, but you need to
know the size at compile time, as opposed to run time. I want to
number of elements in the array to be configurable at run time, but I
don''t need to be able to resize my vectors. This seems to rule our
boost::array, since it is templatized on size. Are there any other
ideas for array classed I can use to save memory over std::vector?
3) If I just use a standard C++ array, how do I create an array of
pointers on the fly. An array of Objects is easy:
array = new Object[size];
How do I do the same for an array of pointers to Objects?
Thanks,
ben
* Ben:I have a program which is using a lot of memory.
At the moment I store a lot of pointers to objects in std::vector.
(millions of them)
I have three questions:
1) Lets say the average Vector is of size 2. How much memory can I
save by storing my pointers in c++ arrays, rather than vectors.
Depends on (A) what you''re doing with the vectors, (B) the C++ implementation
and (C) how you''d implement the use of raw arrays.
Regarding (1), if any vector you''re using can vary in size, as your question 2
below indicates _may_ be the case, it might be gobbling up memory.
Use something like
template< typename T >
void clearAll( typename std::vector<T>& v )
{
std::vector<T> empty;
empty.swap( v );
}
to get rid of overly large buffers (they only increase, never decrease) inside
the vectors.
2) I looked into using boost::array to save memory, but you need to
know the size at compile time, as opposed to run time. I want to
number of elements in the array to be configurable at run time, but I
don''t need to be able to resize my vectors. This seems to rule our
boost::array, since it is templatized on size. Are there any other
ideas for array classed I can use to save memory over std::vector?
Don''t know; I''d just roll my own, but after careful consideration.
3) If I just use a standard C++ array, how do I create an array of
pointers on the fly. An array of Objects is easy:
array = new Object[size];
How do I do the same for an array of pointers to Objects?
I don''t understand how you can not know this, but anyway:
typedef Object* ObjectPtr;
ObjectPtr* array = new ObjectPtr[size];
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ben wrote:I have a program which is using a lot of memory.
At the moment I store a lot of pointers to objects in std::vector.
(millions of them)
I have three questions:
1) Lets say the average Vector is of size 2. How much memory can I
save by storing my pointers in c++ arrays, rather than vectors.
Typical vector size would be 12 to 16 bytes.
Typical ''array'' size (I assume you mean dynamic array) would be (say) 6
bytes, 4 bytes for a pointer and two bytes to save the size.
In both cases these sizes are exclusive of the actual data stored.
From the above I assume you have ''millions''/2 arrays or vectors. Which
means you would save 3 to 5 ''millions'' of bytes.
2) I looked into using boost::array to save memory, but you need to
know the size at compile time, as opposed to run time. I want to
number of elements in the array to be configurable at run time, but I
don''t need to be able to resize my vectors. This seems to rule our
boost::array, since it is templatized on size. Are there any other
ideas for array classed I can use to save memory over std::vector?
So all your vectors or arrays are of fixed size, but that size is not
known until run time. Is that correct? Combining all the millions of
vectors/arrays into one large vector would seem to be the obvious
solution. Wrap that single large vector into a handy class and Bob''s
your uncle.
3) If I just use a standard C++ array, how do I create an array of
pointers on the fly. An array of Objects is easy:
array = new Object[size];
How do I do the same for an array of pointers to Objects?
Exactly the same syntax, just substitute Object* for Object.
array = new Object*[size];
Of course this just creates the array of pointers, it does not make any
of the pointers themselves, for that you need
for (int i = 0; i < size; ++i)
array[i] = new Object;
Thanks,
ben
john
Ben wrote:I have a program which is using a lot of memory.
At the moment I store a lot of pointers to objects in std::vector.
(millions of them)
Millions in a single std::vector or millions in millions of std::vectors?
I have three questions:
1) Lets say the average Vector is of size 2.
What''s a Vector? Do you mean std::vector?
How much memory can I
save by storing my pointers in c++ arrays, rather than vectors.
Unknown. What''s a Vector?
2) I looked into using boost::array to save memory, but you need to
know the size at compile time, as opposed to run time. I want to
number of elements in the array to be configurable at run time, but I
don''t need to be able to resize my vectors. This seems to rule our
boost::array, since it is templatized on size. Are there any other
ideas for array classed I can use to save memory over std::vector?
No. std::vector is the best choice for your parameters: size is known
at run time, but doesn''t change. Make sure you define your vectors with
the argument when constructing, or ''reserve'' right away.
3) If I just use a standard C++ array, how do I create an array of
pointers on the fly. An array of Objects is easy:
array = new Object[size];
How do I do the same for an array of pointers to Objects?
Object **array = new Object*[size];
Why do you need to keep pointers? Is ''Object'' a base class for some
hierarchy of classes? How do you create individual objects? Is there
a good reason to have collections of pointers instead of collections
of objects?
V
这篇关于数组与矢量帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!