标准模板库 STL(Standard Template Library)

 

 

容器算法迭代器初识

了解STL容器,算法,迭代器概念之后,我们利用代码感受STL的魅力

STL中最常用的容器为Vector,可以理解为数组,下面我们将学习如何向这个容器中插入数据,并遍历这个容器

vector存放内置数据类型

容器:vector

算法:for_each

迭代器:vector<int>::iterator

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//标准算法的头文件
//vector 容器存放内置数据类型

void myprint(int val)
{
	cout << val << endl;
}


void test01()
{
	//创建了一个vector容器,数组
	vector<int>v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	//通过迭代器来访问容器中的数据
	//vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
	//vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个位置

	第一种遍历方式
	//while (itBegin != itEnd)
	//{
	//	cout << *itBegin << endl;
	//	itBegin++;
	//}

	//第二种遍历方式
	/*for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}*/

	//第三种遍历方式 利用STL中提供的遍历算法
	for_each(v.begin(), v.end(), myprint);

}
int main()
{
	test01();

	return 0;
}

Vector存放自定义数据类型

学习目标:vector中存放自定义数据类型,并打印输出

#include<iostream>
using namespace std;
#include<vector>
#include<string>
//vector中存放自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

void test01()
{
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	//遍历容器中的数据
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		//cout << "姓名: " << (*it).m_Name << " 年龄: " << (*it).m_Age << endl;
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
	}

}
//存放自定义数据类型 指针
void test02()
{
	vector<Person*>v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eee", 50);

	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);

	//遍历容器
	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl;
	}

}
int main()
{

	//test01();
	test02();
	return 0;
}

 也可以这样表示

	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
	{
		Person* p = (*it);
		cout << "Name:" << p->m_Name << "Age:" << (*it)->m_Age << endl;
		//cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl;
	}

Vector容器嵌套容器

void test01()
{
	vector<vector<int>>v;

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

	//向小容器中添加数据
	for (int 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 (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
	{
		//(*it) --- 容器vector<int>
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
		{
			cout << *vit << " ";

		}
		cout << endl;
	}
	
}
int main()
{
	test01();
	return 0;
}

#include<string>
//string的构造函数
void test01()
{
	string s1;//默认构造

	const char* str = "hello world";
	string s2(str);

	cout << "s2= " << s2 << endl;

	string s3(s2);
	cout << "s3= " << s3 << endl;

	string s4(10, 'a');
	cout << "s4= " << s4 << endl;
}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string str1;
	str1 = "hello world";
	cout << "str1= " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2= " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3= " << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4= " << str4 << endl;

	string str5;
	str5.assign("hello c++", 5);
	cout << "str5= " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6= " << str6 << endl;

	string str7;
	str7.assign(10, 'w');
	cout << "str7= " << str7 << endl;

}
int main()
{
	test01();

	return 0;
}

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1= " << str1 << endl;

	str1 += ":";
	cout << "str1= " << str1 << endl;

	string str2 = "LOL DNF";
	str1 += str2;
	cout << "str1= " << str1 << endl;

	string str3 = "I";
	str3.append("love");
	cout << "str3= " << str3 << endl;

	str3.append("game abcde", 4);
	cout << "str3= " << str3 << endl;

	str3.append(str2);
	cout << "str3= " << str3 << endl;

	str3.append(str2, 0, 3);
	//截取到LOL
	str3.append(str2, 4, 3);
	//截取DNF 参数2是从哪个位置开始截取 参数三截取的字符个数
}
int main()
{
	test01();

	return 0;
}

