首页 > 学院 > 开发设计 > 正文

ACM选修第一节内容整理 3.5

2019-11-06 07:07:46
字体:
来源:转载
供稿:网友
万能头文件:#include<bits/stdc++.h>Using namespace std;栈(stack) 先进后出(First In Last Out, FILO) #include<stack> using namespace std; int main() { stack<int> s; s.push(1); //向栈压入一个元素1. s.push(2); s.push(3); cout << "Top: " << s.top() << endl; //返回栈顶元素值。 cout << "Size: " << s.size() << endl; //返回栈内元素个数。 s.pop(); //移除栈顶元素。 cout << "Size: " << s.size() << endl; if(s.empty())//返回bool型 判断栈内是否为空。 { cout << "Is empty" << endl; } else { cout << "Is not empty" << endl; } return 0;}队列(queue) 先进先出(First In First Out, FIFO) 从底端加入元素,从顶端取出元素#include <iostream>#include <algorithm>#include <vector>using namespace std;int main(){ vector<int> a; for (int i = 0; i < 5; ++i){ a.push_back(5 - i); } cout << "Size: " << a.size() << endl; a.pop_back();//删除尾部元素。 a[0] = 1; cout << "Size: " << a.size() << endl; for (int i = 0; i < (int)a.size(); ++i){ cout << a[i] << ", " << endl; } cout << endl; return 0;}sort 排序函数 默认升序排列#include <algorithm>#include <vector>#include <iostream>using namespace std;int main(){ int a[10]; vector <int> b; for(int i=0;i<5;i++) { cin>>a[i]; b.push_back(a[i]); } sort(a,a+5);//此处范围为前开后闭 sort(b.begin(),b.end()); for(int i=0;i<5;i++) cout<<a[i]<<" "; cout<<endl; for(int i=0;i<5;i++) cout<<b[i]<<" "; return 0;}

降序排列方法:

bool cmp(int a, int b){ return a > b;}sort(num, num + 5, cmp); //三个变量,最后一个变量为自定义函数。upper_bound 和 lower_bound 计算有序队列中相同元素的个数upper_bound(begin, end, value); //返回>value的元素的第一个位置。lower_bound(begin, end, value);//返回>=value的元素的第一个位置。num[] = {1,2,2,3,4,5};lower_bound(num, num + 6, 2)为num + 1upper_bound(num, num + 6, 2)为num + 3set 和 multiset 根据特定排列准则,自动将元素排列。 set不允许元素重复。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表