首页 > 其他分享 >10 STL-list

10 STL-list

时间:2022-11-25 18:14:45浏览次数:43  
标签:10 200 容器 STL printList list elem l1

 重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!

 

 生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦!

 
写在前面,本篇章主要介绍STL中常用容器list。

1.1 list基本概念

功能:将数据进行链式存储。

链表list 的数据存储:是非连续的,数据元素的前后顺序是通过链表中的指针链接来         实现的。

链表的组成: 由一系列的结点组成。

结点的组成:

  •   一个数据域,用来存储数据元素。
  •   一个指针域,用来存储上一个和下一个结点地址。

链表是一个双向循环链表。 因为指针域可以存储上一个和下一个结点地址,第一个结点的的prev指针指向的是最后一个结点的地址,最后一个结点的next指针指向的第一个结点的地址,使得头尾结点可以相互访问。

链表的迭代器:
由于链表的存储方式不是连续的内存空间,因此链表中的迭代器只支持前移和后移,属于双向迭代器。不是随机迭代器。

编辑

 

list的优点:

  •  存储不是连续的,采用动态存储分配,不会造成内存浪费和溢出。
  •  插入和删除非常方便,修改指针域即可,不需要移动大量元素。
  •  插入和删除都不会造成原有list迭代器失效,这在vector不成立。

  
list的缺点:

  • 灵活,但是空间和时间消耗大。(指针域造成空间消耗大)(在遍历时需要根据指针来判断下一个结点,时间消耗大)  

 

1.2 list构造函数

函数原型:

  •  list<T> lst;         //list采用采用模板类实现,对象的默认构造形式:
  • list(beg, end);         //构造函数将[beg, end)区间中的元素拷贝给本身。
  • list(n, elem);          //构造函数将n个elem拷贝给本身。
  • list(const list& lst);         //拷贝构造函数。

  
示例:

#include <iostream>
#include <string>
#include<list>
using namespace std;

// list<T> lst; //list采用采用模板类实现,对象的默认构造形式:
// list(beg, end); //构造函数将[beg, end)区间中的元素拷贝给本身。
// list(n, elem); //构造函数将n个elem拷贝给本身。
// list(const list& lst); //拷贝构造函数。
void printList(const list<int>& l) {
    for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

void test() {
    list<int> l;
    l.push_back(1);
    l.push_back(2);
    l.push_front(3);
    l.push_front(4);
    printList(l);

    list<int> l2(l.begin(), l.end());
    printList(l2);

    list<int> l3(l2);
    printList(l3);

    list<int>l4(10, 3);
    printList(l4);
}

int main() {
    test();

    system("pause");
    return 0;
}

1.3 list赋值和交换

函数原型:

  • size();                           //返回容器中元素的个数empty();            //判断容器是否为空
  • resize(num);                //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。
  •  resize(num, elem);         //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。

示例:


#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 test() {
	//assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
	list<int> l1;
	for (int i = 0; i < 5; i++) {
		l1.push_back(i);
	}
	printList(l1);

	list<int>l2;
	l2.assign(l1.begin(), l1.end());
	printList(l2);

	//assign(n, elem); //将n个elem拷贝赋值给本身。
	l2.assign(10, 3);
	printList(l2);

	//list& operator=(const list & lst); //重载等号操作符
	list<int> l3;
	l3 = l2;
	printList(l3);

	//swap(lst); //将lst与本身的元素互换。
	l1.swap(l3);
	cout << "互换后的:" << endl;
	printList(l1);
	printList(l3);

}

int main() {
	test();

	system("pause");
	return 0;
}

 1.4 list大小操作

函数原型:

  • size();                          //返回容器中元素的个数
  • empty();                      //判断容器是否为空
  • resize(num);                //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。
  • resize(num, elem);   //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。

示例:


#include <iostream>
#include <string>
#include<list>
using namespace std;

//size();				//返回容器中元素的个数
//empty();			//判断容器是否为空
//resize(num);		//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//​					//如果容器变短,则末尾超出容器长度的元素被删除。
//resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//					//如果容器变短,则末尾超出容器长度的元素被删除。

