关于STL中List解析

pagerank算法详解 目录一、pagerank定义两个重要假设二、pagerank算法公式定义计算演示矩阵化计算三、存在的两个问题问题1.Dead Ends问题2.Spider Traps 一、pagerank定义 入链数:指向该节点的链接数 出链数:由该节点指出的链接数 以上图为例:A的入链数为1,出链数为3,所以将由A指向其他节点的边权重设置为1/3,表示A访问B、C、D节点的概率均为1/3 两个重要假设 数量假设:在Web图模型中,如果一个页面节点接收到的其他网页指向的入链数量越多,那么这个页面越重要。 质量假设:指向 阅读详情

相对于vector的连续性空间,list就显得复杂许多,它的好处每次插入或者删除一个元素,就配置或释放一个元素空间。因此,list对于空间的运用有绝对的精准。而且,对于任何位置的元素插入或元素的移除,list永远是常熟时间。

list和vector是两个最常被使用的 容器。什么时机下最适合使用哪个容器,必须看元尿素的多寡,元素的构造复杂度等关于他们的比较我们看下以下的叙述:

list是双向链表


    每一个结点都包括一个信息快Info、一个前驱指针Pre、一个后驱指针Post。可以不分配必须的内存大小方便的进行添加和删除操作。使用的是非连续的内存空间进行存储。
   优点:(1) 不使用连续内存完成动态操作。
               (2) 在内部方便的进行插入和删除操作
               (3) 可在两端进行push、pop
   缺点:(1) 不能进行内部的随机访问,即不支持[ ]操作符和vector.at()

               (2) 相对于verctor占用内存多

接下来看看其迭代器


此处附上本人自己实现的双向链表点击打开链接

并且在此附上本热你自己实现单链表点击打开链接


并且,在此在这里给大家附上STL的源码:

stl_list.h 
// Filename:    stl_list.h
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/* NOTE: This is an internal header file, included by other STL headers.
 *   You should not attempt to use it directly.
 */

#ifndef __SGI_STL_INTERNAL_LIST_H
#define __SGI_STL_INTERNAL_LIST_H

__STL_BEGIN_NAMESPACE

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#endif

////////////////////////////////////////////////////////////////////////////////
// list结点, 提供双向访问能力
////////////////////////////////////////////////////////////////////////////////
//  --------           --------           --------           --------
//  | next |---------->| next |---------->| next |---------->| next |
//  --------           --------           --------           --------
//  | prev |<----------| prev |<----------| prev |<----------| prev |
//  --------           --------           --------           --------
//  | data |           | data |           | data |           | data |
//  --------           --------           --------           --------
////////////////////////////////////////////////////////////////////////////////

template <class T>
struct __list_node
{
  typedef void* void_pointer;
  void_pointer next;
  void_pointer prev;
  T data;
};

// 至于为什么不使用默认参数, 这个是因为有一些编译器不能提供推导能力,
// 而作者又不想维护两份代码, 故不使用默认参数
template<class T, class Ref, class Ptr>
struct __list_iterator
{
  // 标记为'STL标准强制要求'的typedefs用于提供iterator_traits<I>支持
  typedef __list_iterator<T, T&, T*>             iterator;   // STL标准强制要求
  typedef __list_iterator<T, const T&, const T*> const_iterator;
  typedef __list_iterator<T, Ref, Ptr>           self;

  typedef bidirectional_iterator_tag iterator_category;
  typedef T value_type;                                 // STL标准强制要求
  typedef Ptr pointer;                                  // STL标准强制要求
  typedef Ref reference;                                // STL标准强制要求
  typedef __list_node<T>* link_type;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;                    // STL标准强制要求

  // 这个是迭代器实际管理的资源指针
  link_type node;

  __list_iterator(link_type x) : node(x) {}
  __list_iterator() {}
  __list_iterator(const iterator& x) : node(x.node) {}

  // 在STL算法中需要迭代器提供支持
  bool operator==(const self& x) const { return node == x.node; }
  bool operator!=(const self& x) const { return node != x.node; }

  // 重载operator *, 返回实际维护的数据
  reference operator*() const { return (*node).data; }

#ifndef __SGI_STL_NO_ARROW_OPERATOR
  // 如果支持'->'则重载之
  // 解释一下为什么要返回地址
  // class A
  // {
  // public:
  //    // ...
  //    void fun();
  //    // ...
  // }
  // __list_iterator<A, A&, A*> iter(new A)
  // iter->fun();
  // 这就相当于调用(iter.operator())->fun();
  // 经过重载使其行为和原生指针一致
  pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */

  // 前缀自加
  self& operator++()
  {
    node = (link_type)((*node).next);
    return *this;
  }

  // 后缀自加, 需要先产生自身的一个副本, 然会再对自身操作, 最后返回副本
  self operator++(int)
  {
    self tmp = *this;
    ++*this;
    return tmp;
  }

  self& operator--()
  {
    node = (link_type)((*node).prev);
    return *this;
  }

  self operator--(int)
  {
    self tmp = *this;
    --*this;
    return tmp;
  }
};

