split vector in half cpp 1

split vector in half cpp

std::vector vec = {89, 15, 51, 27, 98};
std::size_t const half_size = vec.size() / 2;
std::vector half1(vec.begin(), vec.begin() + half_size);
std::vector half2(vec.begin() + half_size, vec.end());

Here is what the above code is Doing:
1. We create a vector of 5 integers.
2. We create a constant variable half_size and assign it the value of the size of the vector divided by 2.
3. We create a vector half1 and assign it the first half of the elements of vec.
4. We create a vector half2 and assign it the second half of the elements of vec.

Similar Posts