还没写完O~
到时候偶会再补充滴~
无耻求赞
#include <windows.h>
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <deque>
using namespace std;
vector<int> v;
queue<int> q;
priority_queue<int> q1;
priority_queue <int, vector <int>, greater <int> > q2;
stack<int> s;
string str;
deque<int> dq;
void use_vector() {
while(1) {
Sleep(500);
system("cls");
puts("0. 退出use_vector函数"); Sleep(100);
puts("1.push_back 在数组的最后添加一个元素"); Sleep(100);
puts("2.pop_back 删除数组的最后一个元素"); Sleep(100);
puts("3.size 数组当前的大小"); Sleep(100);
puts("4.empty 是否为空"); Sleep(100);
puts("5.clear 清空vector"); Sleep(100);
puts("6.front 返回vector中的第一个元素"); Sleep(100);
puts("7.back 返回vector中最后一个元素"); Sleep(100);
puts("8.[] 随机访问第x个元素"); Sleep(100);
puts("9. 输出所有元素第一种方法"); Sleep(100);
puts("10. 输出所有元素第二种方法"); Sleep(100);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_vector函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x; scanf("%d", &x);
v.push_back(x);
printf("在数组末尾加上元素%d\n", x);
}
if (opt == 2) {
puts("删除数组末尾的元素");
v.pop_back();
}
if (opt == 3) printf("当前vector数组的实际长度是:%d\n", v.size());
if (opt == 4) {
int f = v.empty();
if (f == 1) puts("v.empty()返回1,也就是当前vector数组为空");
else puts("v.empty()返回0,也就是当前vector数组不为空");
}
if (opt == 5) {
v.clear();
puts("vector数组已清空");
}
if (opt == 6) printf("当前vector数组的第一个元素是%d\n", v.front());
if (opt == 7) printf("当前vector数组最后一个元素是%d\n", v.back());
if (opt == 8) {
int x = 0; scanf("%d", &x);
if (x == 0) puts("数据出错");
else if (x > v.size()) printf("当前vector数组的长度是%d,输入的x超出了这个范围\n", v.size());
else printf("当前vector数组的第%d个元素是%d", x, v[x - 1]);
}
if (opt == 9) {
puts("使用第一种方法输出vector的所有元素");
for (int i = 0;i < v.size(); i++) printf("%d ", v[i]);
puts("");
}
if (opt == 10) {
puts("使用第二种方法输出vector的所有元素");
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) printf("%d ", *it);
puts("");
}
}
}
void use_queue() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_queue函数"); Sleep(170);
puts("1.push 入队"); Sleep(170);
puts("2.pop 出队"); Sleep(170);
puts("3.front 取队头元素"); Sleep(170);
puts("4.back 取队尾元素"); Sleep(170);
puts("5.size 队列的大小"); Sleep(170);
puts("6.empty 判空"); Sleep(170);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_queue函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x; scanf("%d", &x);
printf("元素%d入队\n", x);
q.push(x);
}
if (opt == 2) {
puts("出队");
q.pop();
}
if (opt == 3) printf("队头元素是%d\n", q.front());
if (opt == 4) printf("队尾元素是%d\n", q.back());
if (opt == 5) printf("当前队列的长度是%d\n", q.size());
if (opt == 6) {
int f = q.empty();
if (f == 1) puts("q.empty()返回1,也就是当前队列为空");
else puts("q.empty()返回0,也就是当前队列不为空");
}
}
}
void use_priority_queue1() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_priority_queue1函数"); Sleep(200);
puts("1.push 把元素插入堆"); Sleep(200);
puts("2.pop 删除堆顶元素"); Sleep(200);
puts("3.top 查询堆顶元素"); Sleep(200);
puts("4.size 堆的元素个数"); Sleep(200);
puts("5.empty 判空"); Sleep(200);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_priority_queue1函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x = 0; scanf("%d", &x);
printf("将元素%d插入堆\n", x);
q1.push(x);
}
if (opt == 2) {
puts("删除堆顶元素");
q1.pop();
}
if (opt == 3) printf("堆顶的元素是%d\n", q1.top());
if (opt == 4) printf("堆的元素个数是%d\n", q1.size());
if (opt == 5) {
int f = q1.empty();
if (f == 1) puts("q1.empty()返回1,也就是当前堆为空");
else puts("q1.empty()返回0,也就是当前堆不为空");
}
}
}
void use_priority_queue2() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_priority_queue2函数"); Sleep(200);
puts("1.push 把元素插入堆"); Sleep(200);
puts("2.pop 删除堆顶元素"); Sleep(200);
puts("3.top 查询堆顶元素"); Sleep(200);
puts("4.size 堆的元素个数"); Sleep(200);
puts("5.empty 判空"); Sleep(200);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_priority_queue2函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x = 0; scanf("%d", &x);
printf("将元素%d插入堆\n", x);
q2.push(x);
}
if (opt == 2) {
puts("删除堆顶元素");
q2.pop();
}
if (opt == 3) printf("堆顶的元素是%d\n", q2.top());
if (opt == 4) printf("堆的元素个数是%d\n", q2.size());
if (opt == 5) {
int f = q2.empty();
if (f == 1) puts("q2.empty()返回1,也就是当前堆为空");
else puts("q2.empty()返回0,也就是当前堆不为空");
}
}
}
void use_stack() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_stack函数"); Sleep(200);
puts("1.push 元素进栈"); Sleep(200);
puts("2.pop 元素出栈"); Sleep(200);
puts("3.top 返回栈顶元素"); Sleep(200);
puts("4.size 返回栈的大小"); Sleep(200);
puts("5.empty 判空"); Sleep(200);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_stack函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x = 0; scanf("%d", &x);
printf("元素%d入栈", x);
s.push(x);
}
if (opt == 2) {
puts("栈顶元素出栈");
s.pop();
}
if (opt == 3) printf("栈顶元素是%d\n", s.top());
if (opt == 4) printf("栈的大小是%d\n", s.size());
if (opt == 5) {
int f = s.empty();
if (f == 1) puts("s.empty()返回1,也就是当前栈为空");
else puts("s.empty()返回0,也就是当前栈不为空");
}
}
}
void use_string() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_string函数"); Sleep(166);
puts("1.[] 随机访问"); Sleep(166);
puts("2.size/length 长度"); Sleep(166);
puts("3.empty 判空"); Sleep(166);
puts("4.clear 清空"); Sleep(166);
puts("5.cin 输入字符串"); Sleep(166);
puts("6.push_back 在末尾插入字符"); Sleep(166);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_string函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x = 0; scanf("%d", &x);
if (x > str.size() - 1) puts("数据范围出错!");
else printf("字符串str的第%d位是%c", x, str[x]);
}
if (opt == 2) printf("字符串str的长度是%d", str.size());
if (opt == 3) {
int f = str.empty();
if (f == 1) puts("str.empty()返回1,也就是说当前字符串str为空");
else puts("str.empty()返回0,也就是说当前字符串str不为空");
}
if (opt == 4) puts("字符串str已清空!"), str.clear();
if (opt == 5) cin >> str, puts("读入字符串str");
if (opt == 6) {
char ch; cin >> ch;
str.push_back(ch);
printf("插入字符%c\n", ch);
}
}
}
void use_deque() {
while (1) {
Sleep(500);
system("cls");
puts("0. 退出use_deque函数"); Sleep(100);
puts("1.push_front 队头入队"); Sleep(100);
puts("2.push_back 队尾入队"); Sleep(100);
puts("3.pop_front 队头出队"); Sleep(100);
puts("4.pop_back 队尾出队"); Sleep(100);
puts("5.front 取队头元素"); Sleep(100);
puts("6.back 取队尾元素"); Sleep(100);
puts("7.clear 清空队列"); Sleep(100);
puts("8.[] 随机访问"); Sleep(100);
puts("9.size 大小"); Sleep(100);
puts("10.empty 判空"); Sleep(100);
int opt = 0; scanf("%d", &opt);
if (opt == 0) {
puts("退出use_deque函数!");
Sleep(500);
return;
}
if (opt == 1) {
int x; scanf("%d", &x);
dq.push_front(x);
}
if (opt == 2) {
int x; scanf("%d", &x);
dq.push_back(x);
}
if (opt == 3) dq.pop_front();
if (opt == 4) dq.pop_back();
if (opt == 5) printf("双端队列dq的队头元素是%d\n", dq.front());
if (opt == 6) printf("双端队列dq的队尾元素是%d\n", dq.back());
if (opt == 7) puts("双端队列dq已清空!"), dq.clear();
if (opt == 8) {
int x = 8; scanf("%d", &x);
if (x > dq.size() - 1) puts("数据出错!");
else printf("双端队列dq的第%d个元素是%d\n", x, dq[x]);
}
if (opt == 9) printf("双端队列dq的元素个数是%d\n", dq.size());
if (opt == 10) {
int f = dq.empty();
if (f == 1) puts("dq.empty()返回1,也就是当前双端队列dq为空");
else puts("dq.empty()返回0,也就是当前双端队列dq不为空");
}
}
}
int main() {
while (1) {
system("cls");
puts("0.退出");
puts("1.use_vector 动态数组"); Sleep(100);
puts("2.use_queue 队列"); Sleep(100);
puts("3.use_priority_queue1 大根堆"); Sleep(100);
puts("4.use_priority_queue2 小根堆"); Sleep(100);
puts("5.use_stack 栈"); Sleep(100);
puts("6.use_string 字符串"); Sleep(100);
puts("7.use_deque 双端队列"); Sleep(100);
int opt = 0; scanf("%d", &opt);
if(opt == 0) {
int f = 0;
puts("真的要退出吗?若退出输入1,否则输入0");
scanf("%d", &f);
if (f) {
puts("退出程序!");
system("pause");
return 0;
}
else continue;
}
if (opt == 1) use_vector();
if (opt == 2) use_queue();
if (opt == 3) use_priority_queue1();
if (opt == 4) use_priority_queue2();
if (opt == 5) use_stack();
if (opt == 6) use_string();
if (opt == 7) use_deque();
}
return 0;
}
good
谢谢支持
n–
e–
w–
# B
hhh
棒!
谢谢支持~