问题描述
亲爱的名单,
目前尚不清楚标题的含义。 :-)详情如下:
我需要管理一大堆小物件,比如
struct {
int a;
int b [size_undetermined];
} obj;
我不想用通常的int *,new,delete方法,因为
1.一百万新和删除会很慢
2.我需要访问b跨所有对象的元素。可能有更好的方法来循环obj [i] .b [2]。
3. b的大小通常很小所以额外的int的开销*
太大。
我目前正在使用vector< int>(num_objects *(1 + size_of_b))来存储
对象和向量< int> :: iterator访问元素。但是
的问题是:
1.我想将obj的参考传递给其他函数,但我只有
有vector< int> :: iterator,not vector< obj> :: iter。
2.如果我想扩展obj(例如添加另一个变量),我的代码
因索引变化而必须重写。
有没有更好的方法呢?我可以以某种方式创建一个动态类型
struct {int a,int b [size];并应用于现有的向量?
boost :: memorypool似乎是一个好主意,但使用指针仍然浪费了
内存。
非常感谢提前。
Bo
Dear List,
It is not clear what the title means. :-) Here is the details:
I need to manage a big bunch of small objects, like
struct{
int a;
int b[size_undetermined];
}obj;
I do not want to use the usual int*, new, delete method since
1. a million new and delete would be slow
2. I need to access elements of b cross all objects. There might be
better ways to loop through obj[i].b[2].
3. the size of b is usually small so the overhead of an additional int *
is too big.
I am currently using vector<int>( num_objects*(1+size_of_b) ) to store
the objects and vector<int>::iterator to access elements. But the
problems are:
1. I would like to pass reference of obj to other functions but I only
have vector<int>::iterator, not vector<obj>::iter.
2. If I would like to expand obj (e.g. adding another variable), my code
has to be rewritten because of index changes.
Is there a better way to do it? Can I somehow create a dynamic type
struct{ int a, int b[size]; } and apply to the existing vector?
boost::memorypool seems to be a good idea but there is still a waste of
memory for using pointers.
Many thanks in advance.
Bo
推荐答案
-Mike
-Mike
哎呀。那不是一个很好的结构。我认为
对你来说会更好:
// MyFile.cpp:
#include< iostream>
#include< vector>
使用std :: vector;
struct MyStruct
{
int a;
vector< int> b;
};
int main(无效)
{
使用std :: cout;
使用std :: endl;
// InitSize是对象容器的初始大小:
const int InitSize = 10;
vector< MyStruct> Objs(InitSize);
{//限制范围
MyStruct Splat; //临时对象
Splat.a = 13;
Splat.b.push_back(22);
Splat.b.push_back(10 );
Objs [0] = Splat;
} //这里没有创建Splat
{//限制范围
MyStruct Splat; //临时对象
Splat.a = 97;
Splat.b.push_back(33);
Splat.b.push_back(84 );
Splat.b.push_back(27);
Objs [1] = Splat;
} //这里没有创建Splat
vector< MyStruct> :: size_type i; // Objs的索引
vector< int> :: iterator j; //迭代元素b
for(i = 0; i< 2; ++ i)
{
cout<< 对象编号: << i<< endl;
cout<< a的价值: << Objs [i] .a<< endl;
cout<< b的元素: << endl;
for(j = Objs [i] .b.begin(); j!= Objs [i] .b.end(); ++ j)
{
cout<< (* j)<< endl;
}
cout<< endl<<结束;
}
返回0;
}
当然,你会需要处理诸如是否以空的或预先大小的容器开始的问题,以及是否使用
向量或其他容器。如果您经常更改大小
或插入/删除元素,那么列表或双端队列可以更好地工作
。取决于你的申请细节。
希望上面给你一些有用的想法。
-
干杯,
Robbie Hatley
美国加利福尼亚州塔斯廷
电子邮件:来自pacbell dot net的lonewolfintj
web :home dot pacbell dot net slant earnur slant
---- ==发表于Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News == ----
#1新闻组服务世界! > 100,000新闻组
--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---
Yuck. That''s not a very good construct. I think the
following kind of thing will work much better for you:
// MyFile.cpp:
#include <iostream>
#include <vector>
using std::vector;
struct MyStruct
{
int a;
vector<int> b;
};
int main(void)
{
using std::cout;
using std::endl;
// InitSize is initial size of container of objects:
const int InitSize = 10;
vector<MyStruct> Objs (InitSize);
{ // Limit scope
MyStruct Splat; // Temporary object
Splat.a = 13;
Splat.b.push_back(22);
Splat.b.push_back(10);
Objs[0] = Splat;
} // Splat is uncreated here
{ // Limit scope
MyStruct Splat; // Temporary object
Splat.a = 97;
Splat.b.push_back(33);
Splat.b.push_back(84);
Splat.b.push_back(27);
Objs[1] = Splat;
} // Splat is uncreated here
vector<MyStruct>::size_type i; // Index to Objs
vector<int>::iterator j; // Iterator to elements of b
for (i = 0; i <2; ++i)
{
cout << "Object Number: " << i << endl;
cout << "Value of a: " << Objs[i].a << endl;
cout << "Elements of b:" << endl;
for (j = Objs[i].b.begin(); j != Objs[i].b.end(); ++j)
{
cout << (*j) << endl;
}
cout << endl << endl;
}
return 0;
}
Of course, you''ll need to handle issues such as whether to
start with empty or pre-sized containers, and whether to use
vectors or some other containers. If you change sizes
or insert/delete elements often, then a list or deque may work
better. Depends on the details of your application.
Hope the above gives you some useful ideas.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
这篇关于管理许多小物件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!