C++STL(五)list容器

32 篇文章 0 订阅

list容器基本概念

链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

相较于vector的连续线性空间,list就显得负责许多,它的好处是每次插入或者删除一个元素,就是配置或者释放一个元素的空间。因此,list对于空间的运用有绝对的精准,一点也不浪费。而且,对于任何位置的元素插入或元素的移除,list永远是常数时间。

List和vector是两个最常被使用的容器。

List容器是一个双向链表。

  1. 动态存储分配,不会造成内存浪费和溢出
  2. 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
  3. 链表灵活,但是空间和时间额外耗费较大

 常用方法

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

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值匹配的元素。

assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。 

对组(pair)

对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有属性first和second访问。

类模板:template <class T1, class T2> struct pair.

//第一种方法创建一个对组

pair<string, int> pair1(string("name"), 20);

cout << pair1.first << endl; //访问pair第一个值

cout << pair1.second << endl;//访问pair第二个值

//第二种

pair<string, int> pair2 = make_pair("name", 30);

cout << pair2.first << endl;

cout << pair2.second << endl;

//pair=赋值

pair<string, int> pair3 = pair2;

cout << pair3.first << endl;

cout << pair3.second << endl;

 

示例 

#include<iostream>
#include<stack>
#include<queue>
#include<list>
#include<algorithm>

using namespace std;

class person {
	friend void printList(const list<person> L);
	friend bool myCompare(person p1, person p2);
private:
	string name;
	int age;
	double salary;
public:
	person(string name, int age, double salary) :name(name), age(age), salary(salary) {
	}

	bool operator==(person p) {
		if (this->name==p.name&&this->age==p.age&&this->salary==p.salary) {
			return true;
		}
		return false;
	}

	~person() {
	}
};

void printList(const list<person> L) {
	for (list<person>::const_iterator i = L.begin(); i != L.end(); ++i) {
		cout << "name:" << (*i).name << " age:" << (*i).age << " salary:" << (*i).salary << endl;
	}
}

bool myCompare(person p1,person p2) {
	if (p1.age == p2.age) {
		return p1.salary > p2.salary;
	}
	return p1.age > p2.age;
}

void test() {
	list<person> L1;
	person p1("Tom", 21, 7864);
	person p2("Sam", 33, 678);
	person p3("Tim", 19, 3454);
	person p4("David", 26, 4734);
	person p5("Jack", 77, 4734);
	person p6("Lucy", 26,5634);
	person p7("John", 26, 1234);
	person p8("Jim",26,1234);
	L1.push_back(p1);
	L1.push_back(p2);
	L1.push_back(p3);
	L1.push_back(p4);
	L1.push_front(p5);
	L1.push_front(p6);
	L1.push_front(p7);
	L1.insert(++L1.begin(), 1, p8);

	printList(L1);
	
	cout << "------------------------------" << endl;

	L1.sort(myCompare);
	printList(L1);

	cout << "------------------------------" << endl;

	L1.reverse();
	printList(L1);

	cout << "------------------------------" << endl;

	L1.erase(L1.begin());
	L1.pop_back();
	L1.pop_front();
	printList(L1);

	cout << "------------------------------" << endl;

	L1.clear();
	printList(L1);
}

int main() {
	test();
	system("pause");
	return EXIT_SUCCESS;
}

 执行结果

name:John age:26 salary:1234
name:Jim age:26 salary:1234
name:Lucy age:26 salary:5634
name:Jack age:77 salary:4734
name:Tom age:21 salary:7864
name:Sam age:33 salary:678
name:Tim age:19 salary:3454
name:David age:26 salary:4734
------------------------------
name:Jack age:77 salary:4734
name:Sam age:33 salary:678
name:Lucy age:26 salary:5634
name:David age:26 salary:4734
name:John age:26 salary:1234
name:Jim age:26 salary:1234
name:Tom age:21 salary:7864
name:Tim age:19 salary:3454
------------------------------
name:Tim age:19 salary:3454
name:Tom age:21 salary:7864
name:Jim age:26 salary:1234
name:John age:26 salary:1234
name:David age:26 salary:4734
name:Lucy age:26 salary:5634
name:Sam age:33 salary:678
name:Jack age:77 salary:4734
------------------------------
name:Jim age:26 salary:1234
name:John age:26 salary:1234
name:David age:26 salary:4734
name:Lucy age:26 salary:5634
name:Sam age:33 salary:678
------------------------------
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值