C/C++教程

C++容器篇(二)

本文主要是介绍C++容器篇(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

list容器

#include<iostream>
#include<list>
#include<string>
#include<functional>//less和greater的头文件
using namespace std;
//操作基本数据类型
void testList() {
	list<int> iNum;
	list<string> strNum;
	//插入
	strNum.push_back("string1");
	strNum.push_back("string2");
	strNum.push_front("string3");
	//遍历
	// 不删除的方式,用迭代器遍历
	list<string>::iterator iter;
	for (iter = strNum.begin(); iter != strNum.end(); iter++) {
		cout << *iter << "\t";
	}
	cout << endl;
	cout << "是否为空:" << boolalpha << !strNum.empty() << endl;
	cout << "当前节点个数:" << strNum.size() << endl;
	// 删除的方式,打印头删头
	while (!strNum.empty()) {
		cout << strNum.front() << " ";//back()
		strNum.pop_front();	//头部删除strNum.pop_back();
	}
	cout << "当前节点个数:" << strNum.size() << endl;
	//指定位置操作
	//iterator=find(iterator begin,iterator end,data)查找
	for (int i = 0; i < 3; i++) {
		iNum.push_back(i);
	}
	list<int>::iterator result = find(iNum.begin(), iNum.end(), 2);	//可以用auto result = find(iNum.begin(), iNum.end(), 2);
	//若没有找到返回的是end结束的位置
	if (result == iNum.end()) {
		cout << "未找到指定位置" << endl;
	}
	//insert
	iNum.insert(result, 100);
	for (auto v : iNum) {
		cout << v << "\t";
	}
	cout << endl;
	//删除函数
	iNum.erase(result);
	for (auto v : iNum) {
		cout << v << "\t";
	}
	cout << endl;
	iNum.reverse();//反转列表
	iNum.sort();//iNum.sort(greater<int>())从大到小排序默认是less<int>()
	for (auto v : iNum) {
		cout << v << "\t";
	}
	cout << endl;
}
//如何做相同元素删除!!!!!
void testDelete() {
	int array[5] = { 1,2,2,4,2 };
	list<int> data;
	data.assign(array, array + 5);
	for (auto v : data) {
		cout << v << "\t";
	}
	cout << endl;
	for (list<int>::iterator iter=data.begin(); iter != data.end();) {
		if (*iter == 2) {
			iter = data.erase(iter);//一定要返回一个接受的erase返回的是下一个迭代器
		}
		else {
			iter++;
		}
	}
	
	for (auto v : data) {
		cout << v << "\t";
	}
	cout << endl;
}
//操作自定义类型数据
class MM {
public:
	MM(string name,int age,int num):name(name),age(age),num(num){}
	void print() {
		cout << name << "\t" << age << "\t" << num << endl;
	}
	bool operator==(const string& name)const {
		return this->name == name;
	}
	bool operator<(const MM& object)const {
		return this->name < object.name;
	}
	string getName()const {//string& getName()const错误带const就不能返回引用了,类外可修改,所以不能返回引用
		return name;
	}
	
protected:
	string name;
	int age;
	int num;
};
bool compareByName(const MM& object1,const MM& object2) {
	return object1.getName() < object2.getName();
}
void testMM() {
	list<MM> mmData;
	string name;
	int age;
	int num;
	while (1) {
		cout << "Input MM:" << endl;
		cin >> name >> age >> num;
		mmData.push_back(MM (name, age, num));
		cout << "是否继续输入" << endl;
		while (cin.get() != '\n');
		if (cin.get() == 'n') {
			break;
		}
	}
	cout << "MM的name\tage\tnum" << endl;
	for (auto v : mmData) {
		v.print();
	}
	auto result = find(mmData.begin(), mmData.end(), string("name"));
	list<list<MM>::iterator> resultList;
	resultList.push_back(result);
	//采用重载的方式
	mmData.sort(less<MM>());//重载<
	//mmData.sort(greater<int>());重载>
	//不采用重载的方式需要自己写比较准则
	mmData.sort(compareByName);//sort的参数是一个函数指针,这样避免了同一个操作符参数一样时不能被二次重载(比如比较年龄和姓名等于号重载)
}
int main() {
	testList();
	testDelete();
	testMM();
	return 0;
}

stack栈:

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

void testStack() {
	//穿脱原则,先进后出
	//push(data)入栈
	//pop()删除
	//top()获取栈顶元素
	//size()  empty()
	stack<int> intStack;
		for (int i = 0; i < 3; i++) {
			intStack.push(i);
		}
		while (!intStack.empty()) {
			cout << intStack.top()<<"\t";
			intStack.pop();

		}
		cout << endl;
}
//进制转换
void NumTobinary(int data) {
	stack<int> binaryData;
	while (data) {
		binaryData.push(data % 2);
		data = data / 2;  
	}
	if (binaryData.size() < 8) {
		for (int i = binaryData.size(); i < 8; i++) {
			binaryData.push(0);
		}
	}
	while (!binaryData.empty()) {
		cout << binaryData.top();
		binaryData.pop();
	}
	cout << endl;
	//a+b中缀
	//ab+后缀
	//我们一般读的是中缀表达式,计算机转换成后缀表达式
}

int main() {
	//testStack();
	int data;
	cin >> data;
	NumTobinary(data);
	
	return 0;
}

队列

queue/deque/priority_queue

普通队列queue:

#include<queue>
#include<iostream>
#include<string>
using namespace std;
//队列可以用来遍历二叉树

//若不想影响原容器,自己写一个函数传参不传引用,就不会影响原来的容器(拷贝本)
void pop_queue(queue<int> qData) {
	while (!qData.empty()) {
		cout << qData.front() << " ";//获取队头
		qData.pop();	//出队
	}
	cout << endl;
}
void testQueue() {//先进先出
	queue<int> qData;
	for (int i = 0; i < 3; i++) {
		qData.push(i);//入队
	}
	while (!qData.empty()) {
		cout << qData.front() << " ";//获取队头
		qData.pop();	//出队
	}
	//pop_queue(qData);
	cout << endl;
	cout << qData.size() << endl;
	
}


int main() {
	testQueue();

	return 0;
}

双向队列deque

#include<deque>
#include<iostream>
#include<string>
using namespace std;
//双向队列,可以从头到尾也可以从尾到头,速度较快
void pop_front_deQueue(deque<int> dData) {
	while (!dData.empty()) {
		//从头部出来
		cout << dData.front();
		dData.pop_front();
	}
	cout << endl;
}
void pop_back_deQueue(deque<int> dData) {
	while (!dData.empty()) {
		//从尾部出来
		cout << dData.back();
		dData.pop_back();
	}
	cout << endl;
}
void testDeque() {
	deque<int> deData;
	for (int i = 0; i < 3; i++) {
		deData.push_back(i);	//尾插法入队
		deData.push_front(i);	//头插法入队
		//00
		//1001
		//210012 
	}
	deData.push_back(66666);
	pop_front_deQueue(deData);
	pop_back_deQueue(deData);
}

int main() {

	testDeque();
	return 0;
}

优先队列priority_queue

#include<queue>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//优先队列是按照特定的比较方式出队

//需要一个中间容器充当优先队列的容量
	/*template <class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type>>
class priority_queue {*/
template <class _Ty, class _Container = vector<_Ty>, class _Pr = less<_Ty>>
class my_priority_queue {
public:
protected:
};
//创建方式
void testCreatePriorityQueue() {
	priority_queue<int> pqData1;
	priority_queue<int, vector<int>> pqData2;
	priority_queue<int, vector<int>,less<int>> pqData3;//从小的出队把less改成greater
	
	//默认从大到小出
	pqData1.push(63);
	pqData1.push(52);
	pqData1.push(75);
	while (!pqData1.empty()) {
		cout << pqData1.top() << " ";
		pqData1.pop();
	}
	cout << endl;
}

int main() {
	testCreatePriorityQueue();

	return 0;
}

这篇关于C++容器篇(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!