c++ vector combine two vectors

To combine two vectors in C++, you can use the insert function from the vector class. Here are the steps to combine two vectors:

  1. Declare two vectors: cpp std::vector<int> vec1 = {1, 2, 3}; std::vector<int> vec2 = {4, 5, 6};

  2. Create a third vector to store the combined result: cpp std::vector<int> combinedVector;

  3. Use the insert function to combine the two vectors: cpp combinedVector.insert(combinedVector.end(), vec1.begin(), vec1.end()); combinedVector.insert(combinedVector.end(), vec2.begin(), vec2.end());

Here, the insert function takes three arguments: the position to insert the elements, the starting iterator of the range to be inserted, and the ending iterator of the range to be inserted. In this case, we use combinedVector.end() as the position to insert the elements, and vec1.begin() and vec1.end() for the first range, and vec2.begin() and vec2.end() for the second range. This will insert all the elements from vec1 and vec2 into combinedVector.

  1. Print the combined vector: cpp for (int num : combinedVector) { std::cout << num << " "; }

This loop iterates over each element in the combinedVector and prints it.

The output will be: 1 2 3 4 5 6, which is the combined result of vec1 and vec2.