C++标准库使用

本文介绍了C++标准库中的数据结构,包括stack(栈)、queue(队列)、vector(向量)和list(链表)的基本操作和应用实例。通过示例代码展示了它们在处理运算表达式、颜色存储和数据操作等方面的功能,强调了这些数据结构在程序设计中的重要性。
摘要由CSDN通过智能技术生成

标准库的数据结构

stack

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

int main() {
	stack<int> s;

	s.push(3);
	s.push(7);
	s.push(1);
	cout << s.size() << endl;
	
	cout << s.top() << " ";
	s.pop();
	
	cout << s.top() << " "; 
	
	s.push(5);
	
	cout << s.top() << " ";
	return 0;

}
3
1 7 5

总结

size() 返回栈的元素数

top() 返回栈顶的元素

pop() 从栈中取出并删除

push() 从栈中添加元素

empty() 在栈为空时返回true

#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;

int main() {
	stack<int> S;

	int a, b, x;
	string s;
	
	while(cin >> s) {
		if(s[0] == '+') {
			a = S.top();
			S.pop();
			b = S.top();
			S.pop();
			S.push(a + b);
		} else if(s[0] == '-') {
			b = S.top();
			S.pop();
			a = S.top();
			S.pop();
			S.push(a - b);
		} else if(s[0] == '*') {
			a = S.top();
			S.pop();
			b = S.top();
			S.pop();
			S.push(a * b);
		} else {
			S.push(atoi(s.c_str()));
		}
	}
	cout << S.top() << endl;
	return 0;

}
1 2 + 3 4 - *
^Z
-3

queue

#include<bits/stdc++.h>
using namespace std;

int main() {
	queue<string> Q;
	
	Q.push("red");
	Q.push("yellow");
	Q.push("yellow");
	Q.push("blue");
	
	// red
	cout << Q.front() << " " << endl;
	Q.pop();
	
	// yellow
	cout << Q.front() << " " << endl;
	Q.pop();
	
	// yellow
	cout << Q.front() << " " << endl;
	Q.pop();
	
	Q.push("green");
	
	// blue
	cout << Q.front() << " " << endl;
	Q.pop();
	
	// green
	cout << Q.front() << " " << endl;
	
	return 0;
}

size() 返回队列的元素数

front() 返回队头的元素

pop() 从队列中取出并删除元素

push() 向队列中添加元素x

empty() 在队列为空时返回true

#include<bits/stdc++.h>
using namespace std;

int main() {
	int n, q, t;
	string name;
	
	queue<pair<string, int> > Q;
	
	cin >> n >> q;
	
	for(int i = 0; i < n; i++) {
		cin >> name >> t;
		Q.push(make_pair(name, t));
	}
	
	pair<string, int> u;
	int elaps = 0, a;
	
	while(!Q.empty()) {
		u = Q.front();
		Q.pop();
		
		a = min(u.second, q);
		u.second -= a;
		elaps += a;
		
		if(u.second > 0) {
			Q.push(u);
		} else {
			cout << u.first << " " << elaps << endl;
		}
	}

	return 0;
}
5 100
p1 150
p2 80
p3 200
p4 350
p5 20
    
p2 180
p5 400
p1 450
p3 550
p4 800

vector

#include<bits/stdc++.h>
using namespace std;

void print(vector<double> V) {
	int vlen = V.size();
	for(int i = 0; i < vlen; i++) {
		cout << V[i] << " ";
	}
	cout << endl;
}

int main() {
	vector<double> V;
	V.push_back(0.1);
	V.push_back(0.2);
	V.push_back(0.3);
	
	V[2] = 0.4;
	print(V);
	
	V.insert(V.begin() + 2, 0.8);
	print(V);
	
	V.erase(V.begin() + 1);
	print(V);
	
	V.push_back(0.9);
	print(V);
	
	return 0;
}

size() 返回向量的元素数

push_back(x) 在向量末尾添加元素x

pop_back() 删除向量的最后一个元素

begin() 返回指向向量开头的迭代器

end() 返回指向向量末尾(最后一个元素的后一个位置)的迭代器

insert(p, x) 在向量的位置p处插入元素x

erase§ 删除向量中位置p的元素

clear() 删除向量中所有元素

list

#include<bits/stdc++.h>
using namespace std;

int main() {
	// 双向链表list 
	list<char> L;
	
	// [b]
	L.push_front('b');
	// [bc]
	L.push_back('c');
	// [abc]
	L.push_front('a');
	
	// a
	cout << L.front();
	// c
	cout << L.back();
	
	// [bc]
	L.pop_front();
	// [bcd]
	L.push_back('d');
	
	// b
	cout << L.front();
	// d
	cout << L.back();
	return 0;

}

size() 返回表的元素数

begin() 返回指向表开头的迭代器

end() 返回指向表末尾(最后一个元素的后一个位置)的迭代器

push_front(x) 在表的开头添加元素x

push_back(x) 在表的末尾添加元素x

pop_front() 删除位于表开头的元素

pop_back() 删除位于表结尾的元素

insert(p, x) 在表的位置p处插入x

erase§ 删除表中p的元素

clear() 删除表中所有的元素

#include<bits/stdc++.h>
using namespace std;

int main() {
	int q, x;
	char com[20];
	
	// 双向链表list 
	list<int> v;
	cin >> q;
	for(int i = 0; i < q; i++) {
		cin >> com;
		if(com[0] == 'i') {
			cin >> x;
			v.push_front(x);
		} else if(com[6] == 'L') {
			v.pop_back();
		} else if(com[6] == 'F') {
			v.pop_front();
		} else {
			cin >> x;
			list<int>::iterator it;
			for(it = v.begin(); it != v.end(); it++) {
				if(*it == x) {
					v.erase(it);
					break;
				}
			}
		}
	}
	
	int i = 0;
	for(list<int>::iterator it = v.begin(); it != v.end(); it++) {
		if(i++) {
			cout << " ";
			cout << *it;
		}
		cout << endl;
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值