• 一段简单的练习代码,关于vector,queue,stack,priority_queue


    代码:

      1 #include <assert.h>
      2 #include <vector>
      3 #include <queue>
      4 
      5 using namespace std;
      6 
      7 template <class T>
      8 class CStack{
      9 private:
     10     T* bottom;
     11     T* top;
     12     int size;
     13 public:
     14     CStack():top(bottom), bottom(new T[1]), size(1){}
     15     ~CStack(){
     16         if(!bottom)
     17             delete[] bottom;
     18     }
     19     bool empty()
     20     {
     21         return top == bottom;
     22     }
     23     bool full()
     24     {
     25         return (top - bottom) >= size;
     26     }
     27     void resize(int s)
     28     {
     29         T* tmp = bottom;
     30         bottom = new T[s];
     31         top = bottom + (top - tmp);
     32         memcpy(bottom, tmp, (size<s?size:s)*sizeof(T));
     33         size = s;
     34         delete[] tmp;
     35     }
     36     void push(T t)
     37     {
     38         if(full())
     39             resize(size*2);
     40         *top = t;
     41         top++;
     42     }
     43     void pop()
     44     {
     45         if(!empty())
     46             top--;
     47         else
     48             assert(false);
     49     }
     50     T peek()
     51     {
     52         if(!empty())
     53             return *(top-1);
     54         else
     55             assert(false);
     56         return T();
     57     }
     58 
     59 };
     60 
     61 
     62 
     63 int main()
     64 {
     65     CStack<int> stk;
     66     stk.push(1);
     67     stk.push(2);
     68     stk.push(3);
     69     stk.push(4);
     70     while(!stk.empty())
     71     {
     72         printf("%d
    ", stk.peek());
     73         stk.pop();
     74     }
     75 
     76     printf(">> 练习vector:
    ");
     77 
     78     vector<int> v;
     79     v.push_back(1);
     80     v.push_back(2);
     81     v.push_back(3);
     82     v.push_back(4);
     83     v.push_back(5);
     84     printf(">> 插入5个元素之后
    ");
     85     if(v.empty())
     86         printf("true == v.empty()
    ");
     87     printf("size:    	%d
    ", v.size());
     88     printf("capacity:	%d
    ", v.capacity());
     89     printf(">> 清空之后
    ");
     90     v.clear();
     91     if(v.empty())
     92         printf("true == v.empty()
    ");
     93     printf("size:    	%d
    ", v.size());
     94     printf("capacity:	%d
    ", v.capacity());
     95 
     96     // queue没有迭代器
     97     // queue只能访问队首元素
     98     // stack没有迭代器
     99     // stack只能访问栈顶元素
    100     printf(">> 练习queue:
    ");
    101     queue<int> q;
    102     q.push(1);
    103     q.push(2);
    104     q.push(3);
    105     q.push(4);
    106     q.push(5);
    107     q.pop();
    108     q.pop();
    109     if(!q.empty())
    110         printf("%d 
    ", q.front());
    111     q.pop();
    112     if(!q.empty())
    113         printf("%d 
    ", q.front());
    114 
    115     // heap是隐式表达,存在于algorithm中
    116     // 函数有make_heap(将一段数据整理成堆)
    117     // pop_heap(将堆顶放到队尾)
    118     // push_heap(将尾元素放到堆顶并调整)
    119     // sort_heap(堆排序算法)
    120     // priority_queue基于heap实现
    121     getchar();
    122     return 0;
    123 }

    输出结果:

    4
    3
    2
    1
    >> 练习vector:
    >> 插入5个元素之后
    size:           5
    capacity:       6
    >> 清空之后
    true == v.empty()
    size:           0
    capacity:       6
    >> 练习queue:
    3
    4
  • 相关阅读:
    笔记56 Mybatis快速入门(七)
    笔记55 Mybatis快速入门(六)
    笔记54 Mybatis快速入门(五)
    笔记53 Mybatis快速入门(四)
    笔记52 Mybatis快速入门(三)
    笔记51 Mybatis快速入门(二)
    笔记50 Mybatis快速入门(一)
    笔记49 在Spittr应用中整合Hibernate
    198. House Robber,213. House Robber II
    303. Range Sum Query
  • 原文地址:https://www.cnblogs.com/zanzan101/p/3369458.html
Copyright © 2020-2023  润新知