用于分类物品的STL容器 [英] STL container for sorted items

查看:72
本文介绍了用于分类物品的STL容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要关于填充的一些建议

我有一个我需要搜索的项目的排序列表和

检索所述项目并且还修改一个项目它的身份。

我认为Map stl容器最适合。我是否正确我的

假设

问题

Reocrds已存储在二进制文件中的唯一标识

格式。结构大致是


----------------------

记录计数

--------------------

Record1 ------------- -------------------------字符串标识符

--------------- ---

--------------------

Obj

指针

记录2

------------------

记录n

--------------------


-------------

Obj1

------------

Obj2

------ ------


我建议有一个< strings,Obj Pointer>的地图。该地图最初将使用文件项初始化,然后根据标识符提供基于标识符的项目

并更新项目

基于标识符。

另外如果处理存储在内存中的文件,建议首先使用

a STL而不是直接使用地址

操作来获取物品,因为标识符已经分类了?

解决方案

3月14日下午4:16,Hunk < santosh.udyav ... @ gmail.comwrote:


想要一些关于填充的建议

我有一个排序列表我需要搜索的项目和

检索所述项目并根据其身份修改项目。

我认为Map stl容器最适合。我是否正确我的

假设

问题

Reocrds已存储在二进制文件中的唯一标识

格式。结构大致是


----------------------

记录计数

--------------------

Record1 ------------- -------------------------字符串标识符

--------------- ---

--------------------

Obj

指针

记录2

------------------

记录n

--------------------


-------------

Obj1

------------

Obj2

------ ------


我建议有一个< strings,Obj Pointer>的地图。该地图最初将使用文件项初始化,然后根据标识符提供基于标识符的项目

并更新项目

基于标识符。

另外如果处理存储在内存中的文件,建议首先使用

a STL而不是直接使用地址

操作来获取项目,因为标识符已经分类了?b $ b排序?



我认为上面的格式化已经搞砸了。我把格式粘贴一次

再次

----------------------

记录数

---------- ----------

Record1

记录2

------------- -----

记录n

--------------------

-------------

Obj1

------------

Obj2

------------


每条记录由

身份组成(独一无二)

Obj指针(指向对象obj1,ibj2的指针)



3月14日,7:23下午,Hunk < santosh.udyav ... @ gmail.comwrote:


3月14日下午4:16,Hunk < santosh.udyav ... @ gmail.comwrote:


想要一些关于填充的建议

我有一个排序列表我需要搜索的项目和

检索所述项目并根据其身份修改项目。

我认为Map stl容器最适合。我是否正确我的

假设

问题

Reocrds已存储在二进制文件中的唯一标识

format.The结构大致是


----------------------

记录数

--------------------

Record1 ----- ---------------------------------字符串标识符

------- -----------

--------------------

Obj

指针

记录2

------------------

记录n

--------------------


-------------

Obj1

------------

Obj2

------------


我建议有一张<的地图;字符串,Obj指针>。该地图最初将使用文件项初始化,然后根据标识符提供基于标识符的项目

并更新项目

基于标识符。

另外如果处理存储在内存中的文件,建议首先使用

a STL而不是直接使用地址

操作来获取项目,因为标识符已经分类了?b $ b排序?



我认为上面的formattin已经搞砸了。我已经把格式粘贴了一次

再次

----------------------

记录数

---------- ----------

Record1

记录2

------------- -----

记录n

--------------------

-------------

Obj1

------------
Obj2

------------


每条记录由
$ b组成$ b身份(唯一)

Obj指针(指向对象obj1,ibj2的指针)



我认为Set更合适;

排序本身应该以<运算符应该

超载。

例如,由于排序后的元素对你很重要,你可以使用

创建一个包含两者的类身份和对象

内容;在那个班级你应该重载较少的操作员比较

到身份。


问候

3月15日上午12:30,coosa < coos ... @ gmail.comwrote:


3月14日下午7:23,Hunk < santosh.udyav ... @ gmail.comwrote:


3月14日下午4:16,Hunk < santosh.udyav ... @ gmail.comwrote:


想要了解一些关于填充的建议/>
我有一个需要搜索的项目的排序列表和

检索所述项目并根据其身份修改项目。

我认为Map stl容器最适合。我是否正确我的

假设

问题

Reocrds已存储在二进制文件中的唯一标识

format.The结构大致是


------------ ----------

记录数

--------------------

Record1 --------------------------------------字符串标识符

------------------

------------------- -

Obj

指针

记录2

------------- -----

记录n

--------------------


-------------

Obj1

------------

Obj2

------------


我建议有一个< strings,Obj Pointer>的地图。该地图最初将使用文件项初始化,然后根据标识符提供基于标识符的项目

并更新项目

基于标识符。

另外如果处理存储在内存中的文件,建议首先使用

a STL而不是直接使用地址

操作来获取项目,因为标识符已经分类了?b $ b排序?


我认为上面的formattin已经搞砸了。我再次粘贴格式



----------------------

记录数

- ------------------

Record1

记录2

----- -------------

记录n

------------------- -


-------------

Obj1

------------

Obj2

------------


每条记录由
组成
标识(唯一)

Obj指针(指向对象obj1,ibj2的指针)



我认为Set更合适;

排序本身应该是这样的<运算符应该

超载。

例如,由于排序后的元素对你很重要,你可以使用

创建一个包含两者的类身份和对象

内容;在那个班级你应该重载较少的操作员比较

到身份。


问候



假设该类被称为A并且它包含id为int和指向对象的

指针;

因此:

typedef set< A,少于< A> A_set;

..

..

..

为您的A级定义:

bool A :: operator< (const A& a)const {return this-> id< a.id;}

现在A_set s;

s简单地取一个A的对象,确保插入所有对象

A根据每个对象内部的ID

按升序排序到集合中。


希望它有帮助


Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?

解决方案

On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:

Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?

I think the formattin above is screwed up.I''m pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------

Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)



On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:

On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:

Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?


I think the formattin above is screwed up.I''m pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)

I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.

Regards


On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:

On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:

On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:

Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?

I think the formattin above is screwed up.I''m pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)


I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.

Regards

Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
..
..
..
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.

Hope it helps


这篇关于用于分类物品的STL容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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