#include <deque>
#include <stack>
#include <queue>
//适配器 依靠其他的数据结构
//没有迭代器
template <typename T,typename Container=deque<T>>
class Stack {
public:
void push(const T& val) {
_con.push_back(val);
}
void pop() {
_con.pop_back();
}
T top()const {
return _con.back();
}
bool empty()const {
return _con.empty();
}
int size()const {
return _con.size();
}
private:
Container _con;
};
int main() {
Stack<int> st;
for (int i = 0; i < 20; i++) {
st.push(rand()%100+1) ;
}
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
stack<int> st1;
for (int i = 0; i < 20; i++) {
st1.push(rand() % 100 + 1);
}
while (!st1.empty()) {
cout << st1.top() << " ";
st1.pop();
}
cout << endl;
queue<int> que;
for (int i = 0; i < 20; i++) {
que.push(rand() % 100 + 1);
}
while (!que.empty()) {
cout << que.front() << " ";
que.pop();
}
cout << endl;
priority_queue<int> pque;
for (int i = 0; i < 20; i++) {
pque.push(rand() % 100 + 1);
}
while (!pque.empty()) {
cout << pque.top() << " ";
pque.pop();
}
cout << endl;
return 0;
}
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <string>
int main() {
#if 0
unordered_multiset<int> set1;
for (int i = 0; i < 100; i++) {
set1.insert(rand() % 30 + 1);
}
cout << set1.size() << endl;
cout << set1.count(15) << endl;
unordered_set<int>::iterator it = set1.begin();
while (it != set1.end()) {
cout << *it << " ";
++it;
}
cout << endl;
unordered_set<int>::iterator it1 = set1.find(15);
if (it1 != set1.end()) {
set1.erase(15);
}
for (int val : set1) {
cout << val << " ";
}
cout << endl;
for (it = set1.begin(); it != set1.end(); ) {
if (*it == 20) {
it=set1.erase(it);
}
else {
++it;
}
}
for (it = set1.begin(); it != set1.end(); ++it) {
cout << *it << " ";
}
cout << endl;
#endif
#if 0
unordered_map<int, string> map1;
map1.insert(make_pair(1000,"张三"));
map1.insert(make_pair(1010, "李四"));
map1.insert({ 1020,"王五" });
map1.insert({ 1030,"赵六" });
map1[1000] = "夏郡役";
map1[2000];
map1[1800] = "小三狗";
cout << map1[1000] << endl;
cout << map1.size() << endl;
unordered_map<int, string>::iterator it = map1.begin();
while (it != map1.end()) {
cout << "id:" << it->first << " name:" << it->second << endl;
++it;
}
it = map1.find(1030);
if (it != map1.end()) {
cout << it->first << " " << it->second << endl;
}
map1.erase(it);
map1.erase(1020);
for (const pair<int, string>& p : map1) {
cout << "id:" << p.first << " name:" << p.second << endl;
}
cout << endl;
#endif
//无序关联器 用于海量数据查重复 去重复
const int ARR_LEN = 1000;
int arr[ARR_LEN] = { 0 };
for (int i = 0; i < ARR_LEN; i++) {
arr[i] = rand() % 10 + 1;
}
unordered_map<int, int> map1;
for (int val : arr) {
/*map1[val]++;*/
unordered_map<int, int>::iterator it = map1.find(val);
if (it != map1.end()) {
it->second++;
}
else {
map1.insert({ val,1 });
//map1.insert(make_pair(val,1));
}
}
for (auto it = map1.begin(); it != map1.end(); ++it) {
cout << it->first << "\t" << it->second << endl;
}
//去重
unordered_set<int> set1;
for (int val : arr) {
set1.insert(val);
}
for (int val : set1) {
cout << val << " ";
}
cout << endl;
return 0;
}
#include <set>
#include <map>
class Student {
public:
Student(int id=0, string name="")
:_id(id)
,_name(name)
{}
bool operator<(const Student& src)const {
return _id < src._id;
}
private:
int _id;
string _name;
friend ostream& operator<<(ostream& out, const Student& src);
};
ostream& operator<<(ostream& out, const Student& src) {
out << "id:" << src._id << " " << "name:" << src._name ;
return out;
}
int main() {
map<int, Student> map1;
Student stu1(1000, "张飞");
Student stu2(1010, "关羽");
Student stu3(920, "刘备");
map1.insert(make_pair(1000,stu1));
map1.insert(make_pair(1010, stu2));
map1.insert(make_pair(920, stu3));
map<int, Student>::iterator it = map1.begin();
for (; it != map1.end(); ++it) {
cout << it->first << " " << it->second << endl;
}
#if 0
set<Student> set1;
Student stu1(1000, "张飞");
Student stu2(1010, "关羽");
Student stu3(920, "刘备");
set1.insert(stu1);
set1.insert(stu2);
set1.insert(stu3);
for (Student s : set1) {
cout << s << endl;
}
#endif
#if 0
set<int> set1;
for (int i = 0; i < 20; i++) {
set1.insert(rand() % 20 + 1);
}
for (int val : set1) {
cout << val << " ";
}
cout << endl;
cout << set1.size() << endl;
cout << set1.count(1) << endl;
#endif
return 0;
}
template<typename T>
bool mygreater(T a, T b) {
return a > b;
}
template<typename T>
bool myless(T a, T b) {
return a < b;
}
template <typename T,typename Compare>
bool compare(T a, T b,Compare cmp) {
return cmp(a , b);
}
int main() {
cout << compare(10, 20, mygreater<int>) << endl;
cout << compare('a', 'b', myless<int>) << endl;
return 0;
}
#include <queue>
int main() {
priority_queue<int> pque;
for (int i = 0; i < 20; i++) {
pque.push(rand() % 20);
}
while (!pque.empty()) {
cout << pque.top() << " ";
pque.pop();
}
cout << endl;
using MinHeap = priority_queue<int, vector<int>, greater<int>>;
MinHeap que;
for (int i = 0; i < 20; i++) {
que.push(rand() % 20 + 1);
}
while (!que.empty()) {
cout << que.top() << " ";
que.pop();
}
cout << endl;
return 0;
}