8.list的使用

一.构造函数以及赋值

二.迭代器部分

三.容量和元素

四.成员函数

五.list的使用

cpp 复制代码
void test_list1()
{
	list<int> lt1 = { 10,2,3,3,4,3,5,6};
	list<int>::iterator it = lt1.begin();
	while (it != lt1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	//sort(lt1.begin(), lt1.end());
	lt1.sort();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	lt1.sort(greater<int>());
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	lt1.unique();
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}

但是list是双向迭代器,所以list不能使用std::sort

list里面的sort()使用的是归并排序

六.测试vector和list排序时的性能

cpp 复制代码
void test_op1()
{
	srand(time(0));
	const int N = 10000000;

	list<int> lt1;
	list<int> lt2;

	vector<int> v;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand() + i;
		lt1.push_back(e);
		v.push_back(e);
	}

	int begin1 = clock();
	// 排序
	sort(v.begin(), v.end());
	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

几乎vector排序的销毁几乎是list的3倍

cpp 复制代码
void test_op2()
{
	srand(time(0));
	const int N = 10000000;

	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand()+i;
		lt1.push_back(e);
		lt2.push_back(e);
	}

	int begin1 = clock();
	// 拷贝vector

	vector<int> v(lt2.begin(), lt2.end());
	// 排序
	sort(v.begin(), v.end());

	// 拷贝回lt2
	lt2.assign(v.begin(), v.end());

	int end1 = clock();

	int begin2 = clock();
	lt1.sort();
	int end2 = clock();

	printf("list copy vector sort copy list sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}

上面这个是先将list拷贝到vector排序完之后再拷贝会到list,结果用时还少

七.splice接口使用

cpp 复制代码
int main()
{
   std::list<int> mylist1, mylist2;
   std::list<int>::iterator it;

   // set some initial values:
   for (int i = 1; i <= 4; ++i)
       mylist1.push_back(i);      // mylist1: 1 2 3 4

   for (int i = 1; i <= 3; ++i)
       mylist2.push_back(i * 10);   // mylist2: 10 20 30

   it = mylist1.begin();
   ++it;                         // points to 2

   mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
                                 // mylist2 (empty)
                                 // "it" still points to 2 (the 5th element

	return 0;
}

自己插入自己,如下:

cpp 复制代码
int main()
{
   std::list<int> mylist1;
  
   for (int i = 1; i <= 4; ++i)
       mylist1.push_back(i);      // mylist1: 1 2 3 4

   auto it = find(mylist1.begin(), mylist1.end(), 3);

   mylist1.splice(mylist1.begin(), mylist1, it);

   return 0;
}

如果要将后面的全部转移,就不要写三个参数了,只转移一个就要写第三个参数

cpp 复制代码
#include <iostream>
#include <list>

using namespace std;

// 打印链表内容的辅助函数
void printList(const list<int>& l, const string& name) {
    cout << name << ": ";
    for (int num : l) {
        cout << num << " ";
    }
    cout << endl;
}

int main() {
    list<int> list1 = {1, 2, 3};
    list<int> list2 = {4, 5, 6};
    
    printList(list1, "list1初始值");
    printList(list2, "list2初始值");
    
    // 1. 将list2的所有元素转移到list1的末尾
    list1.splice(list1.end(), list2);
    printList(list1, "list1转移后");  // 1 2 3 4 5 6
    printList(list2, "list2转移后");  // 空
    
    // 重新给list2赋值
    list2 = {7, 8, 9};
    printList(list2, "list2重新赋值");  // 7 8 9
    
    // 2. 将list2中第一个元素转移到list1的开头
    list1.splice(list1.begin(), list2, list2.begin());
    printList(list1, "list1转移单个元素后");  // 7 1 2 3 4 5 6
    printList(list2, "list2转移单个元素后");  // 8 9
    
    // 3. 将list2的所有元素转移到list1中值为3的元素前面
    auto it = find(list1.begin(), list1.end(), 3);
    if (it != list1.end()) {
        list1.splice(it, list2, list2.begin(), list2.end());
    }
    printList(list1, "list1转移范围元素后");  // 7 1 2 8 9 3 4 5 6
    printList(list2, "list2转移范围元素后");  // 空
    
    return 0;
}
相关推荐
Ayanami_Reii16 小时前
进阶数据结构应用-维护序列
数据结构·算法·线段树
_w_z_j_16 小时前
mari和shiny() (多状态dp数组)
算法
CoderYanger17 小时前
C.滑动窗口-越长越合法/求最短/最小——2904. 最短且字典序最小的美丽子字符串
java·开发语言·数据结构·算法·leetcode·1024程序员节
Tim_1017 小时前
【算法专题训练】33、堆
算法
lijiatu1008617 小时前
[C++] QTimer与Qt事件循环机制 实验探究
c++·qt
三月微暖寻春笋17 小时前
【和春笋一起学C++】(四十九)C++中string类的简介
c++·cstring·string类·string类的实现·string类方法
Salt_072817 小时前
DAY25 奇异值SVD分解
python·算法·机器学习
Bona Sun17 小时前
单片机手搓掌上游戏机(二十一)—pico运行doom之修改编译
c语言·c++·单片机·游戏机
℉AVE17 小时前
点集配准---迭代最近点算法ICP(Iterative Closest Point)
算法
松涛和鸣17 小时前
23、链式栈(LinkStack)的实现与多场景应用
linux·c语言·c++·嵌入式硬件·ubuntu