How to copy elements of an Array in a Vector in C++
Last Updated :
27 Jan, 2022
An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.

Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
Following are the different ways to copy elements from an array to a vector:
Method 1: Naive Solution
Traverse the complete array and insert each element into the newly assigned vector using the push_back() function. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v;
for ( int i = 0; i < N; i++)
v.push_back(arr[i]);
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Method 2: Range-based Assignment during Initialization
In C++, the Vector class provides a constructor which accepts a range, so to create a vector from array elements, pass the pointer to the first and last position of the range as the argument during the vector initialization that needs to be copied to the vector i.e, (arr, arr+N).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v(arr, arr + N);
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Note that Iterators used to point at the memory addresses of STL containers can also be used.
- std::begin(arr)- Iterator to the first element of an array.
- std::end(arr)- Iterator to the one after the last element of an array.
vector<int> v(begin(arr), end(arr));
Method 3: Using Inbuilt Function Insert(position, first_iterator, last_iterator): The insert() is a built-in function in C++ STL that inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted. Instead of a single value, a range can also be passed as arguments.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v;
v.insert(v.begin(), arr, arr + N);
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Method 4: Using Inbuilt Function Copy(first_iterator, last_iterator, back_inserter()): This is another way to copy array elements into a vector is to use the inbuilt copy function. This function takes 3 arguments, an iterator to the first element of the array, an iterator to the last element of the array, and the back_inserter function to insert values from the back.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v;
copy(begin(arr), end(arr), back_inserter(v));
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Method 5: Using Inbuilt Function Assign( first_iterator, last_iterator ): The vector::assign() function can be used to assign values to a new vector or an already existing vector. It can also modify the size of the vector if necessary. It takes the iterator to the first and last position as arguments.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v;
v.assign(arr, arr + N);
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Method 6: Using Inbuilt Function Transform(first_iterator, last_iterator, back_insert(), function): The std::transform() function takes 4 arguments, an iterator to the first element of the array, an iterator to the last element of the array, the back_inserter function to insert values from the back and a user-defined function which can be used to modify all the elements of the array i.e, perform a unary operation, convert lower case characters to upper case, etc.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int increment( int x)
{
return x + 1;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
vector< int > v;
transform(arr, arr + N, back_inserter(v), increment);
for ( auto ele : v) {
cout << ele << " " ;
}
return 0;
}
|
Similar Reads
How to Insert an element at a specific position in an Array in C++
An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C++. Given an array arr of size n, this article tells how to insert an element x in this array arr at a specific position pos. Approach: Here's how to do it.
2 min read
Get first and last elements from Array and Vector in CPP
Given an array, find first and last elements of it. Input: {4, 5, 7, 13, 25, 65, 98} Output: First element: 4 Last element: 98 In C++, we can use sizeof operator to find number of elements in an array. // C++ Program to print first and last element in an array #include <iostream> using namespa
2 min read
How to Erase Duplicates and Sort a Vector in C++?
In this article, we will learn how to remove duplicates and sort a vector in C++. The simplest method to remove the duplicates and sort the vector is by using sort() and unique() functions. Letâs take a look at an example: [GFGTABS] C++ #include <bits/stdc++.h> using namespace std; int main()
3 min read
How to Insert a Range of Elements in a Set in C++ STL?
Prerequisites: Set in C++ Sets in C++ are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. Syntax: set<datatype> set_name; Some Basic Func
2 min read
Different Ways to Convert Vector to Array in C++ STL
In C++, vectors are dynamic arrays that can resize according to the number of elements. In this article, we will learn how to convert a vector to a C-style array in C++. The easiest way to convert a vector to array is by creating a new array and copy all the elements of vector into it using copy() f
3 min read
Advantages of vector over array in C++
In C++, both vectors and arrays are used to store collections of elements, but vector offers significant advantages over arrays in terms of flexibility, functionality, and ease of use. This article explores the benefits of using vectors in C++ programming. The major advantages of vector over arrays
3 min read
Copy File To Vector in C++ STL
Prerequisite:Â Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the conte
2 min read
Array of Vectors in C++ STL
Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam
3 min read
Passing a Vector to Constructor in C++
Just like any other data, we can also pass a vector to a constructor of the desired class. We can pass it by value or by reference. What we do with it depends on our requirement. The following are some of the common cases: Copy Data to Member Vector If we want to store the copy of the vector in the
3 min read
How to Get std::vector Pointer to Raw Data in C++?
In C++, vector is a dynamic array that stores the data in the contiguous memory allocated during runtime. It internally keeps the pointer to this raw data memory. In this article, we will learn how to get pointer to the vector's raw data. The easiest method to get the pointer to raw data is by using
2 min read