c++ list

embedded/2025/2/2 8:55:02/

1.构造函数

构造函数

// list<T> lst;
// list(beg, end); // 区间构造
// list(n, elem); // 元素构造   
// list(const list &lst); // 拷贝构造
#include <iostream>
#include <fstream>
#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_back(40);printList(l);// 区间构造list<int> l2(l.begin(), l.end());printList(l2);// 拷贝构造list<int> l3(l2);printList(l3);// 元素构造list<int> l4(10, 100);printList(l4);}int main(int argc, char const *argv[]) {test01();return 0;
}

2 赋值和交换

// 函数原型:
// assign(beg, end) // 将区间[beg, end)中的数据拷贝到本身
// assign(n, elem) // 将n个elem拷贝赋值给本身
// list &operator=(const list &L); // 重载=运算符
// swap(L) // 将L与本身的元素互换

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
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_back(40);printList(l);// assign(beg, end) // 将区间[beg, end)中的数据拷贝到本身list<int> l2;l2.assign(l.begin(), --l.end() );printList(l2);// assign(n, elem) // 将n个elem拷贝赋值给本身l2.assign(5, 100);cout << "L2 " << endl;printList(l2);// swap l2.swap(l);cout << "l 的值" << endl;printList(l);cout << "l2的值" << endl;printList(l2);}int main(int argc, char const *argv[]) {test01();return 0;
}

list__115">3 list 大小操作

函数原型:

size() // 返回容器中元素的个数
empty() // 判断容器是否为空
resize(int num) // 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem) // 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;//   list 大小操作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_back(40);printList(l);if(l.empty()) {cout << "l is empty" << endl;}else{cout << "l is not empty" << endl;}cout << "l size = " << l.size() << endl;l.resize(10);printList(l);cout << "l size = " << l.size() << endl;l.resize(2);printList(l);cout << "l size = " << l.size() << endl;l.resize(10, 100);printList(l);}int main(int argc, char const *argv[]) {test01();return 0;
}

list__180">4 list 插入和删除

函数原型
puhsh_back(elem) 在容器尾部插入元素
pop_back() 删除容器最后一个元素
push_front(elem) 在容器头部插入元素
pop_front() 删除容器第一个元素
insert(pos, elem) // 在pos位置插入元素elem,返回新的数据位置
insert(pos,n,elem) // 在pos位置插入n个elem,无返回值
insert(pos,beg,end) // 在pos位置插入[beg,end)区间的数据,无返回值
clear() // 清空容器数据
erase(pos) // 删除pos位置的数据, 返回删除的元素的下一个位置
erase(beg,end) // 删除[beg,end)区间的数据,返回删除的元素的下一个位置
remove(value) // 删除容器中所有与value值匹配的元素
clear() // 清空容器数据

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
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_front(100);L1.push_front(200);L1.push_front(300);// 300 200 100 10 20 30printList(L1);// 尾删L1.pop_back();// 300 200 100 10 20printList(L1);// 头删L1.pop_front();// 200 100 10 20printList(L1);// insert 插入list<int>::iterator it = L1.begin();L1.insert(++it, 1000);printList(L1);// 删除it = L1.begin();L1.erase(++it);printList(L1);// remove 删除所有值为777的元素L1.push_back(777);L1.push_back(777);L1.push_back(777);printList(L1);L1.remove(777);printList(L1);// 清空L1.clear();printList(L1);}int main(int argc, char const *argv[]) {test01();return 0;
}

list_263">5. list数据存取

函数原型

// front() 返回第一个元素的引用
// back() 返回最后一个元素的引用
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;//   list数据存取// 函数原型
// front() 返回第一个元素的引用
// back() 返回最后一个元素的引用// 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);printList(L1);// L1[0] 不可以访问// L1.at(0) 不可以访问// 原因是list本质是链表,不是连续线性空间存储数据,迭代器也不支持随机访问cout << "front: " << L1.front() << endl;cout << "back: " << L1.back() << endl;// 验证迭代器不支持随机访问list<int>::iterator it = L1.begin();it++;it--;// 不可以进行it+1 // 不支持随机访问}int main(int argc, char const *argv[]) {test01();return 0;
}

list__323">6. list 反转和排序

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;//  list 反转和排序// 函数原型
// sort() 返回第一个元素的引用
// reverse() 反转链表void printList(const list<int> &L){for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}bool myCompare(int v1, int v2){// 当前元素比参数大,则返回truereturn v1 > v2;
}void test01() {list<int> L1;L1.push_back(10);L1.push_back(50);L1.push_back(30);printList(L1);// 反转L1.reverse();printList(L1);// 排序L1.sort(); // 默认从小到大printList(L1);L1.sort(myCompare); // 从大到小printList(L1);// 所有不支持随即访问的容器,不支持标准的sort算法// sort(L1.begin(), L1.end()); // 运行时报错}int main(int argc, char const *argv[]) {test01();return 0;
}

