Initialisation of static vector

前端 未结 6 461
遥遥无期
遥遥无期 2020-12-13 23:48

I wonder if there is the \"nicer\" way of initialising a static vector than below?

class Foo
{
    static std::vector MyVector;
    Foo()
    {
           


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 00:13

    with boost you can use the +=() operator defined in the boost::assign namespace.

    #include 
    
    using namespace boost::assign;
    
    int main()
    {
        static std::vector MyVector;
        MyVector += 4,17,20;
        return 0;
    }
    

    or with static initialization:

    #include 
    
    using namespace boost::assign;
    
    static std::vector myVector = list_of(4)(17)(2);
    
    int main()
    {
        return 0;
    }
    

    or even better, if your compiler supports C++ 11, use initialization lists.

提交回复
热议问题