收集vector重新调整大小后指向它的iterator的定位方法。

ttzzgg_80713 2001-12-20 07:47:15
请发言
...全文
135 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
有一腚的道理。肯定比iterator要省。

谢谢你了。给分了。

陈硕 2001-12-20
  • 打赏
  • 举报
回复
可否考虑用一个整形变量来保存当前选项的下标,这样可能比用一个iterator要省空间。
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
应该有的。

用一个iterator指向一个记录集中的一个值。作为当前选择项。
当这个集增大后,依然要指向它。

我想这个时候就有用了吧
陈硕 2001-12-20
  • 打赏
  • 举报
回复
相当于记录下标?
值得一试。
不过这有什么实际用处吗?
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
喔?

我记的在那个书里看到过有distance这个函数可以记算当前的iterator指向的位置与begin位置之间的偏移大小。如果vector重新调整了以后。是不是可以用一个begin 加上这个偏移值。
来重新找到原来指向的位置呢?
陈硕 2001-12-20
  • 打赏
  • 举报
回复
不明白您的意思。
陈硕 2001-12-20
  • 打赏
  • 举报
回复
不明白?
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
翻的好。这样的确可以。顺便再说一下。diastance好像也行吧。或者记下与begin处的offset?
等会给分
陈硕 2001-12-20
  • 打赏
  • 举报
回复
意译:如果你想让某个vector的iterators永不失效,可以用reserve()为这个vector预先分配可能用到的最大内存,并且确保所有的插入和删除动作都在vector的尾端进行。
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
谢谢指正。

[5] 的最后一句说的什么。我看的不明白。那位好心的大哥番一下
陈硕 2001-12-20
  • 打赏
  • 举报
回复
应该是cout << *it << endl;

Notes

[2] Memory will be reallocated automatically if more than capacity() - size() elements are inserted into the vector. Reallocation does not change size(), nor does it change the values of any elements of the vector. It does, however, increase capacity(), and it invalidates [5] any iterators that point into the vector.

[4] Reserve() causes a reallocation manually. The main reason for using reserve() is efficiency: if you know the capacity to which your vector must eventually grow, then it is usually more efficient to allocate that memory all at once rather than relying on the automatic reallocation scheme. The other reason for using reserve() is so that you can control the invalidation of iterators. [5]

[5] A vector's iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting an element in the middle of a vector invalidates all iterators that point to elements following the insertion or deletion point. It follows that you can prevent a vector's iterators from being invalidated if you use reserve() to preallocate as much memory as the vector will ever use, and if all insertions and deletions are at the vector's end.
ttzzgg_80713 2001-12-20
  • 打赏
  • 举报
回复
呵呵。大佛来了。
vector<string> vtStr;
vtStr.push_back("i live you);

vector<string>::iterator it = vtStr.begin();


vtStr.resize(10000);//这样的话它的内存位置是不是应该变动了

cout<< it << endl; //it 指向的还是 i love you 么
陈硕 2001-12-20
  • 打赏
  • 举报
回复
举个具体的例子先,要有简短的代码

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