#include <list>

clear();删除向量中的所有对象

erase(iterator it);删除it所指向的容器对象

insert(iterator it,const T&);向it所指的向量位置前插入一个对象

push_back(const T&);向向量尾部插入一个对象

push_front(const T&);向向量头部插入一个对象

remove(const T&);删除特定的对象

sort();排序

unique();唯一。把链表中前后有相同元素的结点删除

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     std::list<int>mylist;
 7
 8     mylist.push_back(1);//向向量尾部插入一个对象
 9     mylist.push_back(2);
10     mylist.push_back(3);
11     mylist.push_back(4);
12
13     auto ibegin = mylist.begin();//迭代器
14     auto iend = mylist.end();
15
16     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
17     {
18         std::cout << *ibegin << std::endl;
19     }
20 }

对特定元素进行删除,erase实现,先查找,后删除

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     std::list<int>mylist;
 7
 8     mylist.push_back(1);//向向量尾部插入一个对象
 9     mylist.push_back(2);
10     mylist.push_back(3);
11     mylist.push_back(4);
12
13     auto ibegin = mylist.begin();//迭代器
14     auto iend = mylist.end();
15
16     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
17     {
18         if (*ibegin == 3)
19         {
20             mylist.erase(ibegin);
21             break;//删除以后,一定要break,因为ibegin已经变更了
22         }
23     }
24
25     ibegin = mylist.begin();//迭代器
26     iend = mylist.end();
27
28     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
29     {
30         std::cout << *ibegin << std::endl;
31     }
32 }

对特定元素进行删除,remove实现,先查找,后删除

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     int a[5] = { 1,2,3,4,5 };
 7     std::list<int>mylist(a, a + 5);
 8
 9     mylist.push_front(12);//向向量头部插入一个对象
10
11     auto ibegin = mylist.begin();//迭代器
12     auto iend = mylist.end();
13
14     mylist.remove(3);
15
16     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
17     {
18         std::cout << *ibegin << std::endl;
19     }
20 }

在特定元素前插入新的元素,先查找,后插入

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     int a[5] = { 1,2,3,4,5 };
 7     std::list<int>mylist(a, a + 5);
 8
 9     mylist.push_front(12);//向向量头部插入一个对象
10
11     auto ibegin = mylist.begin();//迭代器
12     auto iend = mylist.end();
13
14     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
15     {
16         if (*ibegin == 3)
17         {
18             mylist.insert(ibegin, 30);
19             break;
20         }
21     }
22
23     ibegin = mylist.begin();//迭代器
24     iend = mylist.end();
25
26     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
27     {
28         std::cout << *ibegin << std::endl;
29     }
30 }

使用反向迭代器进行遍历

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     int a[5] = { 1,2,3,4,5 };
 7     std::list<int>mylist(a, a + 5);
 8
 9     auto rb = mylist.rbegin();//反向迭代器
10     auto re = mylist.rend();
11
12     for (; rb != re; rb++)//使用迭代器进行遍历
13     {
14         std::cout << *rb << std::endl;
15     }
16 }

两个链表进行合并

必须先排序,后合并,否则会出错

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     int a[5] = { 1,2,3,104,5 };
 7     std::list<int>mylist1(a, a + 5);
 8
 9     int b[5] = { 11,22,33,44,55 };
10     std::list<int>mylist2(b, b + 5);
11
12     mylist1.sort();//排序
13     mylist2.sort();
14
15     mylist1.merge(mylist2);//合并。必须先排序,后合并,否则会出错
16
17     auto ibegin = mylist1.begin();//迭代器
18     auto iend = mylist1.end();
19
20     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
21     {
22         std::cout << *ibegin << std::endl;
23     }
24 }

链表元素的唯一

必须先排序,后唯一

 1 #include <iostream>
 2 #include <list>
 3
 4 void main()
 5 {
 6     int a[6] = { 1,2,98,104,5,98 };
 7     std::list<int>mylist1(a, a + 6);
 8
 9     mylist1.sort();//排序
10     mylist1.unique();//唯一。必须先排序,后唯一
11
12     auto ibegin = mylist1.begin();//迭代器
13     auto iend = mylist1.end();
14
15     for (; ibegin != iend; ibegin++)//使用迭代器进行遍历
16     {
17         std::cout << *ibegin << std::endl;
18     }
19 }
时间: 2024-08-27 16:14:16

#include <list>的相关文章

#include &lt;mutex&gt;

