STL---llist
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b;
}; void func(Node &t)
{
cout << t.a << " " << t.b << "\n"; //使用换行,尽量使用\n,endl的效率会慢很多
//这里只会打印结构体中的b 因为对于int了类型的0可以直接打印,对于char类型的0,他会找到0对应的assic码,而0对应的assci码无法在控制台打印
cout << "强制转换 " << (int)t.a << "\n";
}
int main()
{
list<Node> li; //无参构造 list<Node> li1(); //结构体重的元素被初始化为0
for_each(li1.begin(), li1.end(), func); return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b;
}; void func(Node &t)
{
cout << t.b << " " << t.a << "\n";
}
int main()
{ Node node = { 'a', };
list<Node> li(, node);
for_each(li.begin(), li.end(), func); cout << "***************************************\n";
list<Node> li2(li);
for_each(li2.begin(), li2.end(), func); cout << "***************************************\n";
list<Node> li3(li.begin(),li.end());
for_each(li3.begin(), li3.end(), func); list<Node>::iterator ite = li3.begin();
while (ite != li3.end())
{
cout << ite->a << " " << ite->b << "\n";
ite++;
}
return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b;
}; void func(Node &item)
{
cout << item.a << " " << item.b << "\n";
}
int main()
{
Node node = { 'a', };
list<Node> li(, node);
cout << "list中有多少个元素: " << li.size() << "\n";
cout << "这个list是否为空 " << li.empty() << "\n";
for_each(li.begin(), li.end(), func); cout << "************************************************\n";
li.resize();
for_each(li.begin(), li.end(), func);
cout << "经过resize改变后的容量: " << li.size() << "\n";
cout << "这个list是否为空 " << li.empty() << "\n"; cout << "************************************************\n";
li.resize();
for_each(li.begin(), li.end(), func);
cout << "这个list是否为空 " << li.empty() << "\n"; return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b; Node(char c, int d)
{
a = c;
b = d;
}
}; void func(Node &t)
{
cout << t.a << " " << t.b << "\n";
}
int main()
{
Node node('a', ); //挡结构体中有初始化函数,要像使用类一样去定义这个结构体变量
list<Node> li(, node); li.push_back(Node('b', )); //输入元素
li.push_front(Node('c', )); cout << "li.front a " << li.front().a << "\n"; //输出元素
cout << "li.back a " << li.back().a << "\n"; //在中间添加元素
list<Node>::iterator ite = li.begin();
ite++;
li.insert(ite, Node('d', )); //第一个参数是一个迭代器,第二个参数是要插入的节点元素
li.insert(ite, , Node('e', )); //插入三个相同的元素 for_each(li.begin(), li.end(), func); return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b; Node(char c, int d)
{
a = c;
b = d;
} bool operator == (const Node& t) //要使用list中的remove函数就必须重载这个函数
{
if ((t.a == this->a) && (t.b == this->b))
{
return true;
}
else
{
return false;
}
}
}; void func(Node &t)
{
cout << t.a << " " << t.b << "\n";
}
int main()
{
list<Node> li;
li.push_back(Node('a', ));
li.push_back(Node('b', ));
li.push_back(Node('c', ));
li.push_back(Node('d', ));
li.push_back(Node('e', ));
li.push_back(Node('d', )); //li.pop_back(); //尾删除
//li.pop_front(); //头删除 list<Node>::iterator ite = li.begin();
ite++;
li.erase(ite); //删除一个指定的元素
//这个函数里面还可以放两个迭代器,表示删除这一段元素 li.remove(Node('e', )); //当这个list中有与之一致的结点就删除这个结点,有多个的情况下就删除多个
li.unique(); //去重 li.assign(, Node('a', )); //重新赋值
//这个函数还可以传入两个迭代器,表示用迭代器中间的元素来重新赋值这个list对象 for_each(li.begin(), li.end(), func); return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b; Node(char c, int d)
{
a = c;
b = d;
} bool operator < (const Node& t) //使用sort函数,要重载<
{
if ( t.b < this->b)
{
return true;
}
else
{
return false;
}
}
}; void func(Node &t)
{
cout << t.a << " " << t.b << "\n";
}
int main()
{
list<Node> li;
list<Node> li1;
li.push_back(Node('a', ));
li.push_back(Node('b', ));
li.push_back(Node('c', ));
li1.push_back(Node('d', ));
li1.push_back(Node('f', ));
li1.push_back(Node('e', )); li.swap(li1); //交换连个list对象 li.reverse(); //翻转这个list对象中的元素 li.sort(); //排序
for_each(li.begin(), li.end(), func); return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b; Node(char c, int d)
{
a = c;
b = d;
} bool operator < (const Node& t)
{
if ( t.b > this->b) //从小到大
{
return true;
}
else
{
return false;
}
}
}; void func(Node &t)
{
cout << t.a << " " << t.b << "\n";
}
int main()
{
list<Node> li;
list<Node> li1;
li.push_back(Node('a', ));
li.push_back(Node('b', ));
li.push_back(Node('c', ));
li1.push_back(Node('d', ));
li1.push_back(Node('e', ));
li1.push_back(Node('f', )); li.merge(li1); //排序函数需要注意几观点
//首先排序前 这两个list必须有序,且有序味相同性质(都是从小到大或者都是从大到小)
//当按照你设置的规则排序两个从小到大的list的时候,注意上面的重载
//当排序的list是从大到小的时候,就需要需要将上面的重载改变一下(改成相反的)
for_each(li.begin(), li.end(), func); return ;
}
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
struct Node
{
char a;
int b; Node(char c, int d)
{
a = c;
b = d;
} bool operator == (const Node t)
{
if ((t.a == this->a) && (t.b == this->b))
{
return true;
}
else
{
return false;
}
} }; void func(Node &t)
{
cout << t.a << " " << t.b << "\n";
}
int main()
{
list<Node> li;
li.push_back(Node('a', ));
li.push_back(Node('b', ));
li.push_back(Node('c', ));
li.push_back(Node('d', ));
li.push_back(Node('e', ));
li.push_back(Node('f', )); list<Node>::iterator ite = find(li.begin(), li.end(), Node('c', ));
cout << ite->a << " " <<ite->b << "\n"; //foreard_list 单向队列,有兴趣可以自行百度 return ;
}
STL---llist的更多相关文章
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++ STL简述
前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- STL的使用
Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
随机推荐
- windows7 端口查看以及杀死进程释放端口
1.调出命令窗口:开始---->运行---->cmd,或者是window+R组合键 2.输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是4300 ...
- Vue收藏资料
组件库的全局引用和按需引用:http://www.cnblogs.com/zhuanzhuanfe/p/7516745.html
- AngularJs学习笔记--Scope
原版地址:http://code.angularjs.org/1.0.2/docs/guide/scope 一.什么是Scope? scope(http://code.angularjs.org/1. ...
- hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2685 题目大意: 求gcd(am-1,an-1)%k 解题思路: 对于am-1 = (a - 1) ...
- hdu-2886 Special Prime---数论推导
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2866 题目大意: 问你1到L中有多少个素数满足n^3 + p*n^2 = m^3(其中n,m为大于1 ...
- Spring AOP 的切点切在Controller上没有起作用的问题。
做下笔记,以防忘记. 在Spring MVC 中使用面向切面编程,感觉所有的配置.切面.切点的编写都没有写错,但是当切点切在@Controller注解的类的方法上时就不起作用.当切点切在的其他的非@C ...
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- ResNet 修改
https://github.com/tornadomeet/ResNet apache 开源项目 修改如下: 训练模块 import argparse,logging,os import mxnet ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- ASP.NET Core 程序发布到Linux(Centos7)爬坑实战
前言 前阶段弄了个Linux系统想倒腾倒腾.NET Core,结果看了下网上的资料,大部分都是过期的,走了不少弯路,不过还好,今下午总算捣鼓出来了.Linux命令太多了,唉.血的教训:安装一定要看官网 ...