#include<iostream>
using namespace std;
#include<string>
//1.查找
void test01()
{ 
	string str1 = "abcdefg";
	int pos=str1.find("de");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串,pos= " << pos << endl;
	}
	cout << "pos= " << pos << endl;
	//rfind和find的区别
	//rfind 从右往左查找,find从左往右查找

	pos = str1.rfind("de");
	cout << "pos= " << pos << endl;
}
//2.替换
void test02()
{
	string str1 = "abcdefg";
	str1.replace(1, 3, "1111");//从一号位置起 3个字符替换为1111
	cout << "str1= " << str1 << endl;
}
int main()
{
	//test01();
	test02();

	return 0;
}

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string str1 = "hello";
	string str2 = "xello";
	if (str1.compare(str2) == 0)
	{
		cout << "str1 等于 str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1 大于 str2" << endl;
	}
	else
	{
		cout << "str1 小于 str2" << endl;
	}
}
int main()
{
	test01();


	return 0;
}

 

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	string str1 = "hello";
	cout << "str1= " << str1 << endl;

	//1.通过[]来访问单个字符的内容
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
	//2.通过at的方式访问单个字符
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1.at(i) << " ";
	}
	cout << endl;

	//修改单个字符
	str1[0] = 'x';
	cout << "str= " << str1 << endl;
	str1.at(1) = 'x';
	cout << "str= " << str1 << endl;
}
int main()
{
	test01();
	return 0;
}

 

#include<string>
//字符串的插入和删除
void test01()
{
	string str = "hello";
	//插入
	str.insert(1, "111");
	cout << "str=" << str << endl;

	//删除
	str.erase(1,3);
	cout << "str=" << str << endl;
}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<string>
//string求子字符串
void test01()
{
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;
	
}
//实用操作
void test02()
{
	string email = "zhangsan@sina.com";
	//从邮件地址中 获取用户名的信息
	int pos = email.find("@");// 8
	string userName = email.substr(0, pos);
	cout << userName << endl;
}
int main()
{
	test01();
	return 0;
}

vector容器

vector容器的概念:

功能:vector数据结构和数组非常相似,也称为单端数组

vector与普通数组的区别是:

不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

并不是在原空间之后续新空间,而是找更大的内存空间,然后讲原数据拷贝新空间,释放原空间

 vector容器的迭代器是支持随机访问的迭代器

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void printVector(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;//默认构造 无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	//通过区间方式进行构造
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);

	//n个elem方式构造
	vector<int>v3(10, 100);//10个100
	printVector(v3);

	//拷贝构造
	vector<int>v4(v3);
	printVector(v4);
}

int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void printVector(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//vector赋值
void test01()
{
	vector<int>v1;//默认构造 无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	//赋值 operator=
	vector<int>v2;
	v2 = v1;
	printVector(v2);

	//assign
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	//n个elem方式赋值
	vector<int>v4;
	v4.assign(10, 100);
	printVector(v4);
	
}

int main()
{
	test01();
	return 0;
}

 

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void print(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	print(v1);
	if (v1.empty())//为真 代表容器为空
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空" << endl;
		cout << "v1的容量为:" << v1.capacity() << endl;
		cout << "v1的大小为:" << v1.size() << endl;
	}
	//重新指定大小
	v1.resize(15,100);//利用重载版本 可以指定默认填充值 参数2
	print(v1);//如果重新指定的比原来的长了 默认用0填充新的位置

	v1.resize(5);
	print(v1);//如果重新指定的比原来的短了 那么其他部分会被删除
}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void print(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);

	//遍历
	print(v1);

	//尾删
	v1.pop_back();
	print(v1);

	//插入
	v1.insert(v1.begin(), 100);
	print(v1);

	v1.insert(v1.begin(), 2, 1000);
	print(v1);

	//删除 参数也是迭代器
	v1.erase(v1.begin());
	print(v1);

	//类似清空 v1.clear()
	v1.erase(v1.begin(), v1.end());
	print(v1);
}

 

#include<iostream>
using namespace std;
#include<string>
#include<vector>
void print(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";

	}
	cout << endl;

	//利用at方式访问元素
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";

	}
	cout << endl;
	//获取第一个元素
	cout << "第一个元素为:" << v1.front() << endl;
	//获取最后一个元素
	cout << "最后一个元素为:" << v1.back() << endl;
}
int main()
{
	test01();
	return 0;
}

