C++之std::list

概念说明:

List是由双链表实现的一个容器,每个节点存储一个元素,支持前后两种移动方向。List的内存随着添加的节点增加而增加。数据在内存上存储是不连续的。

1、构造与赋值。

list<int> nlist;//一个空的list

list<int> nlist1(5, 3);//list大小为5,初始化的值为3

list<int> nlist2(nlist1);//拷贝构造函数

list<int> nlis3(9);//9个元素,默认为0

list<int> nlis4(nlist2.begin(), nlist2.end());//区间拷贝赋值

list<int> nlis5 = nlis4;//重载等号赋值

auto itr = nlis5.begin();

*itr = 10;//单个元素修改赋值

2、访问方式。

通过迭代器访问:

for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)

cout << *it << " ";

3、添加

nlist.push_back(20);//尾部添加

nlist.push_front(21);//头部添加

nlist.insert(nlist.end(),30);//在指定位置插入一个数据

nlist.insert(nlist.end(), nlist.begin(), nlist.end());//在指定数据插入区间数据,这相当复制了一份

4、删除

nlist.remove(21);//移除数据为21的节点

nlist.erase(nlist.begin());//移除itr=begin的节点

auto firt = nlist.begin();

firt++;

nlist.erase(firt, nlist.end());//删除区间数据

5、迭代器

begin()// 返回指向容器中第一个元素的双向迭代器。

end() // 返回指向容器中最后一个元素所在位置的下一个位置的双向迭代器。

rbegin()// 返回指向最后一个元素的反向双向迭代器。

rend()// 返回指向第一个元素所在位置前一个位置的反向双向迭代器。

cbegin() // 和 begin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

cend()//和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改素。

crbegin() // 和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

crend()// 和 rend() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。

6、其他函数

  1. resize(num,elem);//重置大小,若num小于目前大小,则删除后面数据,若大于目前大小,则扩大并且初始值elem.
  2. nlist.swap(nlis5);//两个容器交换
  3. nlist.reverse();//翻转数据
  4. nlist.sort();//排序,默认从小到大
  5. nlist.merge(nlis5);//合并,两个容器必须是有序的,不然会崩溃
  6. auto iter = std::find(nlist.begin(), nlist.end(), 80);//查找元素,返回迭代器

7、优缺点

优点:高效的插入和删除元素。采用动态内存,不会造成内存浪费和溢出。插入操作和删除操作都不会造成原有list 迭代器的失效,这在vector是不成立的

缺点:遍历速度没有数组容器大快,并且占用空间较大(因为链表的指针)。不支持下标访问。

8、例子程序

#include <iostream>

#include<list>

#include<algorithm>

using namespace std;

void printList(const list<int>& L)

{

for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)

{

cout << *it << " ";

}

cout << endl;

}

int main()

{

list<int> nlist;//一个空的list

list<int> nlist1(5, 3);//list大小为5,初始化的值为3

list<int> nlist2(nlist1);//拷贝构造函数

list<int> nlis3(9);//9个元素,默认为0

list<int> nlis4(nlist2.begin(), nlist2.end());//区间拷贝赋值

list<int> nlis5 = nlis4;//重载等号赋值

auto itr = nlis5.begin();

*itr = 10;//单个元素修改赋值

nlist.push_back(20);//尾部添加

nlist.push_front(21);//头部添加

nlist.insert(nlist.end(),30);//在指定位置插入一个数据

nlist.insert(nlist.end(), nlist.begin(), nlist.end());//在指定数据插入区间数据,这里相当复制了一份

cout << "添加操作" << endl;;

printList(nlist);

nlist.remove(21);//移除数据为21的节点

nlist.erase(nlist.begin());//移除itr=begin的节点

auto firt = nlist.begin();

firt++;

nlist.erase(firt, nlist.end());//删除区间数据

cout << "删除操作" << endl;;

printList(nlist);

nlist.clear();

for (int i = 5; i < 16; i++)

nlist.push_back(i);

nlist.resize(10, 33);//重置大小

nlist.reverse();//翻转数据

cout << "翻转" << endl;;

printList(nlist);

nlist.swap(nlis5);//两个容器交换

nlist.sort();//排序,默认从小到大

cout << "排序" << endl;;

printList(nlist);

nlis5.sort();

nlist.merge(nlis5);//合并,两个容器必须是有序的,不然会崩溃

auto iter = std::find(nlist.begin(), nlist.end(), 80);//查找元素,返回迭代器

printList(nlist);

getchar();

}

相关推荐
D_evil__5 小时前
【Effective Modern C++】第三章 转向现代C++:16. 让const成员函数线程安全
c++
Queenie_Charlie7 小时前
前缀和的前缀和
数据结构·c++·树状数组
kokunka8 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏
John_ToDebug10 小时前
浏览器内核崩溃深度分析:从 MiniDump 堆栈到 BindOnce UAF 机制(未完待续...)
c++·chrome·windows
txinyu的博客11 小时前
解析muduo源码之 SocketsOps.h & SocketsOps.cc
c++
ctyshr11 小时前
C++编译期数学计算
开发语言·c++·算法
浪客灿心11 小时前
list_stack_queue
数据结构·list
努力写代码的熊大11 小时前
c++异常和智能指针
java·开发语言·c++
John_ToDebug11 小时前
WebContent 与 WebView:深入解析浏览器渲染架构的双层设计
c++·chrome·ui
千秋乐。11 小时前
C++-string
开发语言·c++