// 如果编译器支持模板类偏特化那么就不需要提供以下traits函数
// 直接使用<stl_iterator.h>中的
// template <class Iterator>
// struct iterator_traits
#ifndef __STL_CLASS_PARTIAL_SPECIALIZATION

template <class T, class Ref, class Ptr>
inline bidirectional_iterator_tag
iterator_category(const __list_iterator<T, Ref, Ptr>&) {
  return bidirectional_iterator_tag();
}

template <class T, class Ref, class Ptr>
inline T*
value_type(const __list_iterator<T, Ref, Ptr>&) {
  return 0;
}

template <class T, class Ref, class Ptr>
inline ptrdiff_t*
distance_type(const __list_iterator<T, Ref, Ptr>&) {
  return 0;
}

#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */

////////////////////////////////////////////////////////////////////////////////
// 链表本身成环, 且是双向链表, 这样计算begin()和end()是常数时间
////////////////////////////////////////////////////////////////////////////////
//       end()              头结点             begin()
//         ↓                  ↓                  ↓
//      --------           --------           --------           --------
// ---->| next |---------->| next |---------->| next |---------->| next |------
// |    --------           --------           --------           --------     |
// |  --| prev |<----------| prev |<----------| prev |<----------| prev |<--| |
// |  | --------           --------           --------           --------   | |
// |  | | data |           | data |           | data |           | data |   | |
// |  | --------           --------           --------           --------   | |
// |  |                                                                     | |
// |  | --------           --------           --------           --------   | |
// ---|-| next |<----------| next |<----------| next |<----------| next |<--|--
//    | --------           --------           --------           --------   |
//    ->| prev |---------->| prev |---------->| prev |---------->| prev |----
//      --------           --------           --------           --------
//      | data |           | data |           | data |           | data |
//      --------           --------           --------           --------
////////////////////////////////////////////////////////////////////////////////

// 默认allocator为alloc, 其具体使用版本请参照<stl_alloc.h>
template <class T, class Alloc = alloc>
class list
{
protected:
  typedef void* void_pointer;
  typedef __list_node<T> list_node;

  // 这个提供STL标准的allocator接口
  typedef simple_alloc<list_node, Alloc> list_node_allocator;

public:
  typedef T value_type;
  typedef value_type* pointer;
  typedef const value_type* const_pointer;
  typedef value_type& reference;
  typedef const value_type& const_reference;
  typedef list_node* link_type;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;

public:
  typedef __list_iterator<T, T&, T*>             iterator;
  typedef __list_iterator<T, const T&, const T*> const_iterator;

#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  typedef reverse_iterator<const_iterator> const_reverse_iterator;
  typedef reverse_iterator<iterator> reverse_iterator;
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  typedef reverse_bidirectional_iterator<const_iterator, value_type,
  const_reference, difference_type>
  const_reverse_iterator;
  typedef reverse_bidirectional_iterator<iterator, value_type, reference,
  difference_type>
  reverse_iterator;
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */

protected:
  // 分配一个新结点, 注意这里并不进行构造,
  // 构造交给全局的construct, 见<stl_stl_uninitialized.h>
  link_type get_node() { return list_node_allocator::allocate(); }

  // 释放指定结点, 不进行析构, 析构交给全局的destroy,
  // 见<stl_stl_uninitialized.h>
  void put_node(link_type p) { list_node_allocator::deallocate(p); }

  // 创建结点, 首先分配内存, 然后进行构造
  // 注: commit or rollback
  link_type create_node(const T& x)
  {
    link_type p = get_node();
    __STL_TRY {
      construct(&p->data, x);
    }
    __STL_UNWIND(put_node(p));
    return p;
  }

  // 析构结点元素, 并释放内存
  void destroy_node(link_type p)
  {
    destroy(&p->data);
    put_node(p);
  }

protected:
  // 用于空链表的建立
  void empty_initialize()
  {
    node = get_node();
    node->next = node;
    node->prev = node;
  }

  // 创建值为value共n个结点的链表
  // 注: commit or rollback
  void fill_initialize(size_type n, const T& value)
  {
    empty_initialize();
    __STL_TRY {
      // 此处插入操作时间复杂度O(1)
      insert(begin(), n, value);
    }
    __STL_UNWIND(clear(); put_node(node));
  }

// 以一个区间初始化链表
// 注: commit or rollback
#ifdef __STL_MEMBER_TEMPLATES
  template <class InputIterator>
  void range_initialize(InputIterator first, InputIterator last)
  {
    empty_initialize();
    __STL_TRY {
      insert(begin(), first, last);
    }
    __STL_UNWIND(clear(); put_node(node));
  }
#else  /* __STL_MEMBER_TEMPLATES */
  void range_initialize(const T* first, const T* last) {
    empty_initialize();
    __STL_TRY {
      insert(begin(), first, last);
    }
    __STL_UNWIND(clear(); put_node(node));
  }
  void range_initialize(const_iterator first, const_iterator last) {
    empty_initialize();
    __STL_TRY {
      insert(begin(), first, last);
    }
    __STL_UNWIND(clear(); put_node(node));
  }
#endif /* __STL_MEMBER_TEMPLATES */

protected:
  // 好吧, 这个是链表头结点, 其本身不保存数据
  link_type node;

public:
  list() { empty_initialize(); }

