c++ vector initialization

前端 未结 5 844
抹茶落季
抹茶落季 2020-12-20 15:47

I have been using the following vector initialization with values in Code::Blocks and MingW compiler:

vector v0 {1,2,3,4};

A

5条回答
  •  离开以前
    2020-12-20 16:24

    I have defined a macro :

    #define init_vector(type, name, ...)\
        const type _init_vector_##name[] { __VA_ARGS__ };\
        vector name(_init_vector_##name, _init_vector_##name + _countof(_init_vector_##name))
    

    and use like this :

    init_vector(string, spell, "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" );
    
    for(auto &a : spell)
      std::cout<< a <<" ";
    

提交回复
热议问题