C++——STL(list容器、set/multiset容器)

链表(list):
是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的链表的组成:链表由一系列结点组成

结点的组成:
一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域;
STL中的链表是一个双向循环链表
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器

list的优点:
采用动态存储分配,不会造成内存浪费和溢出,
链表执行插入和删除操作十分方便,可以在任意位置插入和删除元素,修改指针即可,不需要移动大量元素;

list的缺点:
链表灵活,占用空间比数组大,遍历比数组慢;
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

函数原型:
构造函数与vector相似

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

#include<iostream>
#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()
{
	//默认构造
	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);
	printlist(l2);

	//区间
	list<int>l3(l1.begin(), l1.end());
	printlist(l3);

	//直接赋值
	list<int>l4(10, 1000);
	printlist(l4);


}

int main()
{
	test();
	system("pause");
	return 0;

}

list赋值和交换:

assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem); //将n个elem拷贝赋值给本身。
list& operator=(const list &lst); //重载等号操作符
swap(lst); //将lst与本身的元素互换。

list大小操作:

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

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值匹配的元素。不需要提供迭代器,将链表中的所有elem删除。

list remove测试:

#include<iostream>
#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()
{
	list<int>l1(10,100);
	
	l1.push_back(10);
	l1.push_back(100);
	l1.push_back(20);
	l1.push_back(200);
	printlist(l1);
	l1.remove(100);
	printlist(l1);

}

int main()
{
	test();
	system("pause");
	return 0;

}

list数据存取:
不支持像vector一样的[]和at()方式访问;

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

验证迭代器是不是支持随机访问:
it = it+1;
it = it+3;
验证迭代器是不是支持双向访问:
it++;
it–;

list反转和排序:
所有不支持随机访问爹地啊其的容器,不可以用标准算法;
不支持随机访问迭代器的容器,内部会提供对应一些算法;
与vector不同,这里面使用的是list类中的成员函数,而不是sort和reverse的全局函数

reverse(); //反转链表
sort(); //链表排序

#include<iostream>
#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 myCompare(int a,int b)
{
	return a > b;
}
void test()
{
	list<int>l1;

	l1.push_back(10);
	l1.push_back(30);
	l1.push_back(15);
	l1.push_back(20);
	printlist(l1);
	//反转
	l1.reverse();
	printlist(l1);

	//升序
	l1.sort();      //默认是升序
	printlist(l1);

	//降序
	l1.sort(myCompare);   //改为降序
	printlist(l1);

}

int  main()
{
	test();
	system("pause");
	return 0;

}

排序案例:
按照年龄升序,年龄相同按照升高降序;

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

using namespace std;

class person {

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

	int m_age;
	int m_height;
	string m_name;
};

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;
	}
}

bool compare(person&p1, person&p2)
{
	if (p1.m_age == p1.m_age) {
		return p1.m_height > p2.m_height;
	}
	else {
		return p1.m_age < p2.m_age;
	}
}

void test()
{
	list<person>l1;

	person p1(23, 165, "A");
	person p2(18, 161, "B");
	person p3(33, 163, "C");
	person p4(17, 169, "D");
	person p5(17, 171, "E");
	person p6(17, 180, "F");
	person p7(8, 188, "G");

	l1.push_back(p1);
	l1.push_back(p2);
	l1.push_back(p3);
	l1.push_back(p4);
	l1.push_back(p5);
	l1.push_back(p6);
	l1.push_back(p7);
	
	cout << "排序前" << endl;
	printlist(l1);

	cout << "排序后" << endl;
	l1.sort(compare);
	printlist(l1);

}


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

set/multiset容器
所有元素都会在插入时自动被排序
set/multiset属于关联式容器,底层结构是用二叉树实现。

set构造和赋值

set st; //默认构造函数:
set(const set &st); //拷贝构造函数赋值:
set& operator=(const set &st); //重载等号操作符

set大小和交换

size(); //返回容器中元素的数目
empty(); //判断容器是否为空
swap(st); //交换两个集合容器

set插入和删除
没有push和pop使用insert进行插入数据;

insert(elem); //在容器中插入元素。
clear(); //清除所有元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem); //删除容器中值为elem的元素。

set查找和统计

find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key); //统计key的元素个数