  iterator begin() { return (link_type)((*node).next); }
  const_iterator begin() const { return (link_type)((*node).next); }

  // 链表成环, 当指所以头节点也就是end
  iterator end() { return node; }
  const_iterator end() const { return node; }
  reverse_iterator rbegin() { return reverse_iterator(end()); }
  const_reverse_iterator rbegin() const {
    return const_reverse_iterator(end());
  }
  reverse_iterator rend() { return reverse_iterator(begin()); }
  const_reverse_iterator rend() const {
    return const_reverse_iterator(begin());
  }

  // 头结点指向自身说明链表中无元素
  bool empty() const { return node->next == node; }

  // 使用全局函数distance()进行计算, 时间复杂度O(n)
  size_type size() const
  {
    size_type result = 0;
    distance(begin(), end(), result);
    return result;
  }

  size_type max_size() const { return size_type(-1); }
  reference front() { return *begin(); }
  const_reference front() const { return *begin(); }
  reference back() { return *(--end()); }
  const_reference back() const { return *(--end()); }
  void swap(list<T, Alloc>& x) { __STD::swap(node, x.node); }

////////////////////////////////////////////////////////////////////////////////
// 在指定位置插入元素
////////////////////////////////////////////////////////////////////////////////
//       insert(iterator position, const T& x)
//                       ↓
//                 create_node(x)
//                 p = get_node();-------->list_node_allocator::allocate();
//                 construct(&p->data, x);
//                       ↓
//            tmp->next = position.node;
//            tmp->prev = position.node->prev;
//            (link_type(position.node->prev))->next = tmp;
//            position.node->prev = tmp;
////////////////////////////////////////////////////////////////////////////////

  iterator insert(iterator position, const T& x)
  {
    link_type tmp = create_node(x);
    tmp->next = position.node;
    tmp->prev = position.node->prev;
    (link_type(position.node->prev))->next = tmp;
    position.node->prev = tmp;
    return tmp;
  }

  iterator insert(iterator position) { return insert(position, T()); }
#ifdef __STL_MEMBER_TEMPLATES
  template <class InputIterator>
  void insert(iterator position, InputIterator first, InputIterator last);
#else /* __STL_MEMBER_TEMPLATES */
  void insert(iterator position, const T* first, const T* last);
  void insert(iterator position,
              const_iterator first, const_iterator last);
#endif /* __STL_MEMBER_TEMPLATES */

  // 指定位置插入n个值为x的元素, 详细解析见实现部分
  void insert(iterator pos, size_type n, const T& x);
  void insert(iterator pos, int n, const T& x)
  {
    insert(pos, (size_type)n, x);
  }
  void insert(iterator pos, long n, const T& x)
  {
    insert(pos, (size_type)n, x);
  }

  // 在链表前端插入结点
  void push_front(const T& x) { insert(begin(), x); }
  // 在链表最后插入结点
  void push_back(const T& x) { insert(end(), x); }

  // 擦除指定结点
  iterator erase(iterator position)
  {
    link_type next_node = link_type(position.node->next);
    link_type prev_node = link_type(position.node->prev);
    prev_node->next = next_node;
    next_node->prev = prev_node;
    destroy_node(position.node);
    return iterator(next_node);
  }

  // 擦除一个区间的结点, 详细解析见实现部分
  iterator erase(iterator first, iterator last);

  void resize(size_type new_size, const T& x);
  void resize(size_type new_size) { resize(new_size, T()); }
  void clear();

  // 删除链表第一个结点
  void pop_front() { erase(begin()); }
  // 删除链表最后一个结点
  void pop_back()
  {
    iterator tmp = end();
    erase(--tmp);
  }

  list(size_type n, const T& value) { fill_initialize(n, value); }
  list(int n, const T& value) { fill_initialize(n, value); }
  list(long n, const T& value) { fill_initialize(n, value); }

  explicit list(size_type n) { fill_initialize(n, T()); }

// 以一个区间元素为蓝本创建链表
#ifdef __STL_MEMBER_TEMPLATES
  template <class InputIterator>
  list(InputIterator first, InputIterator last)
  {
    range_initialize(first, last);
  }

#else /* __STL_MEMBER_TEMPLATES */
  list(const T* first, const T* last) { range_initialize(first, last); }
  list(const_iterator first, const_iterator last) {
    range_initialize(first, last);
  }
#endif /* __STL_MEMBER_TEMPLATES */

  // 复制构造
  list(const list<T, Alloc>& x)
  {
    range_initialize(x.begin(), x.end());
  }

  ~list()
  {
    // 释放所有结点  // 使用全局函数distance()进行计算, 时间复杂度O(n)
  size_type size() const
  {
    size_type result = 0;
    distance(begin(), end(), result);
    return result;
  }
    clear();
    // 释放头结点
    put_node(node);
  }

