代码之家  ›  专栏  ›  技术社区  ›  Hooked

使用STL/Boost初始化硬编码集<vector<int>>

  •  4
  • Hooked  · 技术社区  · 16 年前

    这样地 question 我已经问过了,我想用STL初始化一个容器,其中元素以最干净的方式进行硬编码。在本例中,元素是双重嵌套的容器:

    set<vector<int> > A;
    

    A = [[0,0,1],[0,1,0],[1,0,0],[0,0,0]];
    

    C++ 0x使用 g++

    2 回复  |  直到 9 年前
        1
  •  10
  •   Matthew Flaschen    16 年前

    它使用g++4.4.1,并且-std=c++0x

    #include <set>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        set<vector<int>> A = {{0,0,1},{0,1,0},{1,0,0},{0,0,0}};
    
    }
    
        2
  •  7
  •   Eddy Pronk    16 年前
    #include <boost/assign/list_of.hpp> 
    #include <vector>
    #include <set>
    
    using namespace std;
    using namespace boost::assign;
    
    int main()
    {
        set<vector<int> > A;
    
        A = list_of
            (list_of(0)(0)(1))
            (list_of(0)(1)(0))
            (list_of(1)(0)(0));
            (list_of(0)(0)(0));
        return 0;
    }