STL容器运用练习

string

#include 
#include 
#include 
using namespace std;
//1 初始化
void test01()
{
	string s1 = "aaaaaa";
	string s2("bbbbbb");
	string s3 = s2;
	string s4(10, 'c');
	cout << s4 << endl;
}
//2 遍历
void test02()
{
	string s1 = "abcdefg";
	for (int i = 0; i < s1.length(); i++) {		//1 数组方式
		cout << s1[i];
		//cout << s1.at(i);	//越界时会抛出异常
	}
	cout << endl;
	
	for (string::iterator it = s1.begin(); it < s1.end(); it++) {	//2 迭代器方式 本质一个指针
		cout << *it;
	}
	cout << endl;
}
//3 string和char*的转换
void test03()
{
	string s1 = "abcdefg";
	cout << s1.c_str() << endl;//aaaaaa

	char buf1[128] = { 0 }; //剩余位置用空字符填充,C风格字符串
	s1.copy(buf1, 3, 1);	//可以不用其返回值
	cout << buf1 << endl;
}
//4 赋值和连接 比较
void test04()
{
	string s1 = "abcdefg";
	string s2;
	s2.assign(s1);
	s2.assign(s1, 1, 4);
	cout << s2 << endl;	//bcde

	//s1 += s2;
	//s1.append(s2);
	s1.append(s2,1,2);	//s1+cd
	cout << s1 << endl;

	cout << s1.compare(s2) << endl;	//-1
}
//5 查找和替换
void test05()
{
	string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
	//求某个字符串出现的次数
	int index = s1.find("wbm", 0);
	while (index != string::npos)	//-1
	{
		cout << "we find wbm in " << index << endl;
		index++;
		index = s1.find("wbm", index);
	}
	//大写替换小写
	index = s1.find("wbm", 0);
	while (index != string::npos)	//-1
	{
		s1.replace(index, 3, "WBM");
		index++;
		index = s1.find("wbm", index);
	}
	cout << s1 << endl;
}
//6 区间删除和插入
void test06()
{
	string s1 = "hello1 hello2 hello1";
	string::iterator it = find(s1.begin(), s1.end(), 'l');	//此处find为算法函数?赋给迭代器
	if (it != s1.end())
	{
		s1.erase(it);
	}
	cout << s1 << endl;
	s1.erase(s1.begin(), s1.end());	//全部删除
	cout << "s1: " << s1 << endl;

	string s2("BBB");
	s2.insert(0, "AAA");	//头插法
	s2.insert(s2.length(), "CCC");	//尾插法
	cout << s2 << endl;
}
//7 算法相关
void test07()
{
	string s1("AAAbbb");
	transform(s1.begin(), s1.end(), s1.begin(), toupper);	//全部大写,加算法头文件
	cout << s1 << endl;
	string s2 = s1;
	transform(s2.begin(), s2.end(), s2.begin(), tolower);
	cout << s2 << endl;
}

int main01()
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	//test06();
	test07();

	system("pause");
	return 0;
}

vector(动态数组)

#include 
#include 
#include 
using namespace std;
//1 元素添加和移除
void test11()
{
	vector v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	cout << "length: " << v1.size() << endl;
	v1.front() = 10;	//当左值
	v1.back() = 50;
	cout << "头部元素:" << v1.front() << endl;
	while (v1.size() > 0)
	{
		cout << "尾部元素:" << v1.back() << endl;
		v1.pop_back();
	}
	cout << "length: " << v1.size() << endl;
}
//2 初始化 遍历 数组方式for
void printV(vector &v1)
{
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;
}
void test12()
{
	vector v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	vector v2(v1.begin(), v1.begin() + 2);//注意该区间是左闭右开的区间
	//cout << v2 << endl;	//无法这样直接输出
	printV(v1);
}
//3 遍历 迭代器方式 分类
void test13()
{
	vector v1(10);	//提前把内存分配好
	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = i + 1;
	}
	//正向遍历
	for (vector::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//反向遍历
	for (vector::reverse_iterator rit = v1.rbegin(); rit != v1.rend(); rit++)
	{
		cout << *rit << " ";
	}
	cout << endl;
}
//4 重点:删除 1 根据元素位置 2 根据元素的值 ; 插入
void test14()
{
	vector v1(10);	//提前把内存分配好
	for (int i = 0; i < v1.size(); i++)
	{
		v1[i] = i + 1;
	}
	//1 根据元素位置
	v1.erase(v1.begin(), v1.begin() + 3);	//删除前三个数据
	printV(v1);
	v1.erase(v1.begin());
	printV(v1);
	//2 根据元素的值
	v1.front() = 2;
	v1.back() = 2;
	for (vector::iterator it = v1.begin(); it != v1.end();)
	{
		if (*it == 2)
		{
			it = v1.erase(it);//删除it位置的元素,并把数据删除后的下一个元素位置返回给迭代器
		}
		else
			it++;
	}
	printV(v1);
	//插入
	vector v2(5, 1);
	v1.insert(v1.begin() + 2, v2.begin(), v2.end());
	printV(v1);
	cout << endl;
}