  list<T, Alloc>& operator=(const list<T, Alloc>& x);

protected:

////////////////////////////////////////////////////////////////////////////////
// 将[first, last)区间插入到position
// 如果last == position, 则相当于链表不变化, 不进行操作
////////////////////////////////////////////////////////////////////////////////
// 初始状态
//                   first                             last
//                     ↓                                 ↓
//      --------   --------   --------     --------   --------   --------
//      | next |-->| next |-->| next |     | next |-->| next |-->| next |
//  ... --------   --------   -------- ... --------   --------   -------- ...
//      | prev |<--| prev |<--| prev |     | prev |<--| prev |<--| prev |
//      --------   --------   --------     --------   --------   --------
//
//                           position
//                               ↓
//      --------   --------   --------   --------   --------   --------
//      | next |-->| next |-->| next |-->| next |-->| next |-->| next |
//  ... --------   --------   --------   --------   --------   -------- ...
//      | prev |<--| prev |<--| prev |<--| prev |<--| prev |<--| prev |
//      --------   --------   --------   --------   --------   --------
//
// 操作完成后状态
//                           first
//                             |
//               --------------|--------------------------------------
//               | ------------|------------------------------------ |   last
//               | |           ↓                                   | |     ↓
//      -------- | |        --------   --------     --------       | |  --------   --------
//      | next |-- |  ----->| next |-->| next |     | next |-----  | -->| next |-->| next |
//  ... --------   |  |     --------   -------- ... --------    |  |    --------   -------- ...
//      | prev |<---  |  ---| prev |<--| prev |     | prev |<-- |  -----| prev |<--| prev |
//      --------      |  |  --------   --------     --------  | |       --------   --------
//                    |  |                                    | |
//                    |  ------                               | |
//                    ------- |  ------------------------------ |
//                          | |  |                              |
//                          | |  |  -----------------------------
//                          | |  |  |
//                          | |  |  |  position
//                          | |  |  |     ↓
//      --------   -------- | |  |  |  --------   --------   --------   --------
//      | next |-->| next |-- |  |  -->| next |-->| next |-->| next |-->| next |
//  ... --------   --------   |  |     --------   --------   --------   -------- ...
//      | prev |<--| prev |<---  ------| prev |<--| prev |<--| prev |<--| prev |
//      --------   --------            --------   --------   --------   --------
////////////////////////////////////////////////////////////////////////////////
  void transfer(iterator position, iterator first, iterator last)
  {
    if (position != last)
    {
      (*(link_type((*last.node).prev))).next = position.node;
      (*(link_type((*first.node).prev))).next = last.node;
      (*(link_type((*position.node).prev))).next = first.node;
      link_type tmp = link_type((*position.node).prev);
      (*position.node).prev = (*last.node).prev;
      (*last.node).prev = (*first.node).prev;
      (*first.node).prev = tmp;
    }
  }

public:
  // 将链表x移动到position之前
  void splice(iterator position, list& x)
  {
    if (!x.empty())
      transfer(position, x.begin(), x.end());
  }

  // 将链表中i指向的内容移动到position之前
  void splice(iterator position, list&, iterator i)
  {
    iterator j = i;
    ++j;
    if (position == i || position == j) return;
    transfer(position, i, j);
  }

  // 将[first, last}元素移动到position之前
  void splice(iterator position, list&, iterator first, iterator last)
  {
    if (first != last)
      transfer(position, first, last);
  }

  void remove(const T& value);
  void unique();
  void merge(list& x);
  void reverse();
  void sort();

#ifdef __STL_MEMBER_TEMPLATES
  template <class Predicate> void remove_if(Predicate);
  template <class BinaryPredicate> void unique(BinaryPredicate);
  template <class StrictWeakOrdering> void merge(list&, StrictWeakOrdering);
  template <class StrictWeakOrdering> void sort(StrictWeakOrdering);
#endif /* __STL_MEMBER_TEMPLATES */

  friend bool operator== __STL_NULL_TMPL_ARGS (const list& x, const list& y);
};

// 判断两个链表是否相等
template <class T, class Alloc>
inline bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y)
{
  typedef typename list<T,Alloc>::link_type link_type;
  link_type e1 = x.node;
  link_type e2 = y.node;
  link_type n1 = (link_type) e1->next;
  link_type n2 = (link_type) e2->next;
  for ( ; n1 != e1 && n2 != e2 ;
          n1 = (link_type) n1->next, n2 = (link_type) n2->next)
    if (n1->data != n2->data)
      return false;
  return n1 == e1 && n2 == e2;
}

// 链表比较大小使用的是字典顺序
template <class T, class Alloc>
inline bool operator<(const list<T, Alloc>& x, const list<T, Alloc>& y)
{
  return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}

// 如果编译器支持模板函数特化优先级
// 那么将全局的swap实现为使用list私有的swap以提高效率
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER

