CLIST-添加或删除元素会使先前存储的位置无效吗? [英] CLIST -- Does adding or removing elements invalidates previously stored positions?

查看:117
本文介绍了CLIST-添加或删除元素会使先前存储的位置无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据类型"OpcValue"-其定义是

I have a DataType "OpcValue" -- its definition is

  OpcValue
      |
   ___|___
  |       |
_variant_t   CCsTime



1.)我正在从文件中读取数据值和时间戳,并将OpcValue类型的点插入CLIST对象中-(将从文件中读取总共1000点).添加每个点时,将计算最后插入的位置.

2.)对于每添加的100个点,我将第100个点的位置(第100个点的最后插入位置)存储到一个单独的数组中-CARRAY.

3.)现在,当我不得不根据时间戳显示CList(可能是第388个项目)中的值时,我会通过CARRAy获取

第300个点(CArray的第3个项目)的位置,然后从那里进行迭代以获得第388个点,从而避免了前300个点的迭代.这样可以节省时间,并且效果很好.

现在我有3种情况:

i.)从CLIST(靠近尾部的点)中删除前400个点,如果我尝试显示其余点,如果我使用CArray的引用(存储的CLIST位置引用) ,这些职位仍然有效,我得到了结果.

ii.)如果我将更多点添加到CLIST并使用CArray引用,则整个操作也可以正常工作.

iii.)但是在第三种情况下,如果我添加更多点并从列表中删除一些点,则CArray位置不再有效.


为什么会这样?

在CLIST中添加或删除点会使所有位置无效吗?如果是这样的话,在前两种情况下该如何解决?



1.) I am reading datavalues and timestamps from a file and inserting points of type OpcValue into a CLIST object -- (total 1000 points will be read from the file). A last inserted position is calculated when adding each point.

2.) For every 100th point added, i am storing the position of the 100th point(last inserted position of the 100th point) into a seperate array -- CARRAY.

3.) Now when ever i have to display the values from the CList (may be 388th item) depending on the timestamp, i go through the CARRAy to get

the position of 300th point(3rd item of the CArray) and iterate from there to get the 388th point, thereby avoiding iteration of first 300 points. this saves time and is working excellent.

Now i have 3 scenarios:

i.) The first 400 points are removed from the CLIST (the points near to the tail), and if i am trying to display the remaining points, if i use the CArray''s reference (CLIST''s position reference stored), the positions are still valid and i am getting the result.

ii.) If i add more points to the CLIST and use the CArray reference, the whole operation is working fine too.

iii.) But in the third scenario, if i add more points and remove some points from the list, the CArray positions are no more valid.


Why is this happening?

Does adding or removing points in a CLIST makes all the positions invalid? If so how is it wroking in the first two cases?

推荐答案

假设您使用的是MFC的CList,则无需执行所有这些额外操作工作.结合使用GetAt FindIndex ,您可以在特定的索引处获取项目.

请参阅 http://msdn.microsoft.com/en-us/library/et138h3x(v = VS.100).aspx [
Assuming you are using MFC''s CList, there is no need to do all this extra work. Using a combination of GetAt and FindIndex you can get the item at a specific index.

See http://msdn.microsoft.com/en-us/library/et138h3x(v=VS.100).aspx[^]


------

After our discussion below I thought I should add this here. A CMap may be a better data structure for you in this specific scenario. That way you won''t have to store lookup positions in the array.


只是一个猜测,但是一旦您删除了元素并将其添加到CList中,就有可能重新分配正在进行,这将使您存储的职位无效.您将必须单步执行CList代码以更好地了解何时发生这些重新分配.
Just a guess, but it''s possible that once you are removing and adding elements to the CList, some reallocation is taking place which will invalidate the positions you are storing. You will have to step through the CList code to better understand when these reallocations occur.


这篇关于CLIST-添加或删除元素会使先前存储的位置无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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