using namespace std;
#include<string>
#include<vector>
void print(vector<int>&v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//基本使用
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "交换前: " << endl;
	print(v1);
	vector<int>v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	print(v2);
	cout << "交换后:" << endl;
	v1.swap(v2);
	print(v1);
	print(v2);
}
//实际用途
//巧用swap可以收缩内存空间
void test02()
{
	vector<int>v;
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
	}
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;

	v.resize(3);//重新指定大小
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;

	//vector<int>(v)匿名对象->特点 当前行执行完直接回收
	vector<int>(v).swap(v);
	cout << "v的容量为: " << v.capacity() << endl;
	cout << "v的大小为: " << v.size() << endl;

}

int main()
{
	test01();
	test02();
	return 0;
}

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

void test01()
{
	vector<int>v;
	//利用reserve来预留空间
	v.reserve(100000);
	int num = 0;//统计开辟次数
	int* p = NULL;
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
		if (p != &v[0])
		{
			p = &v[0];
			num++;
		}
	}
	cout << "num= " << num << endl;
}

int main()
{
	test01();
	
	return 0;
}

如果数据量较大,可以一开始利用reserve预留空间

deque容器

deque容器基本概念

功能:双端数组,可以对头端进行插入删除操作

deque和vector区别:

vector对于头部的插入删除效率低,数据量越大,效率越低

deque相对而言,对头部的插入删除速度会比vector快

vector访问元素时的速度会比deque快,这和两者内部实现有关

deque内部工作原理:

deque容器的迭代器也是支持随机访问的

 

#include<iostream>
using namespace std;
#include<deque>
void printdeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		//*it = 100; 容器中的数据不可以修改了
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printdeque(d1);

	deque<int>d2(d1.begin(), d1.end());
	printdeque(d2);

	deque<int>d3(10, 100);
	printdeque(d3);

	deque<int>d4(d3);
	printdeque(d4);
}
//deque 构造函数
int main()
{
	test01();
	return 0;
}

#include<deque>
//deque容器赋值操作

void printdeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printdeque(d1);

	// operator = 赋值
	deque<int>d2;
	d2 = d1;
	printdeque(d2);

	//assign赋值
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	printdeque(d3);

	deque<int>d4;
	d4.assign(10, 100);
	printdeque(d4);
}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<deque>
//deque容器赋值操作

void printdeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	printdeque(d1);
	if (d1.empty())
	{
		cout << "d1为空" << endl;
	}
	else
	{
		cout << "d1不为空" << endl;
		cout << "d1的大小为: " << d1.size() << endl;
		//deque没有容量的概念
	}
	//重新指定大小
	d1.resize(15,1);
	printdeque(d1);

	d1.resize(5);
	printdeque(d1);
}
int main()
{
	test01();
	return 0;
}

#include<deque>
//deque容器插入和删除

void printdeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);

	d1.push_front(100);
	d1.push_front(200);


	printdeque(d1);
	d1.pop_back();

	d1.pop_front();
	printdeque(d1);
}

void test02()
{
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);

	d1.push_front(100);
	d1.push_front(200);

	//insert
	d1.insert(d1.begin(), 1000);

	d1.insert(d1.begin(), 2,10000);

	//按照区间进行插入
	deque<int>d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);

	d1.insert(d1.begin(), d2.begin(), d2.end());

	printdeque(d1);

}
void test03()
{
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);

	d1.push_front(100);
	d1.push_front(200);
	//删除
	deque<int>::iterator it = d1.begin();
	it++;
	d1.erase(it);
	printdeque(d1);

	d1.erase(d1.begin(), d1.end());
	printdeque(d1);
	d1.clear();

}
int main()
{
	test01();
	return 0;
}

void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);

	//通过【】方式访问元素
	for (int i = 0; i < d.size(); i++)
	{
		cout << d[i] << " ";
	}
	cout << endl;
	//通过at的方式访问元素
	for (int i = 0; i < d.size(); i++)
	{
		cout << d.at(i) << " ";
	}
	cout << endl;

	cout << "第一个元素为: " << d.front() << endl;
	cout << "最后一个元素为: " << d.back() << endl;
}

