C++ STL容器 复习例子

1.vector容器

1.1 嵌套容器

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

void Myprint(int val)
{
	cout << val << endl;
}
void test01()
{
	vector<vector<int>> v;

	//创建小容器
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;

	//向小容器中添加数据
	for (auto i = 0; i < 4; i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}

	//将小容器插入到大容器中
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);

	//大容器把所有数据遍历一遍
	for (auto it = v.begin(); it != v.end(); it++)
	{
		//*it是容器vector<int>
		for (auto vit = (*it).begin(); vit != (*it).end(); vit++)
		{
			cout << *vit << " ";
		}
		cout << endl;
	}

}
int main() {
	test01();

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

1.2. 用swap收缩内存空间

  1. 产生一个匿名对象,那么按照v1的大小,编译器产生合适的容量
vector<int>(v1)
  1. 容量交换,那么将小容量从匿名对象交换给v1
.swap(v1)

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

void Myprint(vector<int> &v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << "  ";
	}
	cout<< endl;
}
void test01()
{
	vector<int> v1;

	for (int i = 0; i < 1000; i++)
	{
		v1.push_back(i);
	}
	cout << "v的容量: " << v1.capacity() << endl;
	cout << "v的大小: " << v1.size() << endl;

	//重新指定大小, 容量不会改变
	v1.resize(3);
	cout << "v的容量: " << v1.capacity() << endl;
	cout << "v的大小: " << v1.size() << endl;

	//巧用swap收缩内存!!
	vector<int>(v1).swap(v1);

	cout << "v的容量: " << v1.capacity() << endl;
	cout << "v的大小: " << v1.size() << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
};

1.3.vector容器预留空间

容器预留len个元素额长度,预留未知不初始化,元素不可访问

reserve(int len); 

2. deque容器 排序算法

排序 默认排序规则 从小到大升序;
对于支持随机访问的迭代器的容器,vector等,都可以使用sort算法对其进行排序;

#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>

void printDeque(const deque<int>& d)
{
	for (auto it = d.begin(); it != d.end(); it++)
	{
		//*it = 100;//容器中的数据不可修改,因为迭代器是deque<int>::const iterator
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int> d1;
	for(int i=0;i<8;i++)
	{
		d1.push_front(i);
	}
	for (int i = 0; i < 8; i++)
	{
		d1.push_back(i);
	}
	printDeque(d1);

	//排序 默认排序规则 从小到大升序
	//对于支持随机访问的迭代器的容器,vector等,都可以使用sort算法对其进行排序
	sort(d1.begin(),d1.end());
	
	printDeque(d1);
}
int main() {
	test01();
	system("pause");
	return 0;
};

3.容器用例一

10评委给5个选手打分,去掉最高分和最低分,得出平均分

#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
#include<string>
#include<vector>
#include<ctime>

class Person
{
public:
	Person() = default;
	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;
	}

public:
	string m_Name;
	int m_Score;
};

void creatPerson(vector<Person> &v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i ++)
	{
		string name = "选手";
		name += nameSeed[i];
		int score = 0;
		Person p(name, score);

		//将创建的Person对象 放入到容器中
		v.push_back(p);
	}
}
void setScore(vector<Person>& v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		//将评委的分数放到deque容器中
		deque<int>d;
		for (size_t i = 0; i < 10; i++)
		{
			int score = rand()%41 +60 ;
			d.push_back(score);
		}

		//排序
		sort(d.begin(), d.end());

		//cout << "姓名:" << (*it).m_Name << "   分数:" << (*it).m_Score << endl;
		输出排序后的打分
		//for (auto ddit = d.begin(); ddit != d.end(); ddit++)
		//{
		//	cout << (*ddit) << " ";
		//}
		//cout << endl;
		
		//去掉最高分和最低分
		d.pop_back();
		d.pop_front();

		//取平均分
		int sum = 0;
		for (auto dit = d.begin(); dit != d.end(); dit++)
		{
			sum += (*dit) ;
		}
		int avg = sum / d.size();

		//将平均分赋给选手身上
		(*it).m_Score = avg;


	}
}

