[C++STL]list容器用法介绍

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);list<int>L2(L1.begin(), L1.end());printList(L2);list<int>L3(L2);printList(L3);list<int>L4(10, 100);printList(L4);}int main()
{test01();return 0;
}

测试结果:
在这里插入图片描述
在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);//赋值list<int>L2;L2 = L1;printList(L2);list<int>L3;L3.assign(L2.begin(), L2.end());printList(L3);list<int>L4;L4.assign(10, 100);printList(L4);}//交换
void test02()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;L2.assign(10, 100);cout << "交换前" << endl;printList(L1);printList(L2);cout << endl;L1.swap(L2);cout << "交换后" << endl;printList(L1);printList(L2);
}int main()
{test01();test02();return 0;
}

测试结果:

在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);if (L1.empty()){cout << "L1 empty" << endl;}else{cout << "L1 no empty" << endl;cout << "L1 capacity = " << L1.size() << endl;}L1.resize(10);printList(L1);L1.resize(2);printList(L1);}int main()
{test01();return 0;
}

测试结果:
在这里插入图片描述

总结:

在这里插入图片描述
在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}void test01()
{list<int>l;l.push_back(10);l.push_back(20);l.push_back(30);l.push_front(100);l.push_front(200);l.push_front(300);printList(l);l.pop_back();printList(l);l.pop_front();printList(l);list<int>::iterator it = l.begin();l.insert(++it, 1000);printList(l);it = l.begin();l.erase(++it);printList(l);l.push_back(10000);l.push_back(10000);l.push_back(10000);printList(l);l.remove(10000);printList(l);l.clear();printList(l);}int main()
{test01();return 0;
}

测试结果:

在这里插入图片描述

总结:
在这里插入图片描述

在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}void test01()
{list<int>l1;l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);//cout<<l1.at(0)<<endl;//错误,不支持at访问数据//cout<<l1[0]<<endl;//错误,不支持[]方式访问数据cout << "front elem = " << l1.front() << endl;cout << "back elem = " << l1.back() << endl;//list容器的迭代器是双向迭代器,不支持随机访问list<int>::iterator it = l1.begin();//it = it+1;//错误,不可以跳跃访问,即便是+1}int main()
{test01();return 0;
}

测试结果:
在这里插入图片描述

总结:

在这里插入图片描述
在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <list>
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;
}bool cmp(int a, int b)
{return a > b;
}void test01()
{list<int>l;l.push_back(90);l.push_back(30);l.push_back(20);l.push_back(70);printList(l);l.reverse();printList(l);l.sort();printList(l);l.sort(cmp);printList(l);}int main()
{test01();return 0;
}

测试结果:

在这里插入图片描述

总结:

反转 — reverse
排序 — sort(成员函数)

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

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

相关文章

C#黔驴技巧之去重(Distinct)

关于C#中默认的Distinct方法在什么情况下才能去重&#xff0c;这个就不用我再多讲&#xff0c;针对集合对象去重默认实现将不再满足&#xff0c;于是乎我们需要自定义实现来解决这个问题&#xff0c;接下来我们详细讲解几种常见去重方案&#xff0c;孰好孰歹自行判之。分组首先…

Almost Union-Find UVA - 11987(并查集的删除操作)

题意&#xff1a;求出每个集合的元素个数&#xff0c;及总和&#xff0c;给出三个操作&#xff1a; 1 将含有a元素和b元素的集合合并&#xff1b;2 将a元素放入含有b元素的集合中&#xff1b;3 输出a元素所在集合的元素个数及总和&#xff1b; 思路&#xff1a;正常并查集&am…

java.sql 拒绝连接_hive jdbc 拒绝连接问题

本帖最后由 willgone 于 2017-06-21 10:56 编辑平台配置在附件中。private static String driverName1 "org.apache.hive.jdbc.HiveDriver";Class.forName(driverName1);String url"jdbc:hive2://192.168.160.241:21076/default";Connection con DriverM…

ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记

任务15&#xff1a;oauth2 oidc 实现 server部分基于之前快速入门的项目&#xff08;MvcCookieAuthSample&#xff09;&#xff1a;ASP.NET Core快速入门&#xff08;第5章&#xff1a;认证与授权&#xff09;--学习笔记ASP.NET Core快速入门&#xff08;第6章&#xff1a;ASP…

[C++STL]set容器用法介绍

代码如下: #include <iostream> #include <set> using namespace std;void printSet(set<int>&s) {for (set<int>::iterator it s.begin(); it ! s.end(); it){cout << *it << " ";}cout << endl; }void test01() {…

Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况

题意:求x在(-100<x<100)区间上&#xff0c;已知a,b,c,d&#xff0c;满足a*x1^2b*x2^2c*x3^2d*x4^20 的情况有多少种 思路&#xff1a;很明显四层for循环肯定超时&#xff0c;可用三层for循环来判断&#xff0c;或是&#xff0c;两两组合&#xff0c;求多少种情况。分正负…

java.time.format例子_java格式化时间示例