#include<deque>
#include<algorithm>//标准算法头文件
void printdeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(100);
	d.push_front(200);
	d.push_front(300);

	printdeque(d);
	//排序 默认从小到大
	//对于支持随机访问的迭代器的容器 都可以利用sort算法直接对其进行排序
	//vectoe容器也可以利用 sort进行排序
	sort(d.begin(), d.end());
	cout << "排序后: " << endl;
	printdeque(d);
}
int main()
{
	test01();
	return 0;
}

案例-评委打分

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<string>
#include<deque>
#include<ctime>
//选手类
class Person
{
public:
	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Score = score;

	}
	string m_Name;
	int m_Score;

};
void createPerson(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 (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		//将评委的分数 放入到deque的容器当中
		deque<int>d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;//60-100
			d.push_back(score);
		}
		cout << "选手: " << it->m_Name << "打分: " << endl;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			cout << *dit << " ";

		}
		cout << endl;
		//排序
		sort(d.begin(), d.end());

		//去除最高和最低分
		d.pop_back();
		d.pop_front();

		//取平均分
		int sum = 0;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;//累加每个评委的分数
		}
		int avg = sum / d.size();

		//将平均分 赋值给选手身上
		it->m_Score = avg;

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


	//1.创建5名选手
	vector<Person>v;//存放选手的容器
	createPerson(v);
	测试
	//for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	//{
	//	cout << "姓名: " << (*it).m_Name << "分数: " << (*it).m_Score << endl;
	//}
	//2.给5名选手打分
	setScore(v);
	//3.显示最后得分
	showScore(v);
	return 0;
}

stack容器

 栈中进入数据称为 --- 入栈 push

栈中弹出数据称为 --- 出栈 pop

生活中的栈(弹夹  挤地铁)

#include<iostream>
using namespace std;
#include<stack>
//stack容器
void test01()
{
	//特点:符合先进后出的数据结构
	stack<int>s;
	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	//只要栈不为空 我们就查看栈顶 并且执行出栈操作
	while (!s.empty())
	{
		//查看栈顶元素
		cout << "栈顶元素为:" << s.top() << endl;

		//出栈
		s.pop();
	}
	cout << "栈的大小:" << s.size() << endl;
}
int main()
{
	test01();
	return 0;
}

 queue容器

 队列容器允许一端新增元素,从另一端移除元素

队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为

队列中进数据称为 --- 入队 push

队列中出数据称为 --- 出队 pop

生活中的队列:排队

queue常用接口

#include<iostream>
using namespace std;
#include<queue>
#include<string>
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;

	}
	string m_Name;
	int m_Age;
};
void test01()
{
	//创建队列
	queue<Person>q;

	//准备数据
	Person p1("1", 11);
	Person p2("2", 22);
	//入队
	q.push(p1);
	q.push(p2);

	//判断队列不为空 看队头 队尾 查看队尾 出队
	cout << "队列大小为: " << q.size() << endl;
	while (!q.empty())
	{
		//查看队头
		cout << "队头元素 --- 姓名:" << q.front().m_Name << " 年龄:" << q.front().m_Age << endl;
		//查看队尾
		cout << "队尾元素 --- 姓名:" << q.back().m_Name << " 年龄:" << q.back().m_Age << endl;
		//出队
		q.pop();
	}
	cout<<"队列大小为: "<<q.size()<<endl;

}
int main()
{
	test01();
	return 0;
}

 list容器

 

 

 

#include<iostream>
#include<list>
using namespace std;
//list容器构造函数
void printList(const list<int>&L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	//创建list容器
	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.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(L2);
	printList(L3);

	//n个elem
	list<int>L4(10, 1000);
	printList(L4);
}
int main()
{
	test01();

	return 0;
}

#include<iostream>
using namespace std;
#include<list>
//list容器赋值和交换
void printlist(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printlist(L1);
	list<int>L2;
	L2 = L1;
	printlist(L2);

	list<int>L3;
	L3.assign(L2.begin(),L2.end());
	printlist(L2);

	list<int>L4;
	L4.assign(10, 100);
	printlist(L4);
}
//交换
void test02()
{
	list<int>L1;

	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);
	cout << "交换前: " << endl;
	printlist(L1);
	printlist(L2);
	L1.swap(L2);
	cout << "交换后: " << endl;
	printlist(L1);
	printlist(L2);
}

