栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习(STL初识容器2)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习(STL初识容器2)

#include
using namespace std;
#include
#include
#include
#include
#include //含有set和multiset
#include







void test_stack_01()
{
	stacks;

	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	//只要栈不为空,查看栈顶,并且执行出栈操作
	while (!s.empty())
	{
		//查看栈顶元素
		cout<< "栈顶元素为:" << s.top() <m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};
void test_queue_01()
{
	queueq;

	Man m1("唐僧", 30);
	Man m2("孙悟空",1000);
	Man m3("猪八戒",900);
	Man m4("沙僧",800);

	//入队
	q.push(m1);
	q.push(m2);
	q.push(m3);
	q.push(m4);

	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 << "队列大小为:" < &L) //为了在打印时不修改它const
{
	for (list::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout<< *it <<" ";
	}
	cout<L1;

	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//遍历容器
	printList(L1);

	//区间方式构造
	listL2(L1.begin(),L1.end());
	printList(L2);

	//拷贝构造
	listL3(L1);
	printList(L3);

	//n个elem
	listL4(10, 100);
	printList(L4);

}



void test_list_value_01()
{
	listL1;

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

	printList(L1);

	//等号赋值
	listL2 = L1;
	printList(L2);

	//assign赋值
	listL3;
	L3.assign(L1.begin(),L1.end()); 
	printList(L3);

	//assign赋值
	listL4;
	L4.assign(10,100);
	printList(L4);

	cout << "交换前:"<< endl;
	printList(L1);
	printList(L4);
	L1.swap(L4);

	cout<< "交换后:"<< endl;
	printList(L1);
	printList(L4);


}



void test_list_capacity_01()
{
	listL1;

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

	printList(L1);

	if (L1.empty())
	{
		cout<< "L1为空"<L1;

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

	L1.push_front(100);
	L1.push_front(200);
	L1.push_front(300);

	printList(L1);//300 200 100 10 20 30

	//尾部删除
	L1.pop_back();
	L1.pop_front();
	printList(L1);//200 100 10 20

	//头部插入
	list::iterator it = L1.begin();
	L1.insert(++it,1000);//偏移后插入
	printList(L1);//200 1000 100 10 20

	//删除
	it = L1.begin();
	L1.erase(++it);//偏移后删除
	printList(L1);//200 100 10 20

	//移除
	L1.push_back(10000);
	L1.push_back(10000);
	L1.push_back(10000);
	L1.push_back(10000);
	printList(L1); //200 100 10 20 10000 10000 10000 10000
	L1.remove(10000);
	printList(L1); //200 100 10 20

	//清空
	L1.clear();
	printList(L1); 

	
}



void test_list__find()
{
	listL1;

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

	//L1[0]  不可以用[] 访问list容器中的元素

	//L1.at(0) 不可以用at方式访问list容器中的元素

	//原因是list本质是链表,不是连续线性空间存储数据,迭代器也是不支持随机访问的

	cout << "第一个元素:" << L1.front()<::iterator it = L1.begin();

	//it = it + 1; //错误,没有与这些操作数匹配的“+”运算符  ,验证不支持随机访问
	it++;//可以往前或往后走  ,验证支持双向   it++,it--   ,(同理可验证其他容器)
}



bool myCompare(int value1,int value2)//回调函数
{
	//降序 就让第一个数 > 第二个数
	return value1 > value2;
}

void test_list_reverse_01()
{
	listL1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(50);
	L1.push_back(40);
	L1.push_back(30);

	cout<< "反转前:"<m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	int m_Age;
	string m_Name;
	int m_Height;
};

//指定排序规则,自己写回调函数
bool comparePerson(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 test_list_example()
{
	listL;

	Person p1("刘备",35 ,175 );
	Person p2("曹操",45 ,180 );
	Person p3("孙权",40 ,170 );
	Person p4("赵云",25 ,190 );
	Person p5("张飞",35 ,160 );
	Person p6("关羽",35 ,200 );

	//插入数据
	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);

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





void printSet(set &s)
{
	for (set::iterator it = s.begin(); it != s.end(); it++)  //用迭代器
	{
		cout<< *it <<" ";
	}
	cout << endl;
}

void test_set_01()
{
	sets1;
	//插入数据只有insert方式
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(30);

	//遍历容器
	//set容器特点:所有元素插入时候被自动排序
	//set容器不允许插入重复值,不会报错,但无效插入
	printSet(s1);

	//拷贝构造
	sets2(s1);
	printSet(s2);

	//赋值
	sets3(s2);
	printSet(s3);

}




void test_set_capacity_01() 
{
	sets1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	printSet(s1);

	//判断是否为空
	if (s1.empty())
	{
		cout<< "s1为空" <s2;
	s2.insert(40);//set自动排序
	s2.insert(30);
	s2.insert(20);
	s2.insert(10);
	s2.insert(50);


	cout << "交换前" << endl;
	printSet(s1);
	printSet(s2);

	s2.swap(s1);
	cout << "交换后" << endl;
	printSet(s1);
	printSet(s2);


}




void test_set_insert_delete_01()
{
	sets1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

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

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

}



void test_find_01()
{
	sets1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set::iterator pos = s1.find(30);

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

	int num = s1.count(30);
	//对于set而言,结果要么0,要么1
	cout << "num =  " << num <s1;
	pair::iterator ,bool> ret = s1.insert(10);   //返回值bool型     **等式赋值过程中执行了s1的插入
	
	if (ret.second)
	{
		cout<<"第一次插入成功" <ms;
	ms.insert(10);
	ms.insert(10);
	ms.insert(10);
	ms.insert(10);

	for (multiset::iterator it = ms.begin(); it != ms.end(); it++)
	{
		cout<< *it << " " ;
	}
	cout<p("Tom",20);

	cout<< "姓名:" << p.first << "年龄:"<< p.second<p2 = make_pair("Jerry", 10);

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

}


class Mycompare {
public:
	bool operator()(int v1 ,int v2) const  //operator()重载的符合 ,第二个(前一个,后一个)放参数比较(看比较数据的类型) 
	{
		return v1 > v2;//前面的数大于后面的数
	}


};
void test_change_sort_01()
{
	sets1;
	s1.insert(50);
	s1.insert(40);
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);

	for (set::iterator it = s1.begin(); it != s1.end(); it++)  // 
	{
		cout << *it << " ";
	}
	cout << endl;
	
}
//set容器排序,存放自定义数据类型
class Person_1 {
public:
	Person_1(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	int m_Age;
	string m_Name;

};

class comparePerson_1 {

public:
	bool operator()(const Person_1& p1, const Person_1& p2)const
	{
		//按照年龄 降序
		return p1.m_Age > p2.m_Age;
	}


};

void test_sort_02()
{
	//自定义的数据类型 都要指定排序规则  写仿函数
	sets;

	Person_1 p1("刘备", 24);
	Person_1 p2("关羽", 28);
	Person_1 p3("张飞", 25);
	Person_1 p4("赵云",21);

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

	for (set::iterator it = s.begin();it != s.end();it++)  //类自创类型
	{
		cout<< "姓名:" << it->m_Name << "  年龄:" << it ->m_Age < &m)
{
	for (map::iterator it = m.begin();it != m.end();it++)
	{
		cout<<  "key = " << (*it).first <<  " value = " << it->second << endl;   //用 (*it).  或 it->
	}
	cout<m;

	m.insert(pair(1, 10));
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	m.insert(pair(4, 40));

	printMap(m);   // 会按照key做排序        key 1 , 2 , 3 ,4

	//拷贝构造
	mapm2(m);
	printMap(m2);

	//赋值
	mapm3;
	m3 = m2;
	printMap(m3);


}


void test_map_size_01()
{
	mapm;

	m.insert(pair(1, 10));
	m.insert(pair(2, 20));
	m.insert(pair(3, 30));

	if (m.empty())
	{
		cout<< "m为空 "<m2;
	m2.insert(pair(4, 100));
	m2.insert(pair(5, 200));
	m2.insert(pair(6, 300));

	cout << "交换前 " << endl;
	printMap(m);
	printMap(m2);

	m.swap(m2);
	cout << "交换后 " << endl;
	printMap(m);
	printMap(m2);


}



void test_insert_delete_01()
{
	mapm;

	//插入
	//第一种
	m.insert(pair(1, 10));

	//第二种
	m.insert(make_pair(2, 20));//记第一、二种好记  

	//第三种
	m.insert(map::value_type(3, 30));//map作用域下的值类型

	//第四种   []用于key找到value 
	m[4] = 40;  //最简单,但不建议[]   

	printMap(m);

	/

void test_map_count_01()
{
	//查找
	mapm;
	m.insert(pair(1, 10));
	m.insert(pair(2, 20));
	m.insert(pair(3, 30));
	m.insert(pair(3, 40));

	map::iterator pos = m.find(3);

	if (pos != m.end())
	{
		cout<<"查找到了元素 key = " << (*pos).first<< " value = "<< pos->second <
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号