void showResult(vector<Person>& v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "   分数:" << (*it).m_Score << endl;
	}
}
void test01()
{	
	//随机数种子
	srand((unsigned int)time(NULL));

	//1.创建5名选手
	vector<Person> v;//存放选手的容器
	creatPerson(v);
	
	//2.给5人打分
	 setScore(v);

	 //3.展示分数
	 showResult(v);

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

4.List排序

  1. 所有不支持随机访问迭代器的容器,都不可以使用标准算法;而是使用容器内部 成员函数 提供的算法
list<int>L!;
#include<iostream>
using namespace std;
#include<list>

void printList(const list<int> L)
{
	for (auto it = L.begin(); it != L.end(); it++)
	{
		cout << (*it) << endl;
	}
	cout << "输出完成" << endl;	
}
//指定排序规则
bool myCompare(int val1, int val2)
{
	return val1 > val2;
}

void test01()
{	
	list<int>L1;
	L1.push_back(40);
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(80);

	//反转容器的元素
	L1.reverse();
	printList(L1);

	//排序容器中的元素 升序(默认)
	L1.sort();
	printList(L1);

	//改变排序规则 降序(默认)
	L1.sort(myCompare);
	printList(L1);
}
int main() {
	test01();
	system("pause");
	return 0;
};

5.set和multiset区别

  • set不可以插入重复数据,插入数据同时会返回插入的结果 bool;
  • multiset不会检测数据,因此可以插入重复数据,用count()查找重复出现的次数
#include<iostream>
using namespace std;
#include<set>

template<typename T>
void printList(T L)
{
	for (auto it = L.begin(); it != L.end(); it++)
	{
		cout << (*it)<<"  ";
	}
	cout  << endl;	
}


void test01()
{	
	set<int>s;
	for (size_t i = 0; i != 2; i++)
	{
		pair<set<int>::iterator, bool> ret = s.insert(10);
		if (ret.second)
		{
			cout << "第"<<i+1<<"次插入成功" << endl;
		}
		else
		{
			cout << "第" << i + 1 << "不成功" << endl;
		}
		printList(s);
	}
}
void test02() 
{
	multiset<int>ms;
	ms.insert(20);
	ms.insert(20);
	ms.insert(30);
	printList(ms);
}
void test03()
{
	multiset<int> ms2;
	ms2.insert(1);
	ms2.insert(2);
	ms2.insert(3);
	ms2.insert(2);
	ms2.insert(9);
	ms2.insert(2);
	printList(ms2);
	cout <<"2出现的次数为:" << (ms2.count(2)) << endl;
}

int main() {
	test01();
	test02();
	test03();
	system("pause");
	return 0;
};

5.2.set容器自定义数据类型排序

  • 对于自定义类型,或者自定义排序方式,利用仿函数指定规则
#include<iostream>
using namespace std;
#include<set>

class Person
{
public:
	Person(string name, int age) :m_Name(name),m_Age(age){}
	
public:
	string m_Name;
	int m_Age;
};

//仿函数 指定排序规则
class ComparePerson
{
public:
	bool operator()(const Person&p1,const Person&p2) const
		//加const限定符,防止编译器错误 C3848:
		//具有指定 const-volatile 类型的变量只能调用使用相同或更大的 const-volatile 限定定义的成员函数。
	{
		//自定义排序规则
		return p1.m_Age > p2.m_Age;
	}
};

void printList(const set<Person, ComparePerson>& s)
{
	for (auto it = s.begin(); it != s.end(); it++)
	{
		cout << (*it).m_Name << "  " << (*it).m_Age << endl;
	}

}
void test01()
{	//自定义数据结构,都会指定排序规则
	set<Person, ComparePerson> s;
	Person p1("AA", 23);
	Person p2("BB", 24);
	Person p4("VV", 25);
	Person p3("CC", 21);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	printList(s);
}


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

6.pair创建对组

·pair<type,type> p(value1, value2);

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

7.容器用例二

  • 美术、策划、研发三组员工(姓名 工资)
#include<iostream>
using namespace std;
#include<set>
#include<string>
#include<vector>
#include<map>
#include<ctime>

#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker
{
public:
	string m_Name;
	int m_Salary;
};
void createWorker(vector<Worker>& w)
{
	string nameSeed = "ABCDEFGHIJ";
	for (auto i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];
		worker.m_Salary = rand() % 10000 + 10000; //10000~19999
		//员工放到vector中
		w.push_back(worker);
	}
}
void printWorker(const vector<Worker>&v)
{
	for (auto i = v.begin(); i < v.end(); i++)
	{
		cout << (*i).m_Name << "  " << (*i).m_Salary << endl;
	}
}
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
	for (auto it = v.begin(); it !=v.end(); it++)
	{
		//随机产生部门编号
		int depId = rand() % 3;//0~2

		//将员工插入到分组中
		m.insert(make_pair(depId, *it));
	}
}
void 	printWorkByGroup( multimap<int, Worker>& m)
{
	cout << "策划部门:" << endl;
	multimap<int, Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA);//统计具体人数
	int index = 0;
	for (; pos != m.end()&&index!=count; pos++,index++)
	{
		cout << "姓名: " << pos->second.m_Name <<"  " << "工资: " << pos->second.m_Salary << endl;
	}
	cout << "美术部门:" << endl;
	 pos = m.find(MEISHU);
	 count = m.count(MEISHU);//统计具体人数
	 index = 0;
	for (; pos != m.end() && index != count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << "  " << "工资: " << pos->second.m_Salary << endl;
	}
	cout << "研发部门:" << endl;
	pos = m.find(YANFA);
	count = m.count(YANFA);//统计具体人数
	index = 0;
	for (; pos != m.end() && index != count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << "  " << "工资: " << pos->second.m_Salary << endl;
	}

}
void test01()
{	
	//1.创建员工
	vector<Worker> vWorker;
	createWorker(vWorker);
	
	//2.员工分组
	multimap<int, Worker>mWorker;
	setGroup(vWorker, mWorker);


	//测试
	printWorker(vWorker);
	printWorkByGroup(mWorker);
}


int main() {
	srand((unsigned int)time(NULL));
	test01();
	system("pause");
	return 0;
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值