template <class T, class Alloc>
inline void swap(list<T, Alloc>& x, list<T, Alloc>& y)
{
  x.swap(y);
}

#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */

// 将[first, last)区间插入到position之前
#ifdef __STL_MEMBER_TEMPLATES

template <class T, class Alloc> template <class InputIterator>
void list<T, Alloc>::insert(iterator position,
                            InputIterator first, InputIterator last)
{
  for ( ; first != last; ++first)
    insert(position, *first);
}

#else /* __STL_MEMBER_TEMPLATES */

template <class T, class Alloc>
void list<T, Alloc>::insert(iterator position, const T* first, const T* last) {
  for ( ; first != last; ++first)
    insert(position, *first);
}

template <class T, class Alloc>
void list<T, Alloc>::insert(iterator position,
                            const_iterator first, const_iterator last) {
  for ( ; first != last; ++first)
    insert(position, *first);
}

#endif /* __STL_MEMBER_TEMPLATES */

// 在position前插入n个值为x的元素
template <class T, class Alloc>
void list<T, Alloc>::insert(iterator position, size_type n, const T& x)
{
  for ( ; n > 0; --n)
    insert(position, x);
}

// 擦除[first, last)间的结点
template <class T, class Alloc>
list<T,Alloc>::iterator list<T, Alloc>::erase(iterator first, iterator last)
{
  while (first != last) erase(first++);
  return last;
}

// 重新设置容量大小
// 如果当前容量小于新容量, 则新增加值为x的元素, 使容量增加至新指定大小
// 如果当前容量大于新容量, 则析构出来的元素
template <class T, class Alloc>
void list<T, Alloc>::resize(size_type new_size, const T& x)
{
  iterator i = begin();
  size_type len = 0;
  for ( ; i != end() && len < new_size; ++i, ++len)
    ;
  if (len == new_size)
    erase(i, end());
  else                          // i == end()
    insert(end(), new_size - len, x);
}

// 销毁所有结点, 将链表置空
template <class T, class Alloc>
void list<T, Alloc>::clear()
{
  link_type cur = (link_type) node->next;
  while (cur != node) {
    link_type tmp = cur;
    cur = (link_type) cur->next;
    destroy_node(tmp);
  }
  node->next = node;
  node->prev = node;
}

// 链表赋值操作
// 如果当前容器元素少于x容器, 则析构多余元素,
// 否则将调用insert插入x中剩余的元素
template <class T, class Alloc>
list<T, Alloc>& list<T, Alloc>::operator=(const list<T, Alloc>& x)
{
  if (this != &x) {
    iterator first1 = begin();
    iterator last1 = end();
    const_iterator first2 = x.begin();
    const_iterator last2 = x.end();
    while (first1 != last1 && first2 != last2) *first1++ = *first2++;
    if (first2 == last2)
      erase(first1, last1);
    else
      insert(last1, first2, last2);
  }
  return *this;
}

// 移除特定值的所有结点
// 时间复杂度O(n)
template <class T, class Alloc>
void list<T, Alloc>::remove(const T& value)
{
  iterator first = begin();
  iterator last = end();
  while (first != last) {
    iterator next = first;
    ++next;
    if (*first == value) erase(first);
    first = next;
  }
}

// 移除容器内所有的相邻的重复结点
// 时间复杂度O(n)
// 用户自定义数据类型需要提供operator ==()重载
template <class T, class Alloc>
void list<T, Alloc>::unique()
{
  iterator first = begin();
  iterator last = end();
  if (first == last) return;
  iterator next = first;
  while (++next != last) {
    if (*first == *next)
      erase(next);
    else
      first = next;
    next = first;
  }
}

// 假设当前容器和x都已序, 保证两容器合并后仍然有序
template <class T, class Alloc>
void list<T, Alloc>::merge(list<T, Alloc>& x)
{
  iterator first1 = begin();
  iterator last1 = end();
  iterator first2 = x.begin();
  iterator last2 = x.end();
  while (first1 != last1 && first2 != last2)
    if (*first2 < *first1) {
      iterator next = first2;
      transfer(first1, first2, ++next);
      first2 = next;
    }
    else
      ++first1;
  if (first2 != last2) transfer(last1, first2, last2);
}

// 将链表倒置
// 其算法核心是历遍链表, 每次取出一个结点, 并插入到链表起始点
// 历遍完成后链表满足倒置
template <class T, class Alloc>
void list<T, Alloc>::reverse()
{
  if (node->next == node || link_type(node->next)->next == node) return;
  iterator first = begin();
  ++first;
  while (first != end()) {
    iterator old = first;
    ++first;
    transfer(begin(), old, first);
  }
}

// 按照升序排序
template <class T, class Alloc>
void list<T, Alloc>::sort()
{
  if (node->next == node || link_type(node->next)->next == node) return;
  list<T, Alloc> carry;
  list<T, Alloc> counter[64];
  int fill = 0;
  while (!empty()) {
    carry.splice(carry.begin(), *this, begin());
    int i = 0;
    while(i < fill && !counter[i].empty()) {
      counter[i].merge(carry);
      carry.swap(counter[i++]);
    }
    carry.swap(counter[i]);
    if (i == fill) ++fill;
  }

  for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1]);
  swap(counter[fill-1]);
}

