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

后端 未结 6 1992
北荒
北荒 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:33

    Try pass array to vector:

    int arr[] = {2,5,8,11,14};
    std::vector TestVector(arr, arr+5);
    

    You could always call std::vector::assign to assign array to vector, call std::vector::insert to add multiple arrays.

    If you use C++11, you can try:

    std::vector v{2,5,8,11,14};
    

    Or

     std::vector v = {2,5,8,11,14};
    

提交回复
热议问题