Chinaunix

标题: 菜鸟读C++ STL源程序的疑问 [打印本页]

作者: catbert    时间: 2005-11-06 13:44
标题: 菜鸟读C++ STL源程序的疑问
看STL list node的定义:

  struct _List_node_base
  {
    _List_node_base* _M_next;   ///< Self-explanatory
    _List_node_base* _M_prev;   ///< Self-explanatory

    static void
    swap(_List_node_base& __x, _List_node_base& __y);

    void
    transfer(_List_node_base * const __first,
             _List_node_base * const __last);

    void
    reverse();

    void
    hook(_List_node_base * const __position);

    void
    unhook();
  };

  template<typename _Tp>
    struct _List_node : public _List_node_base
    {
      _Tp _M_data;                ///< User's data.
    };

疑问:
1)User's data为什么是public,而不是private + friend class?
2)swap(), transfer(), reverse(), hook() 和 unhook()都是用来做什么的?
3) 为什么没有constructor?

另外看list源程序中的注释:
   *  Second, a %list conceptually represented as
   *  @code
   *    A <---> B <---> C <---> D
   *  @endcode
   *  is actually circular; a link exists between A and D.  The %list
   *  class holds (as its only data member) a private list::iterator
   *  pointing to @e D, not to @e A!  To get to the head of the %list,
   *  we start at the tail and move forward by one.  When this member
   *  iterator's next/previous pointers refer to itself, the %list is
   *  %empty.  @endif
如果没有直接指向head的指针,是否在list头和尾操作的效率不一样?
谢谢!
作者: Yarco    时间: 2005-11-06 18:07
提示: 作者被禁止或删除 内容自动屏蔽
作者: yhb1266    时间: 2005-11-06 21:00
那位大哥能详细解释哈,怎么和我看的STL不一样啊?
我看的《数据结构与STL》
作者: catbert    时间: 2005-11-07 05:03
原帖由 yhb1266 于 2005-11-6 08:00 发表
那位大哥能详细解释哈,怎么和我看的STL不一样啊?
我看的《数据结构与STL》


这是/usr/include下的源程序。
请高手赐教!
作者: catbert    时间: 2005-11-08 03:39
自己顶一下,请教!
作者: renstone921    时间: 2005-11-08 13:07
User's data为什么是public,而不是private + friend class?

一个是性能,第二是因为没有必要,作为库的用户,会直接使用_List_node_base这个类吗?这个类仅仅是为了提供一个_List_node类的接口,定义了一个函数集。

为什么没有constructor?
因为你不会直接创建他的实例。
作者: yarco1    时间: 2005-11-08 14:00
原帖由 renstone921 于 2005-11-8 13:07 发表
User's data为什么是public,而不是private + friend class?

一个是性能,第二是因为没有必要,作为库的用户,会直接使用_List_node_base这个类吗?这个类仅仅是为了提供一个_List_node类的接口,定义了一个函 ...


顶了. 经验. "应用大于一切"的意思.
作者: catbert    时间: 2005-11-13 13:56
谢了!

另外请问,原贴中列出的 transfer(), swap(), hood(), unhook(), reverse() 是做什么用的?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2