为什么forward_list不支持push_back

hywxn1 2014-05-11 03:28:51
为什么forward_list不支持push_back而list支持呢,标准库容器这样设计是什么道理啊
...全文
342 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zybjtu 2014-05-14
  • 打赏
  • 举报
回复
说错了,forward_list 查到链表尾部是O(n)复杂度。 因为要从链表头出发,寻找到链表尾
zybjtu 2014-05-14
  • 打赏
  • 举报
回复
list 是由双向链表实现的。只要记录尾指针便可以O(1)插入删除。 forward_list 顾名思义,是单链表实现的,不构成环的话,有去无回。每次从链表头插入需要O(n)复杂度
ri_aje 2014-05-14
  • 打赏
  • 举报
回复
标准自己说了原因: c++11 23.3.4.1/1 [Note: It is intended that forward_list have zero space or time overhead relative to a hand-written C-style singly linked list. Features that would conflict with that goal have been omitted. — end note]
mujiok2003 2014-05-13
  • 打赏
  • 举报
回复
因为单向链表实现push_back效率不高(复杂度为O(n)), 而双向链表的push_back复杂度为O(1)。
rmaly 2014-05-13
  • 打赏
  • 举报
回复
应该是内部数据结构是会有排序的,所以push_back毫无意义
merlinfang 2014-05-11
  • 打赏
  • 举报
回复
Forward list is a container which supports fast insertion and removal of elements from anywhere from the container. Fast random access is not supported. It is implemented as singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage, when bidirectional iteration is not needed.

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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