zoukankan      html  css  js  c++  java
  • How to initialize a static const map in c++?

    #include <map>
    using namespace std;
    struct A{     
    static map<int,int> create_map()         
    {           
    map<int,int> m;           
    m[1] = 2;           
    m[3] = 4;           
    m[5] = 6;           
    return m;         
    }     
    static const map<int,int> myMap;
    }; 
    const map<int,int> A:: myMap =  A::create_map(); 
    int main() 
    { }

    假设是vector。能够使用

    #include <iostream>
    #include <vector>
    using namespace std;
    class A
    
    {
    public:
    static vector<int> v2;
    void show()
    {
    for(vector<int>::iterator i = v2.begin() ; i!= v2.end(); i++)
    {
    cout<<*i<<endl;
    }
    }
    };
    vector<int> A::v2(2,6);
    
    int main()
    {
    A obj;
    obj.show();
    return 0; 
    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    BZOJ-3495 前缀优化建图2-SAT
    洛谷P3979 遥远的国度 树链剖分+分类讨论
    hdu
    hdu
    poj
    poj-1330(暴力写的lca)
    树链剖分
    Dijkstra
    Floyed
    最短路径
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4720759.html
Copyright © 2011-2022 走看看