STLDay2

STLDay2

栈容器stack容器

  • stack所有元素的进出都必须符合先进后出的条件,只有stack顶端的元素,才有机会被外界取用,stack不提供遍历功能,也不提高迭代器
#include <iostream>
#include <stack>
using namespace std;

//stack构造函数
//stack<T>stkT;//stack采用模板类实现,stack对象的默认构造形式
//stackC(const stack &stk);//拷贝构造函数

//stack赋值操作
//stack& operator=(const stack &stk);//重载等号操作符

//stack数据存取操作
//push(elem);//向栈顶添加元素
//pop();//从栈顶移除第一个元素
//top();//返回栈顶元素

//stack大小操作
//empty();//判断堆栈是否为空
//size();//返回堆栈的大小

void test01()
{
	stack<int>S;
	//入栈
	S.push(10);
	S.push(20);
	S.push(30);
	S.push(40);

	cout << "size = " << S.size() << endl;

	while (!S.empty())
	{
		//访问栈顶元素
		cout << S.top() << endl;
		//出栈
		S.pop();
	}
	cout << "size = " << S.size() << endl;
}

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

队列容器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("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	cout << "size = " << Q.size() << endl;

	//入队
	Q.push(p1);
	Q.push(p2);
	Q.push(p3);
	Q.push(p4);

	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 << "size = " << Q.size() << endl;
}

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

list容器

  • list容器是一个双向循环链表
#include<iostream>
using namespace std;
#include <list>
#include <algorithm>
#include <string>

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

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

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

	//正序遍历
	for (list<int>::iterator it = L1.begin(); it != L1.end(); it++)
	{
		cout << *it << endl;
	}
	//逆序遍历
	for (list<int>::reverse_iterator it = L1.rbegin(); it != L1.rend(); it++)
	{
		cout << *it << endl;
	}

	//list迭代器不支持随机访问,是一个双向迭代器
	list<int>::iterator itBegin = L1.begin();
	//itBegin=itBegin+1;
}

void test02()
{
	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();//尾删
	L.pop_front();//头删

	//插入
	L.insert(L.begin(), 10000);
	printList(L);

	L.erase(L.begin());
	printList(L);

	L.remove(100);
	printList(L);
}

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

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

void test03()
{
	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);

	list<int>L2;
	L2.assign(10, 100);

	L.swap(L2);

	printList(L);
}
//list反转排序
//reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
//sort(); //list排序

bool myCompare(int v1, int v2)
{
	return v1 > v2;
}

void test04()
{
	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);

	L.reverse();
	printList(L);

	//排序 如果容器的迭代器支持随机访问,可以使用系统提共的标志算法
	//不支持随机访问的迭代器的容器,内部会提高对应的算法接口
	//sort(L.begin(), L.end());
	L.sort(myCompare);
	printList(L);
}

class Person
{
public:
	Person(string name, int age, int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age && this->m_Height == p.m_Height)
		{
			return true;
		}
		return false;
	}

	string m_Name;
	int m_Age;
	int m_Height;
};

bool myComparePerson(Person& p1, Person& p2)
{
	if (p1.m_Age == p2.m_Age)
	{
		return p1.m_Height < p2.m_Height;
	}

	return  p1.m_Age > p2.m_Age;
}

void test05()
{
	list<Person> L;

	Person p1("亚瑟", 40, 180);
	Person p2("赵云", 20, 160);
	Person p3("妲己", 30, 120);
	Person p4("孙悟空", 50, 200);
	Person p5("关羽", 10, 170);
	Person p6("刘备", 10, 165);
	Person p7("张飞", 10, 185);

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

	//按照年龄进行降序   从大到下 , 如果年龄相等,按照身高进行升序 
	//对于自定义数据类型,必须要指定排序规则
	L.sort(myComparePerson);

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

	//删除孙悟空
	L.remove(p4);

	cout << "删除孙悟空后:" << endl;
	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();
	test02();
	test03();
	test04();
	test05();
	return EXIT_SUCCESS;
}

set容器

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

//set构造函数
//set<T> st;//set默认构造函数:
//mulitset<T> mst; //multiset默认构造函数:
//set(const set &st);//拷贝构造函数

//set赋值操作
//set& operator=(const set &st);//重载等号操作符
//swap(st);//交换两个集合容器

//set大小操作
//size();//返回容器中元素的数目
//empty();//判断容器是否为空

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

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

void test01()
{
	set<int>s;

	s.insert(10);
	s.insert(50);
	s.insert(20);
	s.insert(40);

	printSet(s);

	if (s.empty())
	{
		cout << "set容器为空" << endl;
	}
	else
	{
		cout << "set容器为空" << endl;
	}

	s.erase(30);
	printSet(s);
}

//set查找操作
//find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
//count(key);//查找键key的元素个数
//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

