zoukankan      html  css  js  c++  java
  • STL

    常用的STL有

      stack,queue,map,vector

      这些都是封装好的容器,容器,顾名思义,可以存放任一类型,包括结构体类型。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct Node
     4 {
     5     int id;
     6     int val;
     7     Node(int id,int val):id(id),val(val){}
     8 };
     9 stack<Node >_stack;
    10 
    11 int main()
    12 {
    13     //调用栈顶的id元素
    14     _stack.push(Node(1,2));
    15     int id=_stack.top().id;
    16     cout<<id<<endl;
    17 }
    stack存放结构体类型

      最常用的是vector容器,在图论中,当顶点数较多时,以至于数组开不下,或者是顶点多,边数少,用数组会MLE,这时就可以选择用vector容器,非常好用的一个容器。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define pb push_back
     4 const int maxV=1e5+50;//最大的顶点数
     5 struct Node
     6 {
     7     int to;
     8     int weight;
     9     Node(int to,int weight):to(to),weight(weight){}
    10 };
    11 vector<Node >G[maxV];
    12 void addEdge(int u,int v,int w)//加边操作
    13 {
    14     G[u].pb(Node(v,w));//加入一条 u -> v 的边,且权重为 w
    15 }
    图的邻接表表示(vector)

      其实,用嵌套map也可以高效的存储图中的边,且占用的内存也比数组小。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 map<int ,map<int ,int > >G;
     5 
     6 int main()
     7 {
     8     G.clear();
     9     for(int i=1;i <= 5;++i)
    10     {
    11         int u,v,w;
    12         scanf("%d%d%d",&u,&v,&w);
    13         G[u][v]=w;//有向图 u - > v , 权重为 w
    14     }
    15     //嵌套map的遍历
    16     //遍历谁就用谁的迭代器
    17     map<int ,map<int ,int > >::iterator it;
    18     for(it=G.begin();it != G.end();++it)
    19     {
    20         int u=it->first;
    21         for(map<int ,int >::iterator iter=G[u].begin();iter != G[u].end();++iter)
    22         {
    23             int v=iter->first;
    24             int w=iter->second;
    25             cout<<u<<"->"<<v<<' '<<w<<endl;//u->v 权重为 w
    26         }
    27     }
    28     return 0;
    29 }
    图的邻接表表示(嵌套map)

      图的存储方式还有一种比较好用的数据结构,那就是链式前向星。

    题目一览表 来源 考察知识点 完成时间
    A  2559 Largest Rectangle in a Histogram poj 2018.10.12
  • 相关阅读:
    linux文件操作
    文件和目录维护
    随心而记
    开学了
    基本逻辑门电路原理
    ubuntu搭建ftp服务
    中断
    数码管
    字,寻址和移位
    Error: Could not find or load main class resourcemanager
  • 原文地址:https://www.cnblogs.com/violet-acmer/p/9780420.html
Copyright © 2011-2022 走看看