How do I pass multiple ints into a vector at once?

后端 未结 6 1988
北荒
北荒 2020-12-01 04:08

Currently when I have to use vector.push_back() multiple times.

The code I\'m currently using is

  std::vector TestVector;
         


        
6条回答
  •  醉梦人生
    2020-12-01 04:57

    You can do it with initializer list:

    std::vector array;
    
    // First argument is an iterator to the element BEFORE which you will insert:
    // In this case, you will insert before the end() iterator, which means appending value
    // at the end of the vector.
    array.insert(array.end(), { 1, 2, 3, 4, 5, 6 });
    

提交回复
热议问题