int main()
{
	//test01();
	test02();
	return 0;
}

#include<list>
//list容器大小的操作
void printlist(const list<int>&L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printlist(L1);
	//判断容器是否为空
	if (L1.empty())
	{
		cout << "L1为空" << endl;

	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的元素个数为: " << L1.size() << endl;

	}
	//重新指定大小
	L1.resize(10,2);
	printlist(L1);

	L1.resize(2);
	printlist(L1);
}
int main()
{
	test01();

	return 0;
}

//容器的插入和删除
#include<list>
void printlist(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	list<int>L;
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	
	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);

	//300 200 100 10 20 30
	printlist(L);

	//尾删
	L.pop_back();
	printlist(L);

	//头删
	L.pop_front();
	printlist(L);

	//insert
	L.insert(L.begin(), 1000);
	printlist(L);

	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	//200 1000 100 10 20
	printlist(L);

	//删除
	it = L.begin();
	L.erase(++it);
	printlist(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	printlist(L);
	L.remove(10000);
	printlist(L);
}
int main()
{
	test01();
	return 0;
}

list数据存取

对list容器中数据进行存取

函数原型:

front();//返回第一个元素

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

#include<iostream>
using namespace std;
#include<list>
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//不可以用 【】 at去访问容器中中的元素 原因是list本质是链表 不是用连续线性空间存储数据
	//迭代器也是不支持随机访问的
	cout << "第一个元素为: " << L1.front() << endl;
	cout << "最后一个元素为:  " << L1.back() << endl;
	//验证迭代器是不支持随机访问的
	list<int>::iterator it = L1.begin();
	it++;
	//it = it+1不能这样写因为不支持随机访问  支持双向
	it--;
	//可以这样来验证 一个容器支不支持随机访问
}
int main()
{
	test01();
	return 0;
}

list反转和排序

将容器中的元素反转,以及将容器中的数据进行排序

函数原型:

reverse(); //反转链表

sort();//链表排序

#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
void printlist(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	cout << "翻转前: " << endl;
	printlist(L1);
	L1.reverse();
	cout << "翻转后: " << endl;
	printlist(L1);


}
bool myCompare(int v1,int v2)
{
	//降序 就让第一个数》第二个数
	return v1 > v2;
}
//排序
void test02()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(90);
	L1.push_back(30);
	L1.push_back(40);

	//排序前
	cout << "排序前: " << endl;
	printlist(L1);

	//所有不支持随机访问迭代器的容器 不可以用标准算法
	//不支持随机访问迭代器的容器 内部会提供对应一些算法

	cout << "排序后: " << endl;
	//sort(L1.begin(), L1.end());
	L1.sort();
	printlist(L1);

	L1.sort(myCompare);
	printlist(L1);

}
int main()
{
	//test01();
	test02();
	return 0;
}

排序案例:

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名,年龄,身高

排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

#include<iostream>
using namespace std;
#include<string>
#include<list>
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 comparePerson(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 test01()
{
	list<Person>L;//创建容器
	Person p1("1", 11, 111);
	Person p2("2", 22, 222);
	Person p3("3", 33, 333);
	Person p4("4", 11, 444);
	Person p5("5", 55, 555);

	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << (*it).m_Height << endl;
	}
	cout << " ----------------------------------------" << endl;
	cout << " 排序后:" << endl;
	L.sort(comparePerson);
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄:" << it->m_Age << " 身高:" << (*it).m_Height << endl;
	}
}
int main()
{
	test01();

	return 0;
}

set容器 

 

 

#include<iostream>
using namespace std;
#include<set>
void print(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	//插入方式只有insert的方式
	s1.insert(10);
	s1.insert(20);
	s1.insert(90);
	s1.insert(30);
	s1.insert(40);
	s1.insert(40);
	//遍历容器
	//特点:所有呀u盛怒插入的时候自动被排序
	//set容器不允许插入重复值
	print(s1);
	//拷贝构造
	set<int>s2(s1);
	print(s2);

	set<int>s3;
	s3 = s2;
	print(s3);
}
int main()
{
	test01();
	return 0;
}