多线程初级 1 #include <iostream> 2 #include <thread> 3 #include <windows.h> 4 #include <mutex> 5 6 //两个线程并行访问一个变量 7 8 int g_num = 20;//找到或者找不到的标识 9 std::mutex g_mutex; 10 11 void goA(int num) 12 { 13 g_mutex.lock();//你访问的变量,在你访问期间,别人访问不

[C++11 并发编程] 08 - Mutex std::unique_lock

相对于std::lock_guard来说,std::unique_lock更加灵活,std::unique_lock不拥有与其关联的mutex.构造函数的第二个参数可以指定为std::defer_lock,这样表示在构造unique_lock时,传入的mutex保持unlock状态.然后通过调用std::unique_lock对象的lock()方法或者将将std::unique_lock对象传入std::lock()方法来锁定mutex. #include <mutex> class some

[C++11 并发编程] 06 Mutex race condition

上一节中介绍了mutex的基本使用方法,使用mutex来保护共享数据并不能解决race condition带来的问题,假如我们有一个堆栈数据结构类似于std::stack它提供了5个基本操作push(),pop(),top(),empty(),和size().这里的top()操作返回栈顶元素的拷贝,这样我们就可以使用一个mutex来保护栈内部的数据.但是race codition情况下,虽然使用mutex在stack的每个接口内都对共享数据进行了保护,仍然有问题存在. #include <deq

数据共享之互斥量mutex

互斥量介绍 互斥量可以保护某些代码只能有一个线程运行这些代码.如果有个线程使用互斥量执行某些代码,其他线程访问是会被阻塞,直到这个线程执行完这些代码,其他线程才可以执行. 一个线程在访问共享数据前,给互斥量上锁,这时其他线程再给互斥量上锁会阻塞直到这个线程给互斥量解锁. 互斥量是C++中最常用的数据保护机制,但是它也不万能的.在编写代码时,合理的组织代码来避免资源竞争非常重要.使用互斥量可能会带来其他问题,比如死锁. 在C++中使用互斥量 创建互斥量使用mutex,给互斥量上锁使用函数lock(

c++11多线程学习笔记之二 mutex使用

// 1111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <thread> #include <mutex> int gcounter = 0; std::mutex gmtx; std::mutex gmtxOutput; void Increases() { for (int i = 0; i<10000; ++i) { if (g

mutex和CRITICAL_SECTION,互斥和临界区

本文不没有任何知识可讲,只是帖上自己测试的结果. 想看底层原理的可以直接关闭. 不过对于急着要选方案的人,倒提供一些帮助. 先说一些无关紧要的废话: ==================================================================================================================================================== 先说说为什么会有这篇文章. 我在做练习的时候,参考一

关于#include &lt;bits/stdc++.h&gt;

经常看人写#include <bits/stdc++.h>却不知道是干啥的? #include<bits/stdc++.h>包含了目前c++所包含的所有头文件 对比: #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include &l

[C++11 并发编程] 05 Mutex 基本操作

Mutex是C++中最常见的数据保护机制之一,在访问一块共享数据前,lock mutex,在完成对数据的访问后,unlock mutex.线程库当一个特定mutex被某个线程lock后,其它尝试lock同一个mutex的线程都会被挂起指导这个mutex被unlock.这就保证了所有线程看到的数据都是完整的,不会被修改了一部分的数据. 在C++中,通常我们通过创建std::mutex的实例来获得一个mutex,调用它的成员函数lock()来锁住它,调用unlock来解锁,但实际实现时,并不推荐这么

漫话C++0x(五)—- thread, mutex, condition_variable

熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不同平台上各自提供的api,这样带来的问题就是程序的跨平台移植性比较差,经常要用一大堆的#ifdef WIN32类似的宏来区分不同的平台,搞得程序很难看.C++0x最原始的初衷之一就是为了让C++的功能更加强大,更加方便使用.现如今硬件如此发达,concurrency在程序设计中已经是司空见惯的事情了

code::blocks配置头文件#include&lt;bits/stdc++.h&gt;

万用头文件#include<bits/stdc++.h>,包含了<queue>,<vector>,<string>,<algorithm>,<iostream>等等各个竞赛中常用或不常用的头文件,可谓省时省力,竞速必备.如今已被大部分oj所支持.面试时大部分的判题系统也亲测可用.但有的编译器却不能够使用这个头文件.本文以code::blocks为例,教大家配置此头文件 1.寻找code::blocks的gnu gcc编译环境 sett