Skip to content

Instantly share code, notes, and snippets.

@kinoko3
Last active March 26, 2018 08:54
Show Gist options
  • Save kinoko3/a5cdc7dd19f4eb6e5d4b98b2ead35f9b to your computer and use it in GitHub Desktop.
Save kinoko3/a5cdc7dd19f4eb6e5d4b98b2ead35f9b to your computer and use it in GitHub Desktop.
C++ STL Algorithm
#include <queue>
using namespace std;
int main(){
queue<string> Q;
Q.push("red");
Q.push("yellow");
Q.push("blue");
Q.front(); //取出队头的元素
Q.size(); //返回队列的元素数
Q.pop(); //从队列里取出元素并删除
Q.empty(); //队列为空时返回true
}
#include <algorithm>
#include <iostream>
using namespace std;
// 使用搜索前先排序
int main(){
int A[10] = {1,1,2,3,4,5,6,10,9,15};
int *pos;
int idx;
bool h;
//pos = lower_bound(A, A+10, 3) //返回第一个大于或等于的元素的指针 3
pos = upper_bound(A, A+10, 3); //返回第一个大于的元素的指针 4
h = binary_search(A, A+10, 3); //搜索是否存在,存在则返回true
idx = distance(A, pos);
cout << idx << endl;
return 0;
}
#include <stack>
using namespace std;
int main(){
stack<int> S;
S.push(3); // 压入数据
S.top(); //返回栈顶的元素
S.pop(); //删除栈顶的元素
S.size(); //返回栈的元素数
S.empty(); //栈空的时候返回true
}
#include <vector>
int main(){
vector<int> V;
V.push_back(1); //在向量末尾添加元素
V.size();
V.pop_back(); //删除向量的最后的一个元素
V.begin(); //返回一个指向开头的迭代器
V.end(); //返回指向末尾的得带器;
V.insert(p,x) // 向着位置P处插入元素P
V.clear(); //清楚向量中的所有的元素
V.erase(); //删除向量中位置P的元素
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment