初见——STL(上)

初识STL

一、vector存放内置数据类型

#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(); 
}

二、vector容器中存放自定义数据类型

#include <iostream>
#include <vector>
#include <string>
using namespace std;
//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("bbb", 30);
	Person p4("bbb", 40);
	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//遍历容器中的数据
	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("bbb", 30);
	Person p4("bbb", 40);
	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	//遍历容器
	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it) -> m_Age << endl;
	}
}
int main()
{
	test01();
	test02();
}

三、vextor容器嵌套容器

/*vextor容器嵌套容器*/ 
#include <iostream>
using namespace std;
#include <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;
} 

四、string容器构造函数

/*string容器构造函数*/
#include <iostream>
#include <string>
using  namespace std;
//-string();  创建一个新的字符串 例如string str;
//-string(const char* s); //使用字符串s初始化
//-string (const string& str);//使用一个string对象初始化另一个string对象
//-string (int n,char c);  使用n个字符c初始化
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();
}

五、string 的赋值操作

/*string 的赋值操作*/
#include <iostream>
#include <string>
using namespace std;
//-string& operator=(const char* s); char*类型字符串 赋值给当前的字符串
//-string& operator=(const string &s); 把字符串s赋给当前的字符串
//-sting& operator=(char c); 字符赋值给当前的字符串
//-string& assign(const char *s) 把字符串s赋给当前的字符串
//-string& assign(const char *s,int n); 把字符串s前n个字符赋给当前的字符串
//-string& assign(const string &s);  把字符串s赋给当前的字符串
//string& assign(int n, char c); //用n个字符c赋给当前字符串
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();
}

六、字符串拼接

/*字符串拼接*/
#include <iostream>
#include <string>
using namespace  std;
void test01()
{
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str=" << 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,4,3); //参数2从哪个位置开始截取,参数3截取的字符个数
	cout << "str3=" << str3 << endl;
}
int main()
{
	test01();
}

七、字符串查找和替换

/*字符串查找和替换*/
#include <iostream>
#include <string>
using namespace std;
void test01()
{
	string str1 = "abcdefgde";
	int pos = str1.find("de");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "pos=" << pos << endl;
	}
	//rfind和find的区别,find从左往右找
	pos = str1.rfind("de");
	cout << "pos=" << pos << endl;
}
void test02()
{
	string str1 = "abcdefg";
	//从1号起三个字符替换为1111
	str1.replace(1, 3, "1111");
	cout << "str1=" <<str1 << endl;
}
int main()
{
	test01();
	test02();
}

八、 string字符串比较

/*string字符串比较*/
#include <iostream>
using namespace std;
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>
#include <string>
using namespace std;
void test01()
{
	string str = "Hello";
	//1.通过[]访问单个字符
	for (int i = 0; i < str.size();i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;
	//2.通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	//修改单个字符
	str[0] = 'x';
	cout << "str=" << str << endl;
	str.at(1) = 'x';
	cout << "str=" << str << endl;
}
int main()
{
	test01();
}

十、sting插入和删除

/*sting插入和删除*/
#include <iostream>
#include <string>
using namespace std;
void test01()
{ 
	string str = "hello";
	//插入
	str.insert(1, "111");
	//h111ello
	cout << "str=" << str << endl;
	//删除
	str.erase(1, 3);//从第一个位置起三个
	cout << "str=" << str << endl;
}
int main()
{
	test01();
	return 0;
} 

十一、 string子串

/*string子串*/
#include <iostream>
using namespace std;
#include <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("@");
	string usrName = email.substr(0, pos);
	cout << usrName << endl;
}
int main()
{
	test01();
	test02();
	return 0;
}

十二、 vector容器

 /*vector容器*/
//与数组相似,但可以动态扩展
//动态扩展并不是续接新空间,而是找一块新的更大的空间,把原有数据拷贝过来,释放原空间
#include<iostream>
#include <vector>
using namespace std;
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);
	//通过区间方式进行构造
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);
	//n个elem方式构造
	vector<int>v3(10, 100);
	printVector(v3);
	//拷贝构造
	vector<int>v4(v3);
	printVector(v4);
}

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

十三、vector容器赋值操作

/*vector容器赋值操作*/
#include <iostream>
using namespace std;
#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();
}

十四、vector容量和大小

/*vector容量和大小*/
#include <iostream>
#include <vector>
using namespace std;
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);
	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
	printVector(v1);//如果重新指定的比原来长了,利用默认值0填充新的位置
	v1.resize(5);
	printVector(v1);//超出的部分删除掉了
}
int main()
{
	test01();
	system("pause");
	return 0;
}

十五、 vector插入和删除

/*vector插入和删除*/
#include <iostream>
#include <vector>
using namespace std;
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;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	printVector(v1);
	//尾删
	v1.pop_back();
	printVector(v1);
	//插入
	v1.insert(v1.begin(), 1000);
	printVector(v1);
	v1.insert(v1.begin(), 2, 1000);//插入2个1000
	printVector(v1);
	//删除 参数也是迭代器
	v1.erase(v1.begin());
	printVector(v1);
	//清空
	v1.clear();
	printVector(v1);
}
int main()
{
	test01();
}

十六、vector数据存取

/*vector数据存取*/
#include<iostream>
#include<vector>
using namespace std;
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;
}

十七、 vector互换容器

/*vector互换容器*/
#include<iostream>
#include<vector>
using namespace std;
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);
	}
	cout << "交换前:" << endl;
	printVector(v1);
	vector<int>v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	printVector(v2);
	cout << "交换后" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(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;
	//用swap收缩
	vector<int>(v).swap(v);//相当于创建了一个匿名对象x,再交换v
	//匿名对象特点,使用之后系统会自行释放
	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl; 
}

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

十八、vector容器reserve 预留空间

#include <iostream>
using namespace std;
#include <vector>
//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;
}

十九、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);
}
int main()
{
	test01();
	return 0;
}

二十、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++)
	{
		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;
	printDeque(d2);
	//assign赋值
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	printDeque(d3);
}
int main()
{
	test01();
	return 0;
}

二十一、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++)
	{
		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 << "为空" << endl;
	}
	else
	{
		cout << "d1不为空" << endl;
		cout << "d1的大小为" << d1.size() << endl;
		//deque容器没有容量的概念
	}
	//重新指定大小
	//d1.resize(15);
	d1.resize(15, 1);
	printDeque(d1);
	d1.resize(5);
	printDeque(d1);
}
int main()
{
	test01();
	return 0;
}

二十二·、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++)
	{
		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();
	//200 100 10
	printDeque(d1);
	//头删
	d1.pop_front();
	//100 10
	printDeque(d1);
}
void test02()
{
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_back(100);
	d1.push_back(200);
	printDeque(d1);
	//insert插入
	d1.insert(d1.begin(), 1000);
	printDeque(d1);
	d1.insert(d1.begin(), 2, 10000);
	printDeque(d1);
	//按照区间进行插入
	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);

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

二十三、deque容器数据存取

/*deque容器数据存取*/
#include <iostream>
using namespace std;
#include <deque>
void test01()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_front(200);
	d.push_front(300);
	//通过[]方式访问元素
	//300 200 100 10 20 30
	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;
	for(int i=0;i<d.size();i++)
	cout << endl;
	cout << "第一个元素为:" << d.front() << endl;
	cout << "最后一个元素为" << d.back() << endl;
}
int main()
{
	test01();
	return 0;
}

二十四、deque排序

/*deque排序*/
#include <iostream>
using namespace std;
#include <deque>
#include <algorithm>
void printDeque(const deque<int>&d)
{
	for (deque<int>::const_iterator it = d.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
}
void test01()
{
	deque<int>d;
	printDeque(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算法对其进行排序
	//vector容器也可以
	sort(d.begin(), d.end());
	cout << "排序后;" << endl;
	printDeque(d);

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

二十五、案例

/*有5名选手:ABCDE,10个评委分别对每一名选手打分,去除最高分,最低分
取平均分*/
#include <iostream>
#include <string>
using namespace std;
#include <vector>
#include <deque>
#include <algorithm>
#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<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);
	system("pause");
	return 0;
} 

二十六、栈stack容器

//栈不允许遍历,可以判断是否为空,可以返回size 先进后出
#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容器常用接口

/*queue容器常用接口*/
//先进先出 只有对头,队尾能被外界访问,所以也不能遍历
#include <iostream>
#include <queue>
#include <string>
using namespace std;
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("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);
	//入列
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);
	//判断只要队列不为空,查看队尾队头,出队
	while (!q.empty())
	{
		//查看队头
		cout << "队头元素—姓名:" << q.front().m_Name << "nianling" << 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容器赋值和交换

/*list容器赋值和交换*/
#include <iostream>
using namespace std;
#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>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(L3);
	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;
}

二十九、list容器的大小操作

#include <iostream>
using namespace std;
#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>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,520);
	printList(L1);
}
int main()
{
	test01();
	system("pause");
	return 0;

三十、list容器的插入和删除

/*list容器的插入和删除*/
#include <iostream>
using namespace std;
#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);
	printList(L);
	//尾删
	L.pop_back();
	printList(L);
	//头删
	L.pop_front();
	printList(L);
	//insert插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	printList(L);
	//200 1000 100 10 20
	//删除
	it = L.begin();
	L.erase(++it);
	//200 100 10 20
	printList(L);
	//移除
	L.push_back(10000);
	L.push_back(10000);
	printList(L);
	L.remove(10000);
	printList(L);
	L.clear();
	printList(L);
}
int main()
{
	test01();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值