【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 = { , , };
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 {,,};
list<int> l2 {, , };
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<); }
// a predicate implemented as a class:
struct is_odd {
bool operator() (const int& value) { return (value%)==; }
};
int main ()
{
int myints[]= {,,,,,,,};
std::list<int> mylist (myints,myints+); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20return ;
} 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+); mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35
mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
mylist.unique (same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0
mylist.unique (is_near()); // 2.72, 12.15, 72.25return ;
} 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 ;
}
//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);
【STL基础】list的更多相关文章
- c++中级 STL基础学习(二)
deque 和vector差不多,可以在前端后端插入,一般用deque取代vector,vector只能在后端插入push_back().deque还可以push_front(),但是deque后端插 ...
- STL基础知识
一,STL的组成 1.什么是STL STL(Standard Template Library)标准模板库的简称,是由惠普开发的一系列软件的总称,STL现在是C++的一部分,已经被构建于编译系统之内, ...
- STL基础复习
stl容器:vector,deque,list,map/multimap,set 特殊容器:stack,queue,priority_queue 通用操作 size() 返回当前容器元素数量 emp ...
- STL基础--算法(排序)
STL排序算法 排序算法要求随机访问迭代器 vector, deque, container array, native array 例子 vector<int> vec = {9,1,1 ...
- STL基础--仿函数(函数对象)
1 首先看个仿函数的例子 class X { public: void operator()(string str) { // 函数调用运算符,返回类型在operator之前 cout << ...
- STL基础--迭代器和算法
1 迭代器 Iterators 5种迭代器类型 随机访问迭代器: vector, deque, array // 允许的操作 vector<int> itr; itr = itr + 5; ...
- STL基础--容器
容器种类 序列容器(数组,链表) Vector, deque, list, forward list, array 关联容器(二叉树),总是有序的 set, multiset根据值排序,元素值不能修改 ...
- STL基础--基本介绍
为什么要使用C++标准库 /* * 为什么使用C++标准库: * 1. 代码重用,不用重新造轮子 * 2. 效率(快速,且使用更少的资源). 现代C++编译器经常对C++标准库的代码有优化 * 3. ...
- STL基础
vector: 1.头文件#include<vector> 2.声明vector对象,vector<int> vec; 3.尾部插入a:vec.push_back(a); 4. ...
- STL基础4:deque
#include <iostream> #include <queue> #include <string> using namespace std; #defin ...
随机推荐
- jquery-messager-消息提示
一.页面引入 jquery.js 下载地址问度娘 jquery-message.js 下载地址:jquery-message.js 二.页面使用 //ajax轮询检查新的订单 function che ...
- Tornado之抽屉实战(1)--分析与架构
项目模拟地址:http://dig.chouti.com/ 知识点应用: AJAX 用于偷偷发请求 原生ajax jQuery ajax($.ajax) iframe伪造 上传文件 传统Form ...
- os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path) os.path.join(path1[, path2[, ...]])
提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于 ...
- 如何设置Win10文件资源管理器默认打开“这台电脑”
摘录自:http://www.ithome.com/html/win10/126066.htm
- php 关于锁的一些看法
背景:在一个项目中,需要一次对数据很复杂的计算,其中一次计算需要花费大概30秒钟时间,大概需要查询一个比较大的表300次左右,然后还需要进行查询7-8次数据库,然后进行组合排序等功能,完成最终结果.对 ...
- 使用ServerSocket建立聊天服务器(一)
-------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...
- Spring 第一天课程
一. 面试题部分 1. 什么是IOC?什么是DI?他们有什么区别? 答案: IOC,即控制反转.是指将原来程序中自己创建实现类对象的控制权反转到IOC容器中. IOC的别名:依赖注入(DI),DI 是 ...
- ROS Learning-007 beginner_Tutorials ROS节点
ROS Indigo beginner_Tutorials-06 ROS节点 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
1. package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /******* ...
- UVA1723 Intervals
这题$n$倍经验…… 考虑差分约束: 我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} \leq c_i$,那么连边$ ...