void printList(const list<int>& l) {
	for (list<int>::const_iterator it = l.begin(); it != l.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test() {
	//assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
	list<int> l1;
	for (int i = 0; i < 5; i++) {
		l1.push_back(i);
	}
	printList(l1);
	
	if (l1.empty()) {
		cout << "l1 为空" << endl;
	}
	else {
		cout << "l1 不为空" << endl;
		cout << "l1 size(): " << l1.size() << endl;
	}

	l1.resize(10);
	printList(l1);

	l1.resize(20,6);
	printList(l1);

	l1.resize(3);
	printList(l1);
}

int main() {
	test();

	system("pause");
	return 0;
}

输出:

0 1 2 3 4
l1 不为空
l1 size(): 5
0 1 2 3 4 0 0 0 0 0
0 1 2 3 4 0 0 0 0 0 6 6 6 6 6 6 6 6 6 6
0 1 2
 

 1.5 list插入和删除

函数原型:

  • push_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(beg, end);                //删除[beg,end)区间的数据,返回下一个数据的位置。
  • erase(pos);                        //删除pos位置的数据,返回下一个数据的位置。
  • remove(elem);                  //删除容器中所有与elem值匹配的元素。

示例:


#include <iostream>
#include <string>
#include<list>
using namespace std;

void printList(const list<int>& l) {
	for (auto item : l) {
		cout << item << " ";
	}
	cout << endl;
}
void test() {
	list<int> l1;
	for (int i = 0; i < 5; i++) {
		//push_back(elem);//在容器尾部加入一个元素
		l1.push_back(i);
	}
	//push_front(elem);//在容器开头插入一个元素
	l1.push_front(5);
	printList(l1);// 5 0 1 2 3 4

	//pop_front();//从容器开头移除第一个元素
	l1.pop_front();
	printList(l1);// 0 1 2 3 4

	//pop_back();//删除容器中最后一个元素
	l1.pop_back();
	printList(l1);// 0 1 2 3 

	//insert(pos, elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
	l1.insert(l1.begin(), 100);
	printList(l1);// 100 0 1 2 3

	//insert(pos, n, elem);//在pos位置插入n个elem数据,无返回值。
	l1.insert(l1.end(), 3, 200);
	printList(l1);//100 0 1 2 3 200 200 200 

	//insert(pos, beg, end);//在pos位置插入[beg,end)区间的数据,无返回值。
	l1.insert(l1.begin(), l1.begin(), l1.end());
	printList(l1);//100 0 1 2 3 200 200 200 100 0 1 2 3 200 200 200 
	
	//erase(beg, end);//删除[beg,end)区间的数据,返回下一个数据的位置。
	l1.erase(l1.begin(), ++l1.begin());
	printList(l1);//0 1 2 3 200 200 200 100 0 1 2 3 200 200 200 

	//erase(pos);//删除pos位置的数据,返回下一个数据的位置。
	l1.erase(++l1.begin());
	printList(l1);//0 2 3 200 200 200 100 0 1 2 3 200 200 200 
	
	//remove(elem);//删除容器中所有与elem值匹配的元素。
	l1.remove(200);
	printList(l1);//0 2 3 100 0 1 2 3

	//clear();//移除容器的所有数据
	l1.clear();
	printList(l1);


}

int main() {
	test();

	system("pause");
	return 0;
}

 

 1.6 list数据存取

函数原型:

  • front();                 //返回第一个元素
  • back();                //返回最后一个元素

示例:

#include <iostream>
#include <string>
#include<list>
using namespace std;

void test() {
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	// l[0];  // 错误
	// l.at(0);  // 错误
	// 原因:list本质是双向迭代器,不是随机迭代器,不支持随机访问,不是连续线性存储。

	cout << "first item: " << l.front() << endl;
	cout << "last item: " << l.back() << endl;
	list<int>::iterator it = l.begin();
	it++;// 支持双向
	it++++;
	it--;
	//it = it + 1;//不支持随机访问 即便是+1;
}

int main() {
	test();

	system("pause");
	return 0;
}

 

1.7 list反转和排序

函数原型:

  • reverse();                反转链表
  • sort();                        链表排序,默认升序

示例:

#include <iostream>
#include <string>
#include<list>
#include<algorithm>

using namespace std;
void printList(const list<int>& l) {
	for (auto item : l) {
		cout << item << " ";
	}
	cout << endl;

}
bool myCompare(int v1, int v2) {
	// 降序 就让第一个数 > 第二个数
	return v1 > v2;
}

void test() {
	list<int> l;
	for (int i = 0; i < 5; i++) {
		l.push_back(i);
	}
	printList(l);

	l.reverse();
	printList(l);

	//所有不支持随机访问迭代器的容器,不可以用标准算法
	// 不支持随机访问迭代器的容器,内部会提供对应的算法
	//sort(l.begin(), l.end());
	
	//使用容器内部算法排序 sort排序,默认升序
	l.sort();
	cout << "升序:" << endl;
	printList(l);

	l.sort(myCompare);
	cout << "降序:" << endl;
	printList(l);
}

int main() {
	test();

	system("pause");
	return 0;
}

 

 1.8 list排序案例

需求描述:

        自定义person类,包含姓名、年龄、升高属性。

        排序规则,按照年龄升序,若年龄相同,按照身高降序。

示例:

#include <iostream>
#include <string>
#include<list>
using namespace std;

class Person {
public:
	Person(string name,int age,int height) {
		this->m_name = name;
		this->m_age = age;
		this->m_height=height;
	}

	string m_name;
	int m_age;
	int m_height;
};

bool myCompare(Person& p1, Person& p2) {
	// 年龄相同 按照升高降序
	if (p1.m_age == p2.m_age) {
		return p1.m_height > p2.m_height;
	}
	else {
		//按照年龄 升序
		return p1.m_age < p2.m_age;
	}
}

void printPerson(const list<Person>& l) {
	for (auto item : l) {
		cout << "name: " << item.m_name
			<< " age: " << item.m_age
			<< " height: " << item.m_height
			<< endl;
	}
}

void test() {
	Person p1("echo", 20, 150);
	Person p2("lisa", 25, 170);
	Person p3("mesa", 20, 160);
	list<Person> l;
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	printPerson(l);

	l.sort(myCompare);
	cout << "排序后:" << endl;
	printPerson(l);

}

int main() {
	test();

	system("pause");
	return 0;
}


标签:10,200,容器,STL,printList,list,elem,l1
From: https://www.cnblogs.com/echolxl/p/16925951.html

相关文章

  • 03-3-淘宝每秒100W请求的秒杀架构体系(1)
                                           ......
  • Java高效找出两个大数据量List集合中的不同元素
    本文将带你了解如何快速的找出两个相似度非常高的List集合里的不同元素。主要通过JavaAPI、List集合双层遍历比较不同、借助Map集合查找三种方式,以及他们之间的执行效率情......
  • 列表list
    列表一共分三种一、有序列表使用ol标签来创建使用li表示列表项二、无序列表(用来做导航菜单)使用ul标签来创建使用li表示列表项三、定义列表使用dl标签来创建使用......
  • IDEA插件List of plugins was not loaded for following hosts: http://plugins.jetbr
    IDEA插件市场点开资源库后报错:Listofpluginswasnotloadedforfollowinghosts:http://plugins.jetbrains.com。解决办法:File-Settings-Plugins-Browserepositori......
  • ArrayList 与linkedlist区别
    ArrayList和Vector使用了数组的实现,可以认为ArrayList或者Vector封装了对内部数组的操作,比如向数组中添加,删除,插入新的元素或者数据的扩展和重定向。LinkedList使用了循环双......
  • datatable 转list
    C#DataTable转List方法,网上有好多,之前也收集了,感觉这个也不错,重要是自己要领会这里面的代码含义。接不来我就把代码贴出来分享一下,大家觉得如果不好,请留言我,我来改进。u......
  • python练习题:求10万以内的质数
    1、使用质数定义计算#version1importdatetime#导入模块计算效率start=datetime.datetime.now()count=0forxinrange(2,100000):#求指定范围内的质数......
  • Redis 数据类型list、hash、zset 原理、使用场景说明
    面试中常会被问到Redis的数据类型用过哪些,被问的多了就想整理一下,以被下次再被问时可以很好的回复,现在对redis中list、hash、zset进行梳理。一、listRedislist的实现......
  • 10、go的面向对象
    结构体结构体的定义//定义结构体,老师的结构体,各个属性统一放入结构体管理typeTeacherstruct{ //变量名大写开头外界可以访问 Namestring Ageint Scho......
  • ASEMI肖特基二极管SBT30100VCT体积,SBT30100VCT大小
    编辑-ZASEMI肖特基二极管SBT30100VCT参数:型号:SBT30100VCT最大重复峰值反向电压(VRRM):100V最大平均正向整流输出电流(IF):30A峰值正向浪涌电流(IFSM):250A每个元件的典型热阻(ReJA):2℃/......