how-to initialize 'const std::vector' like a c array

后端 未结 10 803
走了就别回头了
走了就别回头了 2020-11-27 02:59

Is there an elegant way to create and initialize a const std::vector like const T a[] = { ... } to a fixed (and small) number of val

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 03:17

    You either have to wait for C++0x or use something like Boost.Assign to do that.

    e.g.:

    #include 
    using namespace boost::assign; // bring 'operator+=()' into scope
    
    vector v;
    v += 1,2,3,4,5;
    

    for C++11:

    vector luggage_combo = { 1, 2, 3, 4, 5 };
    

提交回复
热议问题