7. 排序案例

案例描述: 将person自定义数据类型进行排序,Person中属性有 姓名 年龄 身高
排序规制: 按照年龄进行升序

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;//  list 排序案例
// 案例描述: 将person自定义数据类型进行排序,Person中属性有 姓名 年龄 身高
// 排序规制: 按照年龄进行升序class Person{
public:Person(string name, int age, int height) {this->m_Age = age;this->m_Height = height;this->m_Name = name;}string m_Name;int m_Age;int m_Height;
};void printList (list<Person> &L) {for(list<Person>::iterator it = L.begin(); it != L.end(); it++){cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << " 身高: " << it->m_Height << endl;}cout << endl;
}bool comparePerson(Person &p1, Person &p2) {if(p1.m_Age == p2.m_Age) {// 年龄相同 按照身高降序return p1.m_Height > p2.m_Height;}// 年龄升序return p1.m_Age < p2.m_Age;
}void test01() {list<Person> L;L.push_back(Person("刘备", 35, 180));L.push_back(Person("关羽", 35, 175));L.push_back(Person("赵云", 26, 160));L.push_back(Person("曹操", 45, 190));L.push_back(Person("张飞", 35, 170));L.push_back(Person("诸葛亮", 28, 175));//插入数据printList(L);//排序后L.sort(comparePerson);printList(L);}int main(int argc, char const *argv[]) {test01();return 0;
}

http://www.ppmy.cn/embedded/158864.html

相关文章

快速启动与休眠唤醒的区分

留着备查&#xff0c;写驱动的方式可以判断真假S4&#xff1b; 链接

G. XOUR

题目链接&#xff1a;Problem - G - Codeforces 题目大意&#xff1a;给你一个n长的序列&#xff0c; 其中你可以将a[i] XOR a[j] 的值 严格小于4的数对进行交换。 你可以操作任何几次&#xff0c; 让最后的数列最小。如果在 x 和 y 不同的第一个位置&#xff0c; xi<yi &…

软件测试 —— jmeter(2)

软件测试 —— jmeter&#xff08;2&#xff09; HTTP默认请求头&#xff08;元件&#xff09;元件作用域和取样器作用域HTTP Cookie管理器同步定时器jmeter插件梯度压测线程组&#xff08;Stepping Thread Group&#xff09;参数解析总结 Response Times over TimeActive Thre…

Flink报错Caused by: java.io.FileNotFoundException: /home/wc.txt

当在提交一个flink任务报如下的错误时&#xff1a; Caused by: java.io.FileNotFoundException: /home/wc.txt (没有那个文件或目录)at java.io.FileInputStream.open0(Native Method)at java.io.FileInputStream.open(FileInputStream.java:195)at java.io.FileInputStream.&…

分布式微服务系统架构第87集:kafka

Kafka 就是为了解决上述问题而设计的一款基于发布与订阅的消息系统。它一般被称为 “分布式提交日志”或者“分布式流平台”。文件系统或数据库提交日志用来提供所有事务 的持久记录&#xff0c;通过重放这些日志可以重建系统的状态。同样地&#xff0c;Kafka 的数据是按照一定…

小程序-视图与逻辑

前言 1. 声明式导航 open-type"switchTab"如果没有写这个&#xff0c;因为是tabBar所以写这个&#xff0c;就无法跳转。路径开始也必须为斜线 open-type"navigate"这个可以不写 现在开始实现后退的效果 现在我们就在list页面里面实现后退 2.编程式导航…

自定义数据集使用框架的线性回归方法对其进行拟合

代码 import torch import numpy as np import torch.nn as nncriterion nn.MSELoss()data np.array([[-0.5, 7.7],[1.8, 98.5],[0.9, 57.8],[0.4, 39.2],[-1.4, -15.7],[-1.4, -37.3],[-1.8, -49.1],[1.5, 75.6],[0.4, 34.0],[0.8, 62.3]])x_data data[:, 0] y_data data…

10 Flink CDC

10 Flink CDC 1. CDC是什么2. CDC 的种类3. 传统CDC与Flink CDC对比4. Flink-CDC 案例5. Flink SQL 方式的案例 1. CDC是什么 CDC 是 Change Data Capture&#xff08;变更数据获取&#xff09;的简称。核心思想是&#xff0c;监测并捕获数据库的变动&#xff08;包括数据或数…