# Slicing a Vector in C++

• Difficulty Level : Hard
• Last Updated : 19 Jul, 2021

Pre-requisite: Vectors in C++
Slicing a vector means to make a subvector from a given vector.
Given N integers in a vector arr and to positive numbers X and Y, the task is to slice the given vector from index X to Y in a given vector.
Examples:

Input: vector arr = { 1, 3, 4, 2, 4, 2, 1 }, X = 2, Y = 5
Output: 4 2 4 2
Input: vector arr = { 1, 3, 4, 2 }, X = 1, Y = 2
Output: 3 4

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Method 1: The idea is to copy the elements from this range X to Y to a new vector and return it.

1. Get the starting iterator of element at index X as:

`auto start = arr.begin() + X`
1.
2. Get the ending iterator of element at index Y as:

`auto end = arr.begin() + Y + 1`
1.
2. Copy the elements in these range between these iterators using copy() function in vector.

Below is the implementation of the above approach:

## CPP

 `// C++ program for the above approach``#include "bits/stdc++.h"``using` `namespace` `std;` `// Function to slice a given vector``// from range X to Y``vector<``int``> slicing(vector<``int``>& arr,``                    ``int` `X, ``int` `Y)``{` `    ``// Starting and Ending iterators``    ``auto` `start = arr.begin() + X;``    ``auto` `end = arr.begin() + Y + 1;` `    ``// To store the sliced vector``    ``vector<``int``> result(Y - X + 1);` `    ``// Copy vector using copy function()``    ``copy(start, end, result.begin());` `    ``// Return the final sliced vector``    ``return` `result;``}` `// Function to print the vector ans``void` `printResult(vector<``int``>& ans)``{` `    ``// Traverse the vector ans``    ``for` `(``auto``& it : ans) {` `        ``// Print elements``        ``cout << it << ``' '``;``    ``}``}` `// Driver Code``int` `main()``{` `    ``// Given vector``    ``vector<``int``> arr = { 1, 3, 4, 2,``                        ``4, 2, 1 };` `    ``// Given range``    ``int` `X = 2, Y = 5;` `    ``// Function Call``    ``vector<``int``> ans;``    ``ans = slicing(arr, X, Y);` `    ``// Print the sliced vector``    ``printResult(ans);``}`
Output:
`4 2 4 2`

Method 2: The above approach can be implemented using Range Constructor. Below is the implementation of the above approach:

## CPP

 `// C++ program for the above approach``#include "bits/stdc++.h"``using` `namespace` `std;` `// Template class to slice a vector``// from range X to Y``template` `<``typename` `T>``vector slicing(vector ``const``& v,``                  ``int` `X, ``int` `Y)``{` `    ``// Begin and End iterator``    ``auto` `first = v.begin() + X;``    ``auto` `last = v.begin() + Y + 1;` `    ``// Copy the element``    ``vector vector(first, last);` `    ``// Return the results``    ``return` `vector;``}` `// Template class to print the element``// in vector v``template` `<``typename` `T>``void` `printResult(vector ``const``& v)``{` `    ``// Traverse the vector v``    ``for` `(``auto` `i : v) {``        ``cout << i << ``' '``;``    ``}``    ``cout << ``'\n'``;``}` `// Driver Code``int` `main()``{` `    ``// Given vector``    ``vector<``int``> arr = { 1, 3, 4, 2,``                        ``4, 2, 1 };` `    ``// Given range``    ``int` `X = 2, Y = 5;` `    ``// To store the sliced vector``    ``vector<``int``> ans;` `    ``// Function Call``    ``ans = slicing(arr, X, Y);` `    ``// Print the sliced vector``    ``printResult(ans);``}`
Output:
`4 2 4 2`

Method 3: We can also use inbuilt function slice() in C++ STL to slice the given vector. Below is the implementation of the above approach:

## CPP

 `// C++ program for the above approach``#include "bits/stdc++.h"``#include "valarray"``using` `namespace` `std;` `// Function to slice the given array``// elements from range (X, Y)``valarray<``int``> slicing(valarray<``int``> arr,``                      ``int` `X, ``int` `Y)``{` `    ``// Return the slicing of array``    ``return` `arr[slice(X, Y - X + 1, 1)];``}` `// Print the resultant array``// after slicing``void` `printResult(valarray<``int``> v)``{` `    ``// Traverse the vector v``    ``for` `(``auto` `i : v) {``        ``cout << i << ``' '``;``    ``}``    ``cout << ``'\n'``;``}` `// Driver Code``int` `main()``{` `    ``// Given vector``    ``valarray<``int``> arr = { 1, 3, 4, 2,``                          ``4, 2, 1 };` `    ``// Given range``    ``int` `X = 2, Y = 5;` `    ``// To store the sliced vector``    ``valarray<``int``> ans;` `    ``// Function Call``    ``ans = slicing(arr, X, Y);` `    ``// Print the sliced vector``    ``printResult(ans);``}`
Output:
`4 2 4 2`

My Personal Notes arrow_drop_up