#ifdef __STL_MEMBER_TEMPLATES

// 给定一个仿函数, 如果仿函数值为真则进行相应元素的移除
template <class T, class Alloc> template <class Predicate>
void list<T, Alloc>::remove_if(Predicate pred)
{
  iterator first = begin();
  iterator last = end();
  while (first != last) {
    iterator next = first;
    ++next;
    if (pred(*first)) erase(first);
    first = next;
  }
}

// 根据仿函数, 决定如何移除相邻的重复结点
template <class T, class Alloc> template <class BinaryPredicate>
void list<T, Alloc>::unique(BinaryPredicate binary_pred)
{
  iterator first = begin();
  iterator last = end();
  if (first == last) return;
  iterator next = first;
  while (++next != last) {
    if (binary_pred(*first, *next))
      erase(next);
    else
      first = next;
    next = first;
  }
}

// 假设当前容器和x均已序, 将x合并到当前容器中, 并保证在comp仿函数
// 判定下仍然有序
template <class T, class Alloc> template <class StrictWeakOrdering>
void list<T, Alloc>::merge(list<T, Alloc>& x, StrictWeakOrdering comp)
{
  iterator first1 = begin();
  iterator last1 = end();
  iterator first2 = x.begin();
  iterator last2 = x.end();
  while (first1 != last1 && first2 != last2)
    if (comp(*first2, *first1)) {
      iterator next = first2;
      transfer(first1, first2, ++next);
      first2 = next;
    }
    else
      ++first1;
  if (first2 != last2) transfer(last1, first2, last2);
}

// 根据仿函数comp据定如何排序
template <class T, class Alloc> template <class StrictWeakOrdering>
void list<T, Alloc>::sort(StrictWeakOrdering comp)
{
  if (node->next == node || link_type(node->next)->next == node) return;
  list<T, Alloc> carry;
  list<T, Alloc> counter[64];
  int fill = 0;
  while (!empty()) {
    carry.splice(carry.begin(), *this, begin());
    int i = 0;
    while(i < fill && !counter[i].empty()) {
      counter[i].merge(carry, comp);
      carry.swap(counter[i++]);
    }
    carry.swap(counter[i]);
    if (i == fill) ++fill;
  }

  for (int i = 1; i < fill; ++i) counter[i].merge(counter[i-1], comp);
  swap(counter[fill-1]);
}

#endif /* __STL_MEMBER_TEMPLATES */

#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1174
#endif

__STL_END_NAMESPACE

#endif /* __SGI_STL_INTERNAL_LIST_H */

// Local Variables:
// mode:C++
// End:

本博文主要查考《STL源码剖析》,冰洁对List是做了粗浅的解析,如有问题,欢迎留言。



SAP-PLM传输MBOM传输至SAP BOM 项目文本 (行 2)PLM创建及修改BOM接口。PLM 创建BOM接口抬头。BOM 项目文本(行1)PLM 创建BOM接口抬头。PLM BOM 错误消息。PLM BOM接口组件。组件数量 (BTCI) 阅读详情

相关推荐

LeGO-LOAM运行kitti数据集

文章目录1. kitti-lego-loam2. 改动3. 实验结果4. evo轨迹测评 1. kitti-lego-loam 链接:kitti-lego-loam kitti-lego-loam提供了将kitti序列转化成rosbag的代码,在kittibag中. 需要注意下数据文件的存放顺序,以00序列为例: datafolder          |____results          &

Zlp19970106的博客 8705

STL之list实现详解

STL中的list容器底层数据结构用的是双向链表。这样数据在存储和删除的时候都可以做到时间复杂度是O(1)。但是由于其存储空间并不是连续的,所以导致在里面查找数据会很不容易。 1、介绍list如何使用的demo #include &lt;iostream&gt; #include &lt;list&gt; #include &lt;algorithm&gt; #include &lt;...

一个菜鸟的博客 1688

海康安防平台接口调试指南:从签名生成到Vue项目集成

本文详细解析了海康安防平台接口认证规则,从签名生成到Vue项目集成的完整流程。通过核心机制解析、签名工具使用技巧及Vue项目中的优雅集成方案,帮助开发者高效完成接口调试,避免常见问题。特别适合需要将海康安防平台接口集成到Vue项目中的开发者参考。

weixin_42533120的博客 439

STL(三)——list

