zoukankan      html  css  js  c++  java
  • stl总结精简版

    STL 精简版

    vetor

     1 #include<vector>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<iostream> 
     5 using namespace std;
     6 int main()
     7 {
     8     vector <int> a;//定义一维数组
     9     vector <int *> tm; //定义二位数组
    10     int b = 5;
    11     a.push_back(b);//在数组的最后添加一个数据
    12     a.push_back(1);
    13     a.push_back(2);
    14     a.pop_back();//去掉数组的最后一个数据
    15     cout<<a.at(1)<<endl;//得到编号位置的数据 
    16     vector<int>::iterator it;
    17     for(it = a.begin(); it != a.end(); it++){
    18         printf("%d ",*it);
    19     }
    20     puts("");
    21     printf("front = %d; back = %d
    ",a.front(),a.back());
    22     it = a.begin();
    23     a.erase(it);//删除指针数据项
    24     for(int i = 0; i < a.size(); i++){
    25         printf("%d ",a[i]);
    26     }
    27     puts("");
    28     a.clear();//清空 
    29     if(a.empty()) puts("now its over");
    30     return 0;
    31 }

     set

     1 #include<set>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 struct Node{
     7     char str[100];
     8     bool operator < (const Node a) const {
     9         return (strcmp(str,a.str) <= 0);
    10     }
    11 };
    12 int main()
    13 {
    14     set<int>s;
    15     s.insert(1);
    16     s.insert(2);
    17     s.insert(3);
    18     //set中不会有重复的元素,所以我们如果需要去重,直接丢到set里是不错的选择
    19     //set可以定义比较函数,因为set可以通过中序遍历直接从小到大的排序输出
    20     
    21     //中序遍历
    22     set<int>::iterator it;
    23     for(it = s.begin(); it != s.end(); it++){
    24         printf("%d ",*it);
    25     } 
    26     puts("");
    27     s.erase(2);//删除键值为2的元素
    28     printf("find(3) = %d
    ",s.find(3));
    29     for(it = s.begin(); it != s.end(); it++){
    30         printf("%d ",*it);
    31     } 
    32     puts("");
    33     s.clear();//清空set 
    34     if(s.empty()) puts("Now its over");
    35     
    36     set<Node> n;
    37     Node tm;
    38     tm.str[0] = 's'; tm.str[1] = 'o';tm.str[2] = 0;
    39     n.insert(tm);
    40     Node tm2;
    41     tm2.str[0] = 'g'; tm2.str[1] = 'e';tm2.str[2] = 0;
    42     printf("%d
    ", (tm < tm2));
    43     printf("%d
    ", (strcmp("ge", "so")));
    44     tm.str[0] = 'g'; tm.str[1] = 'e';tm.str[2] = 0;
    45     n.insert(tm);
    46     tm.str[0] = 's'; tm.str[1] = 'i';tm.str[2] = 0;
    47     n.insert(tm);
    48     tm.str[0] = 'b'; tm.str[1] = 'e';tm.str[2] = 0;
    49     n.insert(tm);
    50     tm.str[0] = 'd'; tm.str[1] = 'n';tm.str[2] = 0;
    51     n.insert(tm);
    52     set<Node>::iterator p;
    53     for(p = n.begin(); p!=n.end(); p++ ){
    54         printf("%s ",(*p).str);
    55     }
    56      puts("");
    57     return 0;
    58 }

     stack

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<stack>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     stack<int> st;
    10     st.push(1);
    11     st.push(2);
    12     st.push(3);
    13     while(!st.empty()){
    14         printf("%d ", st.top());
    15         st.pop();
    16     }
    17     puts("");
    18     printf("size = %d
    ",st.size());
    19     return 0;
    20 }

     queue

     1 #include<cstdio>
     2 #include<queue>
     3 using namespace std;
     4 int main()
     5 {
     6     queue<int> qu;
     7     for(int i = 0; i < 5; i++){
     8         qu.push(i);
     9     }
    10     printf("front = %d; back = %d
    ",qu.front(), qu.back());
    11     while(!qu.empty()){
    12         printf("%d ",qu.front());
    13         qu.pop();
    14     }
    15     puts("");
    16     printf("size = %d
    ",qu.size());
    17     return 0;
    18 }

     priority_queue

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 int main()
     6 {
     7     priority_queue<int> q;
     8     for(int i = 0;i < 9; i++) q.push(i);
     9     while(!q.empty()){
    10         printf("%d ",q.top());
    11         q.pop();
    12     }
    13     puts("");
    14     return 0;
    15 }
  • 相关阅读:
    什么是模板缓冲(Stencil Buffer)
    linux的source命令
    设置屏幕不被锁屏
    【转】OpenGL ES EGL & TLS(线程局部存储) & G3D
    [转]OpenGL混色介绍
    【转】Android 启动过程汇总
    Analyzing the memory usage of your Android application
    【转】EGL接口介绍(转)
    Android 核心分析(13) Android GWES之Android窗口治理
    /dev/zero和/dev/null的区别
  • 原文地址:https://www.cnblogs.com/shanyr/p/7519559.html
Copyright © 2011-2022 走看看