如何将多个整数同时传递到一个向量中?

目前当我不得不多次使用 vector.push_back()的时候。

我现在使用的代码是

  std::vector<int> TestVector;
TestVector.push_back(2);
TestVector.push_back(5);
TestVector.push_back(8);
TestVector.push_back(11);
TestVector.push_back(14);

有没有一种方法只使用 vector.push_back()一次,并只传递多个值到矢量?

165745 次浏览

Try pass array to vector:

int arr[] = {2,5,8,11,14};
std::vector<int> 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<int> v{2,5,8,11,14};

Or

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

You can also use vector::insert.

std::vector<int> v;
int a[5] = {2, 5, 8, 11, 14};


v.insert(v.end(), a, a+5);

Edit:

Of course, in real-world programming you should use:

v.insert(v.end(), a, a+(sizeof(a)/sizeof(a[0])));  // C++03
v.insert(v.end(), std::begin(a), std::end(a));     // C++11

You can also use Boost.Assignment:

const list<int> primes = list_of(2)(3)(5)(7)(11);


vector<int> v;
v += 1,2,3,4,5,6,7,8,9;

You can do it with initializer list:

std::vector<unsigned int> 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 });

using vector::insert (const_iterator position, initializer_list il); http://www.cplusplus.com/reference/vector/vector/insert/

#include <iostream>
#include <vector>


int main() {
std::vector<int> vec;
vec.insert(vec.end(),{1,2,3,4});
return 0;
}

These are the three most straight forward methods:

1) Initialize from an initializer list:

std::vector<int> TestVector = {2,5,8,11,14};

2) Assign from an initializer list:

std::vector<int> TestVector;
TestVector.assign( {2,5,8,11,14} ); // overwrites TestVector

3) Insert an initializer list at a given point:

std::vector<int> TestVector;
...
TestVector.insert(end(TestVector), {2,5,8,11,14} ); // preserves previous elements

These days (c++17) it's easy:

auto const pusher([](auto& v) noexcept
{
return [&](auto&& ...e)
{
(
(
v.push_back(std::forward<decltype(e)>(e))
),
...
);
};
}
);


pusher(TestVector)(2, 5, 8, 11, 14);

EDIT: for the really adventurous (c++20):

template <typename F, class Tuple>
constexpr void operator|(Tuple&& t, F f)
{
[&]<auto ...I>(std::index_sequence<I...>) noexcept(noexcept((f(std::get<I>(t)), ...)))
{
(f(std::get<I>(t)), ...);
}
(std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>());
}


std::forward_as_tuple(2, 5, 8, 11, 14) | [&](auto&& e){ TestVector.push_back(std::forward<decltype(e)>(e)); }

Yes you can, in your case:

vector<int>TestVector;`
for(int i=0;i<5;i++)
{


TestVector.push_back(2+3*i);
    

}

Since c++17 you could use the following method:

#include <iostream>
#include <vector>


using namespace std;


vector<int> vec;
template<typename... T>
void vecPush(const T& ... x) {
(vec.push_back(x), ...);
}


int main() {
vecPush(4, 10, 4);
for(const auto& a : vec)
cout << a << " ";
return 0;
}