void test02()
{
	set<int>s;

	s.insert(10);
	s.insert(50);
	s.insert(20);
	s.insert(40);

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

	//对于set,count结果不是0就是1
	int num = s.count(40);

	cout << "key为40的个数" << num << endl;

	//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
	set<int>::iterator pos2 = s.lower_bound(30);

	if (pos2 != s.end())
	{
		cout << "lower_bound的值为:" << *pos2 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
	pos2 = s.upper_bound(30);
	if (pos2 != s.end())
	{
		cout << "upper_bound的值为:" << *pos2 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

	pair< set<int>::iterator, set<int>::iterator>  ret = s.equal_range(30);

	if (ret.first != s.end())
	{
		cout << "equal_range中的lower_bound的值为:" << *ret.first << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	if (ret.second != s.end())
	{
		cout << "equal_range中的upper_bound的值为:" << *ret.second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

}

//pair创建
void test03()
{
	pair<string, int> p("Tom", 10);

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


	pair<string, int> p2 = make_pair("Jerry", 18);
	cout << "姓名: " << p2.first << " 年龄: " << p.second << endl;
}


void test04()
{
	set<int>s;
	pair<set<int>::iterator, bool> ret = s.insert(10);

	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}


	ret = s.insert(10);
	if (ret.second)
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}


	printSet(s);

	//允许插入重复的key值
	multiset<int> ms;
	ms.insert(10);
	ms.insert(10);
	cout << "---------" << endl;
	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
	{
		cout << (*it) << endl;
	}

}


class myCompareInt
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};

void test05()
{
	//set容器的排序规则要在插入之前指定
	set<int, myCompareInt >s;

	s.insert(10);
	s.insert(50);
	s.insert(30);
	s.insert(20);
	s.insert(40);

	//printSet(s);

	for (set<int, myCompareInt>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << endl;
	}
}

//对于自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

class myComparePerson
{
public:
	bool operator()(const Person& p1, const Person& p2)
	{
		//按照年龄 升序  从小到大
		return p1.m_Age < p2.m_Age;
	}
};

void test06()
{
	//利用仿函数  指定出自定义数据类型的排序规则
	set<Person, myComparePerson> s;

	Person p1("aaa", 10);
	Person p2("bbb", 40);
	Person p3("ccc", 20);
	Person p4("ddd", 30);
	Person p5("eee", 50);

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

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

}

map容器

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

//map构造函数
//map<T1, T2> mapTT;//map默认构造函数:
//map(const map &mp);//拷贝构造函数

//map赋值操作
//map& operator=(const map &mp);//重载等号操作符
//swap(mp);//交换两个集合容器

//map大小操作
//size();//返回容器中元素的数目
//empty();//判断容器是否为空

//map插入数据元素操作
//map.insert(...); //往容器插入元素,返回pair<iterator,bool>
//map<int, string> mapStu;

// 第一种 通过pair的方式插入对象
//mapStu.insert(pair<int, string>(3, "小张"));

// 第二种 通过pair的方式插入对象
//mapStu.inset(make_pair(-1, "校长"));

// 第三种 通过value_type的方式插入对象
//mapStu.insert(map<int, string>::value_type(1, "小李"));

// 第四种 通过数组的方式插入值
//mapStu[3] = "小刘";
//mapStu[5] = "小王";


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;

	//cout << m[10] << endl; //如果利用第四种 使用未指定的key,生成一个key为为指定的值,value为0的数据插入到容器中

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

}

//map删除操作
//clear();//删除所有元素
//erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
//erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
//erase(keyElem);//删除容器中key为keyElem的对组。

void test02()
{
	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;

	m.erase(3); //删除传入key值

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

}

 //map查找操作
//find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
//count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。


void test03()
{
	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;

	map<int, int>::iterator ret = m.find(3);
	if (ret != m.end())
	{
		cout << "找到了 key为 " << ret->first << " value = " << ret->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//统计  count 
	int num = m.count(4);
	cout << "key为4的对组个数为: " << num << endl;

	//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
	map<int, int>::iterator pos = m.lower_bound(3);

	if (pos != m.end())
	{
		cout << "找到了lower_bound key: " << pos->first << " value: " << pos->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
	pos = m.upper_bound(3);
	if (pos != m.end())
	{
		cout << "找到了upper_bound key: " << pos->first << " value: " << pos->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
	pair<map<int, int>::iterator, map<int, int>::iterator>  ret2 = m.equal_range(3);

	if (ret2.first != m.end())
	{
		cout << "找到了equal_range中的 lower_bound的 key =  " << ret2.first->first << " value = " << ret2.first->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	if (ret2.second != m.end())
	{
		cout << "找到了equal_range中的 upper_bound的 key =  " << ret2.second->first << " value = " << ret2.second->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

}

class myCompareInt
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test04()
{
	map<int, int, myCompareInt> 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;

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

}


int main() {
	test01();
	test02();
	test03();
	test04();
	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值