int main()
{
	//test11();
	//test12();
	//test13();
	test14();
	
	system("pause");
	return 0;
}

deque(双端数组)

#include 
#include 	//双端数组

using namespace std;

void printD(deque &d)
{
	for (int i = 0; i < d.size(); i++)
	{
		cout << d.at(i) << " ";
	}
	cout << endl;
}

void test31()
{
	deque d1;
	d1.push_back(1);
	d1.push_back(3);
	d1.push_back(5);
	d1.push_front(-11);
	d1.push_front(-13);
	d1.push_front(-15);
	cout << "size: " << d1.size() << endl;
	printD(d1);
	//插入
	deque d2 = d1;
	d1.insert(d1.begin() + 1, d2.begin() + 3, d2.end());
	printD(d1);
	//删除3
	for (deque::iterator it = d1.begin(); it != d1.end();)
	{
		if (*it == 3)
			it = d1.erase(it);
		else
			it++;
	}
	printD(d1);

	//寻找值为-11的数组下标
	deque::iterator it = find(d1.begin(), d1.end(), -11);
	if (it != d1.end())
		cout << "-11的数组下标为:" << distance(d1.begin(), it) << endl;//算法的方式
	else
		cout << "无-11的值" << endl;
}

int main31()
{
	test31();
	system("pause");
	return 0;
}

stack(栈模型)

#include 
#include 	//栈模型	先进后出

using namespace std;

class Teacher
{
public:
	int age;
	char name[32];
public:
	void printT()
	{
		cout << "age: " << age << endl;
	}
};

//基本数据类型
void test35()
{
	stack s1;
	s1.push(1);
	s1.push(3);
	s1.push(5);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		cout << s1.top() << " ";//栈顶
		s1.pop();
	}
	cout << endl;
}
//元素
void test36()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	stack s1;
	s1.push(t1);
	s1.push(t2);
	s1.push(t3);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		Teacher tmp = s1.top();
		tmp.printT();	//调用Teacher本身的输出函数
		s1.pop();
	}
}
//元素指针
void test37()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	stack s1;
	s1.push(&t1);
	s1.push(&t2);
	s1.push(&t3);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		Teacher *p = s1.top();
		p->printT();	//调用Teacher本身的输出函数
		s1.pop();
	}
}


int main35()
{
	//test35();
	//test36();
	test37();
	system("pause");
	return 0;
}

queue(队列)(先进先出)

//和stack例子几乎一样,top()换成front()就ok
#include 
#include 	//队列	先进先出

using namespace std;

class Teacher
{
public:
	int age;
	char name[32];
public:
	void printT()
	{
		cout << "age: " << age << endl;
	}
};

//基本数据类型
void test40()
{
	queue s1;
	s1.push(1);
	s1.push(3);
	s1.push(5);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		cout << s1.front() << " ";//队头
		s1.pop();
	}
	cout << endl;
}
//元素
void test41()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	queue s1;
	s1.push(t1);
	s1.push(t2);
	s1.push(t3);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		Teacher tmp = s1.front();
		tmp.printT();	//调用Teacher本身的输出函数
		s1.pop();
	}
}
//元素指针
void test42()
{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	queue s1;
	s1.push(&t1);
	s1.push(&t2);
	s1.push(&t3);
	cout << "size: " << s1.size() << endl;
	while (!s1.empty())
	{
		Teacher *p = s1.front();
		p->printT();	//调用Teacher本身的输出函数
		s1.pop();
	}
}