list介绍 STL的list是一个双向链表,插入删除的复杂度为O(1),不像vector,list是有多少元素使用多少空间,不会存在浪费的情况。   list细节: 1. list的迭代器不能像vector一样,以普通指针作为迭代器,因为双向链表的迭代器是双向迭代器,不是随机访问迭代器 2. 和vector不一样,list的插入、删除等操作不会造成其他迭代器失效(但被操作的迭代器可能失...

Pokemon_Master的博客 342

北京途游游戏后端开发面经

北京途游游戏后端开发面经 我面试岗位是基于Python的后端开发工程师,主要是搞游戏的服务器开发。面试一共分了两轮,之后说是看看其他的候选人再定,感觉凉凉~~ 由于受到此次新型冠状病毒疫情的影响,本次面试采用视频面试的方式,下面是面试的时候面试官提的问题,python的基础知识还是问的比较多的。  一面:(32分钟微信视频)  简单做下自我介绍,   1.说一下你开发过的项目,挑一个说下你都做了...

weixin_45603043的博客 2128

新手也能懂的Simulink汽车二自由度建模:从公式推导到模型搭建全流程

本文为新手提供了在Simulink中搭建汽车二自由度模型的完整实战指南。文章从模型核心概念讲起,详细讲解了如何将车辆动力学方程转化为状态空间模型,并逐步指导在Simulink中实现从参数设置、模块搭建到仿真验证的全过程。通过阶跃转向测试和参数影响分析,帮助读者直观理解车辆转向特性,并提供了实用的模型扩展思路与常见问题排查技巧。

weixin_29047919的博客 215

STL之四:list用法详解

list容器介绍 相对于vector容器的连续线性空间,list是一个双向链表,它有一个重要性质:插入操作和删除操作都不会造成原有的list迭代器失效,每次插入或删除一个元素就配置或释放一个元素空间。也就是说,对于任何位置的元素插入或删除,list永远是常数时间。 常用函数 (1)    构造函数 list c:创建一个空的list list c1(c2):复制另一个同类型元素的list

longshengguoji的专栏 2万+

PPT高清导出秘籍:如何轻松设置600dpi图片分辨率

本文详细解析了PPT导出图片模糊的根本原因,并提供了通过修改Windows注册表将导出分辨率从默认的96dpi提升至600dpi的完整教程。该方法能显著提升图片清晰度,满足学术出版与高质量印刷需求,文中还包含效果对比、进阶设置及常见问题解答。

a0b1c2d3的专栏 1319

STL源码剖析 容器 stl_map.h

map -------------------------------------------------------------------------------- 所有元素都会根据元素的键值自动被排序。 map的所有元素都是 pair,同时拥有实值和键值。 不可以修改元素的键值,因为它关系到 map 元素的排列规则 可以修改元素的实值,因为它不影响 map 的排列规则 map iterator 既不是一种 constant iterators , 也不是一种 mutable iterator 标准

1394

Windows 10下Carla 0.9.15安装避坑指南:从下载到运行的全流程解析

本文详细解析了在Windows 10系统上安装和运行Carla 0.9.15自动驾驶仿真平台的全流程。内容涵盖从系统环境准备、资源下载、Python依赖安装,到首次启动验证及常见错误(如DLL缺失、显卡驱动问题)的深度解决方案,旨在帮助用户快速搭建稳定的开发环境,并运行第一个自动驾驶Python脚本。

alice7model的博客 547

在 AS ABAP 7.40 SP05 里把 HANA 存储过程纳入 ABAP 生命周期:AMDP 实战示例与踩坑笔记

文章摘要:AMDP(ABAP Managed Database Procedures)是SAP推出的框架,用于在ABAP中管理数据库端的存储过程或函数,特别适用于HANA数据库。它通过将数据库语言(如SQLScript)嵌入ABAP方法,实现数据处理逻辑的"下推"(code pushdown),减少应用层与数据库间的数据传输。AMDP适用于需要数据库特有能力或大规模数据处理的场景,但应优先考虑Open SQL或ABAP CDS等更简单的方案。开发时需注意数据库类型检查、参数前缀(:)等语

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 225

STL源码剖析 [容器](二十一)[stl_hash_map.h]

hash_map基于hash table(哈希表)。 哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的 情况下,用空间换时间的做法是值得的。另外,编码比较容易也是它的特点之一。 其基本原理是:使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数,也叫做散列函数),使得每个元素的关

深藏功与名 1438

建行报专版报道建设银行金融科技发展历程

经过十多年的建设,总行前中后台部门都有了自己的业务系统,但系统“部门”特征显著,渠道、客户、产品的横向整合不足,产品系统层次化、结构化、参数化程度不高,渠道支持产品快速部署以及跨渠道服务能力不足,管理分析类系统建设相对滞后,系统较弱的可扩展能力在应对高速增长的交易量时已疲态尽显,同时安全稳定运行的压力和风险日益积聚。站在新的历史起点上,科技条线将聚焦主责主业,完善信息科技管理体系,紧紧围绕安全生产中心任务,持续推动技术能力复用共享和自主可控,为全行做好金融五篇大文章,推动业务高质量发展提供技术支撑。

银行信息系统应用架构详解 386

STL之list函数解析

STL之list函数解析 list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。 使用list容器之前必须加上头文件:#include; list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespa...

乐褀的博客 461

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【C++】STL中List的详细实现解析