set容器大小和交换

统计set容器大小以及交换set容器

函数原型:

size();//返回容器中元素的数目

empty();//判断容器是否为空

swap(st);//交换两个集合容器

#include<iostream>
using namespace std;
#include<set>
void printset(const set<int>& s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	printset(s1);
	if (s1.empty())
	{
		cout << "容器为空!" << endl;
	}
	else
	{
		cout << "容器不为空!" << endl;
		cout << "容器大小为: " << s1.size() << endl;
	}

}
void test02()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>s2;
	s2.insert(100);
	s2.insert(200);
	s2.insert(300);
	s2.insert(400);

	cout << "交换前: " << endl;
	printset(s1);
	printset(s2);
	cout << "交换后: " << endl;
	s1.swap(s2);
	printset(s1);
	printset(s2);

}
int main()
{
	//test01();
	test02();

	return 0;
}

#include<iostream>
using namespace std;
#include<set>
//set容器 插入和删除
void printset(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

	//遍历
	printset(s1);

	//删除
	s1.erase(s1.begin());
	printset(s1);//删掉10就算你把10放在最后也一样是删除10 因为他是最后一个元素

	//删除重载版本
	s1.erase(30);
	printset(s1);

	//清空
	//s1.erase(s1.begin(), s1.end());
	s1.clear();
	printset(s1);

}
int main()
{
	test01();
	return 0;
}

set容器的查找和统计

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

count(key);//统计key的元素个数

#include<set>

void printset(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	//查找
	set<int>s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

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

}
//统计
void test02()
{
	
	set<int>s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(30);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);

	int num = s1.count(30);
	//对于set而言要不1要不是0
	cout << "num = " << num << endl;
}
int main()
{
	//test01();
	test02();
	return 0;
}

 

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

void printset(multiset<int>& s)
{
	for (multiset<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	//插入
	pair<set<int>::iterator,bool> ret = s1.insert(10);
	if (ret.second)//看第二个数据 也就是看bool
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	/*s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);*/
	ret = s1.insert(10);
	if (ret.second)//看第二个数据 也就是看bool
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}
	multiset<int>ms;
	//允许插入重复值
	ms.insert(10);
	ms.insert(10);
	ms.insert(10);
	ms.insert(10);
	printset(ms);
}
int main()
{
	test01();
	
	return 0;
}

pair对组创建

功能描述:

成对出现的数据,利用对组可以返回两个数据

两种创建方式:

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

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

#include<iostream>
using namespace std;
#include<string>
void test01()
{
	//第一种方式
	pair<string, int>p("Tom", 20);

	cout << "姓名: " << p.first << " 年龄: " << p.second << endl;

	//第二种方式
	pair<string, int>p2 = make_pair("Jerry", 30);
	cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
#include<set>
//set容器排序
class MyCompare
{
	//vS2022报错是因为MyCompare中的operator()必须是const成员函数 也就是要写成bool operator()(int v1,int v2)const
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};
void test01()
{
	set<int>s1;
	s1.insert(50);
	s1.insert(30);
	s1.insert(60);
	s1.insert(80);
	s1.insert(10);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//指定排序规则为从大到小

	set<int, MyCompare>s2;
	s2.insert(50);
	s2.insert(30);
	s2.insert(60);
	s2.insert(80);
	s2.insert(10);

	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	return 0;
}
#include<iostream>
using namespace std;
#include<string>
#include<set>
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	string m_Name;
	int m_Age;
};
class comparePerson
{
public:
	bool operator()(const Person&p1,const Person&p2) const
	{
		//按照年龄降序
		return p1.m_Age > p2.m_Age;
	}
};
void test01()
{
	//自定义的数据类型 都会指定排序规则
	set<Person, comparePerson>s;
	Person p1("1", 11);
	Person p2("2", 22);
	Person p3("5", 55);
	Person p4("6", 19);
	
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

	for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
	}

}
int main()
{
	test01();
	return 0;
}

 

