
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to append a vector in a vector in C++?
To append a vector in a vector can simply be done by vector insert() method.
Algorithm
Begin Declare a function show(). Pass a constructor of a vector as a parameter within show() function. for (auto const& i: input) Print the value of variable i. Declare vect1 of vector type. Initialize values in the vect1. Declare vect2 of vector type. Initialize values in the vect2. Call vect2.insert(vect2.begin(), vect1.begin(), vect1.end()) to append the vect1 into vect2. Print “Resultant vector is:” Call show() function to display the value of vect2. End.
Example Code
#include <iostream> #include <vector> #include <algorithm> using namespace std; void show(vector<int> const &input) { for (auto const& i: input) { std::cout << i << " "; } } int main() { vector<int> v1 = { 1, 2, 3 }; vector<int> v2 = { 4, 5 }; v2.insert(v2.begin(), v1.begin(), v1.end()); cout<<"Resultant vector is:"<<endl; show(v2); return 0; }
Output
Resultant vector is: 1 2 3 4 5
- Related Articles
- Append all elements of another Collection to a Vector in Java
- How to convert a string vector into an integer vector in R?
- How to initialize a vector in C++?
- How to reverse a vector in R?
- How to generate a normal random vector using the mean of a vector in R?
- How to multiply each element of a larger vector with a smaller vector in R?
- How to replace values in a vector with values in the same vector in R?
- How to extract the names of vector values from a named vector in R?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to shuffle a std::vector in C++
- How to find minimum value in a numerical vector which is written as a character vector in R?
- How does a vector work in C++?
- How to minus every element of a vector with every element of another vector in R?
- How to multiply a matrix with a vector in R?
- How to reverse a Vector using STL in C++?

Advertisements