【STL基础】list

list

构造函数:

//default:
list<T> l;            //空的list//fill:
list<T> l(n);                //n个元素, 元素默认初始化
list<T> l(n, value);        //n个元素值为value//range:
list<T> l(first, last);     //两个迭代器之间的元素构成
list<T> l(arr, arr + sizeof(arr) / sizeof(T));    //由内置数组构造//copy:
list<T> l(const list<T> &t);        //v是u的拷贝//move:
list<T> l(list<T> &&x);    //x是右值引用(只能引用右值,如list<int> &&x = {1,2,3};)//initializer list:
list<T> l{value1, value2...};

赋值与swap:

l1 = l2;
l1 = { 1, 2, 3 };
l1.swap(l2);
swap(l1, l2);

大小:

size_type l.size() const noexcept;    //元素数目
size_type l.max_size() const noexcept;    //可容纳元素的最大数目 
bool l.empty()    //是否为空
l.resize(n);        
l.resize(n, value); 

获取元素:

l.front();    //首元素
l.back();    //尾元素

修改:

//assign
l.assign(n, value);    //将v置为n个值为value的元素
l.assign(first, last);    //用t的两个迭代器之间的值为l赋值,左闭右开 t可以是vector、array、list、forward_list、deque、set、unordered_set、multiset、unordered_multiset等。 元素的顺序和重复性由传入的容器类型性质决定
l.assign(begin(t), end(t));       //与上条语句类似,除上述类型,还支持内置数组类型
l.assign(arr, arr + n);    //将数组中的一部分赋给l
l.assign({value1, value2...});    //列表

l.push_back(value);    //尾部插入一个元素
l.push_front(value);    //首部插入一个元素
l.pop_back();         //删除最后一个元素
l.pop_front();        //删除第一个元素//insert
l.insert(it, value);    //迭代器指向的位置插入值为value的元素
l.insert(it, n, value);    //迭代器指向的位置插入n个值为value的元素
l.insert(it, first, last);    //迭代器it指向的位置插入另一个容器的两个迭代l之间的元素         
l.insert(it, x);        //x是T的右值引用 T&&
l.insert(it, {value1, value2...});    //列表
//以上函数返回一个指向新插入的第一个元素的迭代器//emplace(C++11)
l.emplace(it,  args);    //以args为参数,调用T的构造函数构造一个对象插入it所指的位置
l.emplace_back(args);    //将构造的T对象插入尾部
l.emplace_front(args);    //插入前端
//以上函数返回一个指向新插入的元素的迭代器//erase
l.erase(it);    //删除it指向的元素
l.erase(first, last);    //删除范围内的元素
//以上函数返回一个迭代器,指向被删除的最后一个元素之后的元素

l.clear();    //删除所有元素

修改:

//splice
l.splice(it, x);     
l.splice(it, x, itx);    //x为引用或右值引用,将x的内容拼接到it指向的位置处. 该过程不包括构造和析构过程,而是元素的转移。如果给定itx则是转移x中itx指向的元素
l.splice(it, first, last);list<int> l1 {1,2,3};
list<int> l2 {10, 20, 30};
l1.splice(l1.begin(), l2);    //l1: 1, 10, 20, 30, 2, 3