int main40()
{
	//test40();
	//test41();
	test42();
	system("pause");
	return 0;
}

priority-queue(优先级队列)

//优先级队列
#include 
using namespace std;
#include "queue" 
//#include 
#include 	//定义greater的头文件
void main50()
{
	priority_queue p1; //默认是 最大值优先级队列 
							//priority_queue, less > p1; //相当于这样写
	priority_queue, greater> p2; //最小值优先级队列

	p1.push(33);
	p1.push(11);
	p1.push(55);
	p1.push(22);
	cout << "队列大小" << p1.size() << endl;
	cout << "队头" << p1.top() << endl;

	while (p1.size() > 0)
	{
		cout << p1.top() << " ";
		p1.pop();
	}
	cout << endl;

	cout << "测试 最小值优先级队列" << endl;
	p2.push(33);
	p2.push(11);
	p2.push(55);
	p2.push(22);
	while (p2.size() > 0)
	{
		cout << p2.top() << " ";
		p2.pop();
	}
}

list(双向链表)

//在deque例程上改改的 区别在于不能随机访问  it+i(err)
#include 
#include 	//双向列表

using namespace std;

void printD(list &d)
{
	for (list::iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test45()
{
	list d1;
	d1.push_back(1);
	d1.push_back(3);
	d1.push_back(5);
	d1.push_front(-11);
	d1.push_front(-13);
	d1.push_front(-15);
	cout << "size: " << d1.size() << endl;
	printD(d1);
	//插入
	list d2 = d1;
	list::iterator it1 = d1.begin();
	list::iterator it2 = d2.begin();
	it1++;
	it2++;
	it2++;
	it2++;

	d1.insert(it1, it2, d2.end());
	printD(d1);
	//删除3	方法一
	/*for (list::iterator it = d1.begin(); it != d1.end();)
	{
		if (*it == 3)
			it = d1.erase(it);
		else
			it++;
	}
	printD(d1);*/
	//删除3	方法一
	d1.remove(3);	//list提供的更简便的方法
	printD(d1);

	//反序排列
	d1.reverse();
	printD(d1);

	//寻找值为-11的数组下标
	list::iterator it = find(d1.begin(), d1.end(), -11);
	if (it != d1.end())
		cout << "-11的数组下标为:" << distance(d1.begin(), it) << endl;//算法的方式
	else
		cout << "无-11的值" << endl;
}

int main45()
{
	test45();
	system("pause");
	return 0;
}

set(集合)

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 	//集合	元素唯一 自动排序(默认从小到大)
#include 

using namespace std;

#define ture 1
#define false 0

class Teacher
{
public:
	int age;
	char name[32];
public:
	Teacher(char *p,int age)
	{
		strcpy(name, p);
		this->age = age;
	}
	void printT()
	{
		cout << "age: " << age << endl;
	}
};

void test55()
{
	set s1;
	//添加 插入
	for (int i = 0; i < 5; i++)
	{
		int tmp = rand();
		s1.insert(tmp);
	}
	s1.insert(100);
	s1.insert(100);
	s1.insert(100);
	for (set::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//删除
	while (!s1.empty())
	{
		set::iterator it = s1.begin();	//这里只能由迭代器操作
		cout << *it << " ";
		s1.erase(it);
	}
	cout << endl;
	//自动排序 从大到小
	set> s2;
	for (int i = 0; i < 5; i++)
	{
		int tmp = rand();
		s2.insert(tmp);
	}
	s2.insert(100);
	s2.insert(100);
	s2.insert(100);
	for (set::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//重点:如何自动排序用户自定义的类型?==>仿函数
//函数对象&仿函数
class TeaFunctor
{
public:
	bool operator()(const Teacher& left, const Teacher& right)
	{
		return (left.age < right.age);
	}
};

void test56()
{
	Teacher t1("t1", 33);
	Teacher t2("t2", 55);
	Teacher t3("t3", 11);
	Teacher t4("t4", 22);
	Teacher t5("t5", 55);	//测试插入同样年龄的对象==>会被覆盖掉

	//看 函数的返回值 确定t5有没有插入成功
	pair::iterator, bool> pair1;	//pair是一个对组,需要判断pair.second
	pair::iterator, bool> pair5;

	set s1;		//定义一个包含仿函数的容器对象
	pair1 = s1.insert(t1);
	if (pair1.second == ture)
	{
		cout << "t1 insert success!" << endl;
	}
	else
	{
		cout << "t1 insert fail!" << endl;
	}

	s1.insert(t2);
	s1.insert(t3);
	s1.insert(t4);

	pair5 = s1.insert(t5);
	if (pair5.second == ture)
	{
		cout << "t5 insert success!" << endl;
	}
	else
	{
		cout << "t5 insert fail!" << endl;
	}
	
	for (set::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << it->age << " " << it->name << endl;	//因为没有重载Teacher的<<
	}
}
//
void test57()
{
	set s1;
	for (int i = 0; i < 10; i++)
	{
		s1.insert(i+1);
	}
	for (set::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	set::iterator it1 = s1.find(5);
	cout << "it1: " << *it1 << endl;
	int num = s1.count(5);
	cout << num << endl;
	set::iterator it2 = s1.lower_bound(5);	//大于等于5
	cout << "it2: " << *it2 << endl;
	set::iterator it3 = s1.upper_bound(5);	//大于5
	cout << "it3: " << *it3 << endl;
	pair::iterator, set::iterator> pair1 = s1.equal_range(5);	//对组,两个迭代器元素,左闭右开
	//cout << "pair1: " << pair1.first << " " << pair1.second << endl;
	//不能直接输出,要用迭代器转换
	set::iterator it4 = pair1.first;
	cout << "it4: " << *it4 << endl;
	set::iterator it5 = pair1.second;
	cout << "it5: " << *it5 << endl;
}

int main55()
{
	//test55();
	//test56();
	test57();
	system("pause");
	return 0;
}

multiset(多重集合)

#include 
#include 	//集合 自动排序(默认从小到大)
//#include 

using namespace std;

void test60()
{
	multiset m1;	//多重集合 同一个值可以出现多次 从小到大
	int tmp;
	cout << "请输入一系列集合值:" << endl;
	cin >> tmp;
	while (tmp != 0)	//以0结束输入
	{
		m1.insert(tmp);
		cin >> tmp;
	}
	for (multiset::iterator it = m1.begin(); it != m1.end(); it++)	//遍历
	{
		cout << *it << " ";
	}
	cout << endl;

	while (!m1.empty())	//删除
	{
		multiset::iterator it = m1.begin();	//这里只能由迭代器操作
		cout << *it << " ";
		m1.erase(it);
	}
	cout << endl;

}

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

map(映射)

#include 
#include 	//关联式容器  键值对序列,即(key,value)对
#include 

using namespace std;

//添加 遍历 删除
void test70()
{
	map map1;
	//4种常用的添加元素的方法
	map1.insert(pair(1, "Teacher01"));	//方法1
	map1.insert(pair(2, "Teacher02"));

	map1.insert(make_pair(3, "Teacher03"));	//方法2
	map1.insert(make_pair(4, "Teacher04"));

	map1.insert(map::value_type(5, "Teacher05"));	//方法3
	map1.insert(map::value_type(6, "Teacher06"));

	map1[7] = "Teacher07";	//方法4
	map1[8] = "Teacher08";

	//遍历
	for (map::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍历结束!" << endl;

	//删除
	while (!map1.empty())
	{
		map::iterator it1 = map1.begin();
		cout << it1->first << "\t" << it1->second << endl;
		map1.erase(it1);
	}
}

//4种常用的添加元素的方法的异同
//重点:
//前3种方法函数返回值 pair ,若key存在则报错
//第4种方法若key存在则修改
void test71()
{
	//使用以上例程
	map map1;
	//4种常用的添加元素的方法	
	pair::iterator, bool> pair1 = map1.insert(pair(1, "Teacher01"));	//方法1

	pair::iterator, bool> pair3 = map1.insert(make_pair(3, "Teacher03"));	//方法2

	pair::iterator, bool> pair5 = map1.insert(map::value_type(5, "Teacher05"));
	if (pair5.second != true)
	{
		cout << "key5 insert fail." << endl;
	}
	else
	{
		cout << pair5.first->first << "\t" << pair5.first->second << endl;//插入成功则输出
	}
	//此时插入相同的key值==>插入失败
	pair::iterator, bool> pair6 = map1.insert(map::value_type(5, "Teacher06"));
	if (pair6.second != true)
	{
		cout << "key6 insert fail." << endl;
	}
	else
	{
		cout << pair6.first->first << "\t" << pair6.first->second << endl;//插入成功则输出
	}

	map1[7] = "Teacher07";
	//此时插入相同的key值==>value被覆盖
	map1[7] = "Teacher08";

	//遍历
	for (map::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍历结束!" << endl;
}

//find	equal_range	异常处理
void test72()
{
	//使用test70例程
	map map1;
	map1.insert(pair(1, "Teacher01"));	//方法1
	map1.insert(pair(2, "Teacher02"));

	map1.insert(make_pair(3, "Teacher03"));	//方法2
	map1.insert(make_pair(4, "Teacher04"));

	map1.insert(map::value_type(5, "Teacher05"));
	map1.insert(map::value_type(6, "Teacher06"));

	map1[7] = "Teacher07";
	map1[8] = "Teacher08";

	for (map::iterator it = map1.begin(); it != map1.end(); it++)
	{
		cout << it->first << "\t" << it->second << endl;
	}
	cout << "遍历结束!" << endl;

	//find
	map::iterator it2 = map1.find(100);
	if (it2 == map1.end())
	{
		cout << "key100 不存在!" << endl;
	}
	else
	{
		cout << it2->first << "\t" << it2->second << endl;
	}
	//equal_range
	pair::iterator, map::iterator> pair0 = map1.equal_range(5);
	if (pair0.first == map1.end())
	{
		cout << "key>=5 不存在!" << endl;
	}
	else
	{
		cout << pair0.first->first << "\t" << pair0.first->second << endl;
		cout << pair0.second->first << "\t" << pair0.second->second << endl;//这个也需要另外判断是否到end?
	}
}

int main70()
{
	//test70();
	//test71();
	test72();
	system("pause");
	return 0;
}

multimap(多重映射)

//1个key值可以对应多个valude  =分组 
//公司有销售部 sale (员工2名)、技术研发部 development (2人)、财务部 financial (1人) 
//人员信息有:姓名,年龄,电话、工资等组成
//通过 multimap进行 信息的插入、保存、显示
//分部门显示员工信息 

#include 
#include 	//关联式容器  键值对序列,即(key,value)对
#include 
#include 

using namespace std;

class Person
{
public:
	Person(string name,int age)	//构造函数初始化
	{
		this->name = name;
		this->age = age;
	}
public:
	string name;
	int	age;
	string tel;
	double salay;
};

void test75()
{
	Person p1("小王1", 31);
	Person p2("小王2", 32);
	Person p3("小张1", 33);
	Person p4("小张2", 34);
	Person p5("小李1", 35);
	multimap map2;	//类作为参数
	map2.insert(make_pair("sale", p1));	//map添加元素的4种方法
	map2.insert(make_pair("sale", p2));
	map2.insert(make_pair("development", p3));
	map2.insert(make_pair("development", p4));
	map2.insert(make_pair("financial", p5));
	for (multimap::iterator it = map2.begin(); it != map2.end(); it++)
	{
		//格式化输出	美观
		cout << setw(12) << setiosflags(ios::left) << it->first << "\t" << it->second.name << endl;
	}
	//分部门显示员工信息 
	int num2 = map2.count("development");
	cout << "\ndevelopment 部门员工人数为:" << num2 << endl;
	cout << "development 部门员工详细信息:" << endl;
	multimap::iterator it2 = map2.find("development");
	while (it2 != map2.end() && num2 != 0)
	{
		cout << it2->first << "\t" << it2->second.name << endl;
		it2++;
		num2--;
	}
	//修改员工信息
	cout << endl;
	for (multimap::iterator it = map2.begin(); it != map2.end(); it++)
	{
		if (it->second.age == 34)
		{
			it->second.name = "阿离";
		}
	}
	for (multimap::iterator it = map2.begin(); it != map2.end(); it++)
	{
		cout << setw(12) << setiosflags(ios::left) << it->first << "\t" << it->second.name << endl;
	}
}

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

 

你可能感兴趣的:(C++编程练习)