在 C++ STL(标准模板库)中,List是一个带头双向链表,可以存储多个元素并且支持动态调整大小,适合频繁插入和删除操作;而Vector是一个动态数组,元素在内存中是连续存储的,适合需要快速随机访问的场景。List提供了添加、删除、查找等操作,而Vector除了这些基本操作外,还提供了按索引访问元素、在指定位置插入元素等功能,进而就可以很好的支持排序算法,二分查找,堆算法等,它的缺点是扩容要付出一定的代价,而且除了尾上的插入和删除外其他位置的插入和删除都不快(因为要挪动数据)。

Colorful___的博客 1707

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

STL解析——list的使用

STL容器中提供的list容器也是一种顺序容器,底层实现方式是带头双向链表,这种实现方式能比单链表更高效的访问数据。下面围绕部分重要接口的使用展开讲解。

Chen_xu3207的博客 1328

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

STL中list的详细解析

list详细全解!!

m0_69005269的博客 754

C++ STL_PJ 源码 list 原理解析

C++ STL_PJ 源码 list 原理解析 (list 必要知识) 了解:C++ list 是属于双向循环链表,就是 “双向链表” 与 “循环链表” 的结合体。所以我们必须对 “双向链表” 与 “循环链表” 了解必要的知识。下篇我们就解析 C++ list容器的奥秘吧! 双向链表 1. 双向链表的结构: 表示的链表的 “前驱” (Prev),存储地址 表示的链表存储的 “值的部分” (...

qq_42766129的博客 444

STL list深度解析:从原理到手写实现

STL list深度解析:从原理到手写实现 本文深入剖析STL list的核心原理与实现细节。首先分析list作为双向循环带头链表的优势,能够实现O(1)时间复杂度的任意位置插入/删除操作。文章详细介绍了list的关键接口及其应用场景,并重点讲解了手写实现方案: 定义节点结构list_node,包含数据域和前后指针 实现迭代器list_iterator,封装指针操作,支持前进后退等操作 构建list类,管理头节点并提供增删改查等接口 通过对比vector,突出list在频繁插入删除场景下的性能优势,帮助开发

2403_87581574的博客 746

深入解析C++ STL链表(List)模拟实现

本文详细介绍了C++中list容器的实现原理,重点分析了三个核心类的设计:结点类、迭代器类和list类本身。结点类采用双向链表结构,包含数据域和前后指针;迭代器类通过封装结点指针并重载运算符,模拟了指针行为;list类实现了带头双向循环链表结构,提供了完整的容器接口。文章通过对比vector和list的特性,阐述了list在插入删除操作中迭代器不失效的优势,并给出了完整的测试验证代码。最后深入分析了list插入不失效、删除仅当前失效的底层机制,与vector的内存管理差异形成鲜明对比。

祯的博客 1072

C++ STL list容器深度解析:从入门到实战

C++ STL list容器深度解析:从入门到实战

嵌入式小程同学的博客 928

STL List::sort() 解析

看侯捷翻译那本《STL源码剖析》中list内置sort的算法,书中注释说是quick sort,看了半天没看明白, 然后就把代码敲出来运行看看,上面的源码如下: template template void list::sort() { if (node->next == node || link_type(node->next)->next == node

lijun5635的专栏 1万+

STL源码解析之list(1)

T data;STL list是一个双向链表,next指向下一个节点,prev指向前一个节点。

taxue4485的专栏 332

深度解析 STL 中 list 的原理

list作为 STL 中的序列式容器,凭借双向链表的数据结构,在插入和删除操作上展现出高效性,尤其适合处理需要频繁进行这些操作的场景。虽然它在内存使用上相对复杂,且不支持随机访问,但在特定应用中具有独特的优势。深入理解list的原理,包括其数据结构、内存管理、插入删除操作以及迭代器的使用,有助于开发者在实际编程中根据需求选择合适的容器,优化代码性能。

AC的博客 947

C++ STL 详解 ——list 的深度解析与实践指南

list提供了多种灵活的定义方式。可以构造一个空容器,如,用于后续动态添加元素。也能创建一个包含指定数量且值相同的元素的容器,像,这里的lt2就包含了 10 个值为 2 的元素。通过拷贝构造,能复制已有容器的内容,将lt2的内容复制到lt3中。此外,还可以利用迭代器或数组区间来初始化list// 打印各个list的内容return 0;

LJY_CF的博客 1801

深入解析STL list实现与手写MiniList教程

链表作为基础数据结构,通过节点间的指针链接实现动态内存管理。STL中的list容器采用双向环形链表设计,配合哨兵节点实现O(1)复杂度的首尾操作。其迭代器通过运算符重载模拟指针行为,使非连续存储的链表能适配STL算法体系。在需要频繁插入删除的场景下,list相比vector具有明显性能优势。本文以C++ STL list为例,剖析其节点设计、内存管理和异常安全机制,并通过实现简化版MiniList演示核心实现技术,帮助开发者深入理解STL容器的设计哲学与工程实践。

weixin_33947521的博客 307
上一篇: 关于vector解析
下一篇: 常见的及Linux中的进程调度算法
Csoul89757
博客等级 码龄10年 34粉丝 72原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值