#include<iostream>
#include <set>


using namespace std;

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

void test()
{
	set<int> s;

	s.insert(30);
	s.insert(20);
	s.insert(20);
	s.insert(40);
	s.insert(15);
	s.insert(30);

	printset(s);
}

void test02()
{
	//find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
	//count(key); //统计key的元素个数     返回要么是0要么就是1,因为set中不允许有重复元素

	set<int> s;

	s.insert(30);
	s.insert(20);
	s.insert(20);
	s.insert(40);
	s.insert(15);
	s.insert(30);

	printset(s);

	set<int>::iterator pos = s.find(300);
	if (pos != s.end()) {
		cout <<"找到元素:"<< *pos << endl;;
	}
	else {
		cout << "未找到该数据" << endl;
	}

	cout << "set 中有" << s.count(30) << "个30" << endl;
}

int main()
{

	test02();
	system("pause");
	return 0;

}

set和multiset区别:
set不允许容器中有重复的元素;
multiset允许容器中有重复的元素;
set插入数据的同时会返回插入结果,表示插入是否成功;
multiset不会检测数据,因此可以插入重复数据;
set在insert时返回的是pair对组,multiset在insert时返回的是迭代器;

#include<iostream>
#include <set>
using namespace std;

void test()
{
	set<int> s1;
	
	//第一次插入10
	pair<set<int>::iterator, bool>ret = s1.insert(10);
	if (ret.second) {
		cout << "第一次插入成功" << endl;
	}
	else {
		cout << "第二次插入失败" << endl;
	}
	
	//第二次插入
	pair<set<int>::iterator, bool>ret2 = s1.insert(10);
	if (ret2.second) {
		cout << "第二次插入成功" << endl;
	}
	else {
		cout << "第二次插入失败" << endl;
	}

}


int main()
{
	test();
	system("pause");
	return 0;

}

pair对组的创建:
成对出现的数据,利用对组可以返回两个数据;
不需要添加头文件
创建方式:

pair<type, type> p ( value1, value2 );
pair<type, type> p = make_pair( value1, value2 );

#include <iostream>
#include <string>

using namespace std;

void test()
{
	//第一种创建方式
	pair<int, string>p1(20, "caohai");
	cout << "first:" << p1.first << "   second:" << p1.second << endl;

	//第二种创建方式
	pair<int, string>p2 = make_pair(30, "haicao");
	cout << "first:" << p2.first << "   second:" << p2.second << endl;

}

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

set容器排序:
默认排序规则是从小到大,但是利用仿函数,可以改变排序规则

#include<iostream>
#include<set>
using namespace std;
class compare {

public:
	bool operator()(int data1,int data2)    //第一个小括号表示重载的符号,第二个括号代表参数列表
	{
		return data1 > data2;
	}
};


void test()
{
	set<int>s;
	s.insert(30);
	s.insert(20);
	s.insert(40);
	s.insert(10);
	s.insert(50);
	cout << "默认排序" << endl;
	for (set<int>::iterator it1 = s.begin(); it1 != s.end(); it1++) {
		cout << *it1 << " ";
	}
	cout << endl;


	set<int, compare>s1;    //因为set中插入数据之后就不能动,所以需要在插入之前加入compare仿函数

	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	s1.insert(10);
	s1.insert(50);
	cout << "使用仿函数更改排序顺序" << endl;
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

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

对于自定义类型的数据set排序需要指定排序规则,仿函数中声明进行比较的自定义类型参数:

#include<iostream>
#include<string>
#include<set>
using namespace std;
class person {
public:

	person(int age, string name) {
		this->m_age = age;
		this->m_name = name;
	}

	int m_age;
	string m_name;
};

class compare {
public:
	bool operator()(const person &p1,const person &p2) {
		return p1.m_age > p2.m_age;
	}
};

void test()
{
	set<person, compare>s;
	person p1(20, "A");
	person p2(30, "B");
	person p3(12, "C");
	person p4(13, "D");
	person p5(41, "E");

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);

	for (set<person>::iterator it = s.begin(); it != s.end(); it++) {
		cout << "name: " << it->m_name << "   age: " << it->m_age << endl;
	}
}

int main()
{

	test();
	system("pause");
	return 0;
}

learned from:黑马程序员

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值