实现日期的格式化&#xff0c;需要用到类: java.text.DateFormatDateFormat没有可以直接使用的构造函数&#xff0c;一般使用DateFormate的子类---java.text.SimpleDateFormat完成构造.public SimpleDateFormat(String pattern)测试代码import java.text.DateFormat;import jav…

[C++STL]map容器用法介绍

代码如下: #include <iostream> #include <string> #include <map> using namespace std;void printMap(map<int,int>&m) {for (map<int, int>::iterator it m.begin(); it ! m.end(); it){cout << "key " << it-&…

ABP框架 v2.7.0已经发布!

ABP框架和ABP商业版 v2.7已经发布.我们没有为2.4,2.5和2.6发布博客文章,所以这篇文章也将涵盖这几个版本中新增内容和过去的2个月里我们完成了什么.关于发布周期与开发之前说过我们已经开始每两个星期发布一个新的次要功能版本,一般在星期四.我们的目标是尽快提供新功能.在过去…

Colossal Fibonacci Numbers! UVA - 11582(斐波那契求模)+快速幂+周期规律

题意&#xff1a; 给出64位整数a、b以及不超过1000的正整数n&#xff0c;求斐波那契数列第a ^ b项模n的结果。 输入&#xff1a;情况数T&#xff0c;之后T行每行a、b、n。 输出&#xff1a;斐波那契数列第a ^ b项模n的结果。 分析&#xff1a;由于斐波那契数列的每一项都是由…

java中factory方法_Java的23中设计模式--工厂方法模式(Factory Method)

1.普通工厂模式工厂类/*** Title Factory.java* Package factory.factory1* date 2015-1-22 上午10:16:02*versionV1.0*/packagefactory.factory1;/*** ClassName Factory* date 2015-1-22 上午10:16:02*/public classFactory {publicSender procedure(String type) {if("…

GCD and LCM HDU - 4497(素数打表+唯一分解定理)求多少种情况

题目大意&#xff1a; 给你两个数最小公倍数L,最大公约数G&#xff0c;问你有多少有序数组&#xff08;x,y,z)满足GCD&#xff08;x,y,z)G,LCM(x,y,z)L,首先如果gcd(x,y,z)G, 思路分析&#xff1a; 当这样的组合存在的时候&#xff0c;求gcd&#xff08;x,y,z&#xff09;1且l…

[C++STL]仿函数用法介绍

代码如下: #include <iostream> #include <string> using namespace std;//函数对象在使用时&#xff0c;可以像普通函数那样调用&#xff0c;可以有参数&#xff0c;可以有返回值 class MyAdd { public:int operator()(int a, int b){return a b;} };void test0…

【壹刊】Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API (上)

—————————Grant_Allen 是一位博客园新晋博主&#xff0c;目前开始专注于Azure方向的学习和研究&#xff0c;是我认识不多的、打算长时间研究Azure的群友&#xff0c;因此打算帮他开个专栏&#xff0c;同时也希望并祝愿他能一直坚持下去&#xff0c;学有所成。正文一&a…

[C++STL]常用遍历算法

代码如下&#xff1a; #include <iostream> #include <algorithm> #include <vector> using namespace std;void print01(int val) {cout << val << " "; }class print02 { public:void operator()(int val){cout << val <&…

用Visual Studio2019自定义项目模板

项目模板简介众所周知&#xff0c;在我们使用VS新建项目时&#xff0c;都需要选择一个项目模板&#xff0c;如下图&#xff1a;我们选择完项目模板进行创建&#xff0c;创建完成之后&#xff0c;可以发现项目中已经包含了一些基础的文件。例如MVC&#xff1a;可以看到&#xff…

A/B HDU - 1576 (逆元或拓展欧几里得或数学公式)多解法求大数结果

题意&#xff1a;求(A/B)%9973&#xff0c;但由于A很大&#xff0c;我们只给出n(nA%9973)(我们给定的A必能被B整除&#xff0c;且gcd(B,9973) 1)。 思维&#xff1a;&#xff08;1&#xff09;逆元扩展欧几里得算法&#xff1a;满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。…

java线程同步的优缺点_浅谈java多线程编程

一、多线程的优缺点多线程的优点&#xff1a;1)资源利用率更好2)程序设计在某些情况下更简单3)程序响应更快多线程的代价&#xff1a;1)设计更复杂虽然有一些多线程应用程序比单线程的应用程序要简单&#xff0c;但其他的一般都更复杂。在多线程访问共享数据的时候&#xff0c;…

[C++STL]常用排序算法

代码如下: #include <iostream> #include <algorithm> #include <vector> using namespace std;void myPrint(int val) {cout << val << " "; }void test01() {vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);…

Fliptile POJ - 3279 (翻转)(二进制+对第一行暴力遍历翻转的位置)

题意&#xff1a;给你一个给你一个矩阵&#xff0c;有黑白两个状态。每次你可以选择一个变为其相反的状态&#xff0c;它周围的4个都会变成相反的状态。问你最少需要改变多少个使得矩阵中的状态全为白色&#xff0c;若有多个答案&#xff0c;输出字典序最小的 思路&#xff1a…