#include<iostream>
using namespace std;
#include<map>
//map容器 构造和赋值
void printmap(map<int,int>&m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << (*it).first << "value =" << it->second << endl;
	}
	cout << endl;
}
void test01()
{
	//创建map容器
	map<int, int>m;
	//会按照key来排序
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(4, 3));
	printmap(m);

	//拷贝构造
	map<int, int>m2(m);
	printmap(m2);
	//赋值
	map<int, int>m3;
	m3 = m2;
	printmap(m3);
}
int main()
{

	test01();
	return 0;
}

map大小和交换

函数原型:

size();//返回容器中元素的个数

empty();//判断容器是否为空

swap(st);交换两个集合容器

#include<iostream>
using namespace std;
#include<map>
void test01()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	if (m.empty())
	{
		cout << "m为空" << endl;
	}
	else
	{
		cout << "m不为空" << endl;
		cout << "m的大小为: " << m.size() << endl;
	}
}
void printmap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "value = " << it->second << endl;
	}
	cout << endl;
}
void test02()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int, int>m2;
	m2.insert(pair<int, int>(4, 100));
	m2.insert(pair<int, int>(5, 200));
	m2.insert(pair<int, int>(6, 300));

	cout << "交换前: " << endl;
	printmap(m);
	printmap(m2);
	cout << "交换后: " << endl;
	m.swap(m2);
	printmap(m);
	printmap(m2);
}
int main()
{
	//test01();
	test02();
	return 0;
}

map容器插入和删除

map容器进行插入数据和删除数据

insert(elem)//在容器中插入元素

clear();//清除所有元素

erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器

erase(beg,end);//删除区间【beg,end】所有元素,返回下一个元素的迭代器。

erase(key);//删除容器中值为key的元素。

#include<iostream>
using namespace std;
#include<map>
void printmap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "value = " << it->second << endl;
	}
	cout << endl;
}
void test01()
{
	map<int, int>m;
	//插入第一种方法
	m.insert(pair<int, int>(1, 10));
	//第二种方法、
	m.insert(make_pair(2, 20));

	//第三种
	m.insert(map<int, int>::value_type(3, 30));

	//第四种
	m[4] = 40;
	//【】不建议插入,用途:可以利用key访问到value
	printmap(m);
	//删除m
	m.erase(m.begin());
	printmap(m);
	m.erase(3);
	printmap(m);
	m.erase(m.begin(), m.end());
	m.clear();
}


int main()
{
	test01();
	
	return 0;
}

map查找和统计

对map容器进行查找数据以及统计数据

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

count(key);//统计key元素个数

#include<iostream>
using namespace std;
#include<map>
void printmap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << "value = " << it->second << endl;
	}
	cout << endl;
}
void test01()
{
	map<int, int>m;
	
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "查到了元素key = " << (*pos).first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	//m不允许key一样的值
	//multimap的统计可能大于1
	int num = m.count(3);
	cout << "num= " << num << endl;
}


int main()
{
	test01();
	
	return 0;
}

 map容器排序操作

#include<iostream>
using namespace std;
#include<map>
class MyCompare
{
public:
	bool operator()(int v1,int v2) const
	{
		//降序
		return v1 > v2;
	}
};
void test01()
{
	map<int, int,MyCompare>m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}

}
int main()
{
	test01();
	return 0;
}

#include<iostream>
using namespace std;
//1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
#include<string>
class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};
void test01()
{
	MyAdd myadd;
	cout << myadd(10, 10) << endl;
}
//2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
	MyPrint()
	{
		count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		count++;
	}

	int count;//内部自己的状态
};
void test02()
{
	MyPrint myprint;
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");

	cout << "myprint调用次数为:" << myprint.count << endl;
}
//函数对象可以作为参数传递
void doprint(MyPrint& mp, string test)
{
	mp(test);
}
void test03()
{
	MyPrint myprint;
	doprint(myprint, "hello c++");

}

int main()
{
	//test01();
	//test02();
	test03();
	return 0;
}

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值