l.remove(value);    //删除所有等于value的元素
l.remove_if(pred);    // list::remove_if
#include <iostream>
#include <list>// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {bool operator() (const int& value) { return (value%2)==1; }
};
int main ()
{int myints[]= {15,36,7,17,20,39,4,1};std::list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1mylist.remove_if (single_digit);           // 15 36 17 20 39mylist.remove_if (is_odd());               // 36 20return 0;
}l.unique();
l.unique(binary_pred);
#include <iostream>
#include <cmath>
#include <list>// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }
// a binary predicate implemented as a class:
struct is_near {bool operator() (double first, double second){ return (fabs(first-second)<5.0); }
};
int main ()
{double mydoubles[]={ 12.15,  2.72, 73.0,  12.77,  3.14,12.77, 73.35, 72.25, 15.3,  72.25 };std::list<double> mylist (mydoubles,mydoubles+10);mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,// 15.3,  72.25, 72.25, 73.0,  73.35mylist.unique();           //  2.72,  3.14, 12.15, 12.77// 15.3,  72.25, 73.0,  73.35mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15// 15.3,  72.25, 73.0mylist.unique (is_near());           //  2.72, 12.15, 72.25return 0;
}l.merge(x);
l.merge(x, comp);
// list::merge
#include <iostream>
#include <list>
// compare only integral part:
bool mycomparison (double first, double second)
{ return ( int(first)<int(second) ); }
int main ()
{std::list<double> first, second;first.push_back (3.1);first.push_back (2.2);first.push_back (2.9);second.push_back (3.7);second.push_back (7.1);second.push_back (1.4);first.sort();second.sort();first.merge(second);// (second is now empty)second.push_back (2.1);first.merge(second,mycomparison);std::cout << "first contains:";for (std::list<double>::iterator it=first.begin(); it!=first.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}
//first contains: 1.4 2.2 2.9 2.1 3.1 3.7 7.1

l.sort();
l.sort(comp);
bool compare_nocase (const T &first, const T &second);l.reverse();

获取迭代器:

l.begin(), l.end();    //首元素位置,尾后位置
l.cbegin(), l.cend();    //const_iterator//reverse_iterator    按逆序寻址
//const_reverse_iterator
l.rbegin(), l.rend();
l.crbegin(), l.crend();begin(l), end(l);

 

转载于:https://www.cnblogs.com/dengeven/p/3737913.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407781.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

带CheckBox的CListCtrl,源码可下载

实现了一个带CheckBox的CListCtrl&#xff0c;源码可从http://d.download.csdn.net/down/2804276/JoeBlackzqq下载。

grid比flex更强大的属性

dispaly:grid;网格布局 学习博客地址 http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html http://www.ruanyifeng.com/blog/2020/08/five-css-layouts-in-one-line.html

单交换机VLAN 配置和结果验证(51cto-o8)

1. 实验线路连接图使用Cisco Packet Tracer5.3 构建拓扑结构图 2. 实验内容(1) 按图配置各台计算机IP 地址。(2) 参阅教材中内容&#xff0c;完成单交换机上的VLAN 配置&#xff0c;配置要求如图 所示&#xff0c;使用show vlan 指令查看VLAN的配置情况&#xff0c;并使用Ping …

Dottext.Web.UI.Handlers.BlogExistingPageHandler

dottext 转2005时的一个错误 在建立Dottext.Web.UI.Handlers.BlogExistingPageHandler对象时 抱不存在的错误 解决办法,把这个类直接复制到 项目 Dottext.Common 里去 web.config 再添加 type"Dottext.Web.UI.Handlers.BlogExistingPageHandler,Dottext.Common" 就可…

Linux下计算程序运行时间的两种方法

Linux下计算程序运行时间的两种方法 1.以下是我在网上看到的&#xff1a; 有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析 ..这个时候可以使用下面这个函数. #include <sys/time.h> int gettimeofday(struct timeval *tv,struct timezone *tz); stru…

vue路由懒加载和组件懒加载

vue路由懒加载和组件懒加载其实原理一样 常用的懒加载方式有两种&#xff1a;即使用vue异步组件 和 ES中的import 路由懒加载 ES 提出的import方法&#xff0c; 方法如下&#xff1a;const HelloWorld &#xff08;&#xff09;>import(需要加载的模块地址) &#xff08;不…

UML:类图复习-鸡生蛋,蛋生鸡

这是前一阵《高级软件工程》课堂上&#xff0c;老师随堂出的一道讨论题&#xff0c;随手贴在这里&#xff1a; ps: 今天是520&#xff0c;正好聊一些OoXx&#xff0c;关于爱的扯淡话题&#xff1a;&#xff09; 题目&#xff1a;“鸡生蛋&#xff0c;蛋孵鸡”&#xff0c;世间万…

[转] 硬盘工具DiskMan使用图解

① 图解DM硬盘分区的基本使用(图) 对于一个新硬盘来说&#xff0c;首先必须进行的工作就是进行分区&#xff0c;只有这样才能正常使用&#xff0c;同时分区也是为方便我们进行资料的管理。DOS中的Fdisk是一个很小巧的工具&#xff0c;但是在使用上有些麻烦&#xff0c;特别是…

Linux下检测网络状态是否正常

// Linux下检测网络状态是否正常#include <sys/types.h>#include <string.h>#include <stdlib.h>#include <sys/ioctl.h>#include <stdio.h>#include <errno.h>#include <net/if.h>struct ethtool_value {__uint32_t cmd;__uin…

websocket阮一峰博客地址教学

http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 教程

asp.net中MaintainScrollPositionOnPostback属性的使用

可能我们会经常遇到这种情况&#xff0c;当页面内容比较多的时候&#xff0c;当用户执行操作执行一次页面回送后&#xff0c;页面又重新从顶端开始显示&#xff0c;用户不得不重新拖动滚动条回到先前的位置&#xff0c;这会给用户带来很不友好的体验。即时使用updatepanel也会有…

Linux下显示当前目录下的全部目录或文件

Linux终端中显示当前目录下的所有目录和文件&#xff08;不包含隐藏文件&#xff09;&#xff1a; [rootlocalhost ~]# ll     // 显示所有目录和文件总用量 124-rw------- 1 root root 2382 5月 6 07:28 anaconda-ks.cfgdrwxr-xr-x 2 root root 4096 6月 25 16:45 …

TradingView用于画K线图表

附上两个文档链接&#xff1a; 官方文档链接&#xff1a;https://github.com/tradingview/charting_library/wiki&#xff08;需向官方申请&#xff09; 中文文档链接&#xff1a;https://zlq4863947.gitbooks.io/tradingview/&#xff08;转&#xff09;

[心得]Ubuntu無法ssh登入

裝好ssh後&#xff0c;發覺無法用root登入&#xff0c;可是sshd_config接正確。 後來發現原因在於&#xff0c;Ubuntu沒有root帳號&#xff0c;但是可以透過sudo -s拿到root權限。 su root 密碼怎樣打也行不通&#xff0c;不過sudo -s卻可以。 總之就是要用sudo -s 後 執行pass…

Flex布局教程篇

Flex布局是什么&#xff1f; Flex是Flexible Box的缩写&#xff0c;意为”弹性布局”&#xff0c;用来为盒状模型提供最大的灵活性。 任何一个容器只要添加”display:flex”都可以指定为Flex布局。行内元素添加”display:inline-flex”便可以指定为使用Flex布局。 注意:设置…

JavaScript使用技巧精萃

(一).确认删除用法:1. BtnDel.Attributes.Add("onclick","return confirm(""确认删除?"")");2. linktempDelete.Attributes["onclick"]"javascript:return confirm(""确认删除?"");";3. pr…