小工具知识网

当前位置: 首页 > 行业知识 > C++ 基础专题容器(list)

行业知识

C++ 基础专题容器(list)

2026-11-07 12:51:09 来源:互联网转载

前言

��       本文主要是总结常用容器,加深理解以及实际使用。相关完整网站参考:C++函数和容器网站

本文主要是关注C++11中的定义和用法。

list

一、类和定义

        template class list;

List containers are implemented as doubly-linked lists。(双向链表为list的底层原理)

Compared to other base standard sequence containers (array, vector and deque), lists perform generally better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained, and therefore also in algorithms that make intensive use of these, like sorting algorithms。(与其他基本标准序列容器(array、vector和deque)相比,列表在容器内的任何位置插入、提取和移动元素(迭代器已经获得)方面通常表现更好,因此在大量使用列表的算法(如排序算法)中也表现更好。)

        list优点是方便插入、访问和移动,缺点是访问指定的某个位置直接访问元素。

1.1 特性

        线性顺序排列;双向链表可以知道前序和后序,但不支持随机访问;动态申请内存。

1.2 定义

二、 使用

2.1 成员函数

构造函数和使用

default (1)explicit list (const allocator_type& alloc = allocator_type());fill (2)explicit list (size_type n);         list (size_type n, const value_type& val,                const allocator_type& alloc = allocator_type());range (3)template   list (InputIterator first, InputIterator last,         const allocator_type& alloc = allocator_type());copy (4)list (const list& x);list (const list& x, const allocator_type& alloc);move (5)list (list&& x);list (list&& x, const allocator_type& alloc);initializer list (6)list (initializer_list il,       const allocator_type& alloc = allocator_type());
//list新建对象void ListConstructor(){list list_a;list list_b(4, 100);list list_c(list_b.begin(), list_b.end());list list_d(list_c);//使用数组初始化int arry_a[] = { 1,2,3,4 };list list_e(arry_a,arry_a+sizeof(arry_a)/sizeof(int));cout 

上一篇:服务器怎么安装php程序吗?

下一篇:Vue3+Vite+Axios Request 请求封装(TS版本)最新