`

C++ STL学习之list

 
阅读更多

LIST 是一个双向链表。

因为本人的C++的编程时间有限,目前还没怎么用到list,在数据结构中,list是一个比较重要的。

首先复习一下什么是双向链表。双向链表是一种每个节点都有两个指针,分别直接的指向了直接前驱和直接后驱。这种方式对访问一个节点的前后都是十分方便快捷的。向前向后搜索的时间都是为常量时间。

并且在链表的头和尾部插入元素的时间都是常量时间。

List有以下优点:

  • Efficient insertion and removal of elements anywhere in the container (constant time).
  • Efficient moving elements and block of elements within the container or even between different containers (constant time).
  • Iterating over the elements in forward or reverse order (linear time).

C++ stl中list提供了两个参数


Where the template parameters have the following meanings:

  • T: 数据类型
  • Allocator: Type of the allocator object used to define the storage allocation model. By default, theallocator class template for type T is used, which defines the simplest memory allocation model and is value-independent.

In the reference for the list member functions, these same names are assumed for the template parameters.

Public base classes

None.

list成员函数 ,描述就不翻译了,保持原汁原味。

Member Where defined Description
value_type Container The type of object, T, stored in the list.
pointer Container Pointer to T.
reference Container Reference to T
const_reference Container Const reference to T
size_type Container An unsigned integral type.
difference_type Container A signed integral type.
iterator Container Iterator used to iterate through a list.
const_iterator Container Const iterator used to iterate through a list.
reverse_iterator Reversible Container Iterator used to iterate backwards through a list.
const_reverse_iterator Reversible Container Const iterator used to iterate backwards through a list.
iterator begin() Container Returns an iterator pointing to the beginning of the list.
iterator end() Container Returns an iterator pointing to the end of the list.
const_iterator begin() const Container Returns a const_iterator pointing to the beginning of thelist.
const_iterator end() const Container Returns a const_iterator pointing to the end of the list.
reverse_iterator rbegin() Reversible Container Returns a reverse_iterator pointing to the beginning of the reversed list.
reverse_iterator rend() Reversible Container Returns a reverse_iterator pointing to the end of the reversed list.
const_reverse_iterator rbegin() const Reversible Container Returns a const_reverse_iterator pointing to the beginning of the reversed list.
const_reverse_iterator rend() const Reversible Container Returns a const_reverse_iterator pointing to the end of the reversed list.
size_type size() const Container Returns the size of the list. Note: you should not assume that this function is constant time. It is permitted to beO(N), where N is the number of elements in the list. If you wish to test whether alist is empty, you should write L.empty() rather than L.size() == 0.
size_type max_size() const Container Returns the largest possible size of the list.
bool empty() const Container true if the list's size is 0.
list() Container Creates an empty list.
list(size_type n) Sequence Creates a list with n elements, each of which is a copy ofT().
list(size_type n, const T& t) Sequence Creates a list with n copies of t.
list(const list&) Container The copy constructor.
template <class InputIterator>
list(InputIterator f, InputIterator l)
[2]
Sequence Creates a list with a copy of a range.
~list() Container The destructor.
list& operator=(const list&) Container The assignment operator
reference front() Front Insertion Sequence Returns the first element.
const_reference front() const Front Insertion Sequence Returns the first element.
reference back() Sequence Returns the last element.
const_reference back() const Back Insertion Sequence Returns the last element.
void push_front(const T&) Front Insertion Sequence Inserts a new element at the beginning.
void push_back(const T&) Back Insertion Sequence Inserts a new element at the end.
void pop_front() Front Insertion Sequence Removes the first element.
void pop_back() Back Insertion Sequence Removes the last element.
void swap(list&) Container Swaps the contents of two lists.
iterator insert(iterator pos, const T& x) Sequence Inserts x before pos.
template <class InputIterator>
void insert(iterator pos, 
            InputIterator f, 
            InputIterator l)
[2]
Sequence Inserts the range [f, l) before pos.
void insert(iterator pos, 
            size_type n, const T& x)
Sequence Inserts n copies of x before pos.
iterator erase(iterator pos) Sequence Erases the element at position pos.
iterator erase(iterator first, iterator last) Sequence Erases the range [first, last)
void clear() Sequence Erases all of the elements.
void resize(n, t = T()) Sequence Inserts or erases elements at the end such that the size becomesn.
void splice(iterator pos, list& L) list See below.
void splice(iterator pos, 
            list& L,
            iterator i)
list See below.
void splice(iterator pos,
            list& L, 
            iterator f, iterator l)
list See below.
void remove(const T& value) list See below.
void unique() list See below.
void merge(list& L) list See below.
void sort() list See below.
bool operator==(const list&, 
                const list&)
Forward Container Tests two lists for equality. This is a global function, not a member function.
bool operator<(const list&, 
               const list&)
Forward Container Lexicographical comparison. This is a global function, not a member function.

在这里,着重强调一下list的构造函数,内部形式如下


下面一个例子:


今天到此结束,准备准备打算自己用C++实现数据结构敬请期待

更多内容:http://blog.csdn.net/wallwind

分享到:
评论

相关推荐

    c++ stl list总结

    c++ stl list总结

    C++ STL list操作

    本文档展示了STL List的基本操作方法,希望对学C++的人有些帮助

    C++ STL 参考手册Cpp_STL_ReferenceManual.pdf

    STL 是 C++ 标准库的一部分,不用单独安装。 C++ 对模板(Template)支持得很好,STL 就是借助模板把常用的数据结构及其算法都实现了一遍,并且做到了数据结构和算法的分离。例如,vector 的底层为顺序表(数组),...

    [C++][STL] C++ STL 之 List

    NULL 博文链接:https://jacky-dai.iteye.com/blog/692604

    c模拟c++ stl list

    实现c++ list部分功能,内存自管理 函数的使用和c++ stl list类似,并不完全相同 简单测试过,并不保证完全无错,仅供参考

    C++ STL list 遍历删除出错解决方案

    主要介绍了C++ STL list 遍历删除出错解决方案的相关资料,这里对出错进行分析,并给出正确的解决方法,需要的朋友可以参考下

    C++实现STL容器之List

    C++实现STL容器之List

    C++STL 一些实例

    一些C++STL 用法的实例。如vector list.

    c++ stl list实现简单的学生信息管理系统

    c++ stl list实现简单的学生信息管理系统

    C++ STL 开发技术导引(随书源码)

    第二篇 C++STL泛化技术基础 第4章 C++STL泛型库概述 第5章 C++STL泛化技术分析 第三篇 C++STL容器技术 第7章 deque双端队列容器 第8章 list双向链表容器 第9章 slist单向链表容器 第10章 bit_vector位向量容器 第11...

    C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示

    C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示

    C++ STL 开发技术导引(第6章)

    4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...

    心希盼 c++ STL Stack(栈)

    心希盼 c++ STL Stack(栈) 包含了用List和Vector来实现的Stack 详细说明请看“心希盼 Stack.doc”

    C++ STL 中文版

    迭代器,utility,iterator,memory,算法,algorithm,numeric,functional,vector,list,deque,set,map,stack,queue逐章介绍

    C++ STL开发技术导引(第5章)

    4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...

    C++ STL vector 容器介绍

    该文档详细讲解了C++中标准容器的使用,是一份不错的学习资料哦

    C++_STL范例大全_教程

    C++_STL范例大全_教程,主要讲STL的容器部分,对初学者有很大的帮助。里面有源码文件。 Vector、 Deque、List、Set等容器。

    STL学习总结

    关于STL学习的总结: STL就是Standard Template Library,标准...每一个C++程序员都应该好好学习STL。大体上包括container(容器)、algorithm(算法)和iterator(迭代器),容器和算法通过迭代器可以进行无缝连接。

    C++_STL_示例

    C++ STL_示例word 格式 提供例程 string vector list等数据结构示例 #include #include using namespace std; void main() { //用const char * 构造strText对象 string strText("This is a test"); //在strText...

    SGI STL list相关代码

    SGI STL list相关代码

Global site tag (gtag.js) - Google Analytics