Extract a SubVector From a Vector in C++

In this article, we are going to learn how to extract a Sub Vector from a vector. Or you can say slice a vector from another vector.

Ways to Extract a SubVector From a Vector in C++


  • Using vector {} List Initialization to Extract a Sub vector From a Vector.
  • Using the copy() Function to Extract a Sub vector From a Vector.

1.vector {} List Initialization to Extract a Sub vector From a Vector in C++


In this method, we are going to initialize a sub-vector with the elements of the main vector. We will provide the elements from a start and end range.let us see in the code below how this can be done.

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
    vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
							
    cout <<"Vector Elements are :";
    for (size_t i = 0; i < vec.size(); ++i) 
	{
        cout << vec[i] << " ";
    }
    cout << endl;
    vector<int> slicevec {vec.begin()+2, vec.begin()+6};

	cout <<"Sliced Vector Elements are :";
    for (size_t i = 0; i < slicevec.size(); ++i) 
    {
        cout << slicevec[i] << " ";
    }

    cout << endl;

    return 0;
}

Output:

Vector Elements are :1 2 3 4 5 6 7 8 9 10
Sliced Vector Elements are :3 4 5 6

Another method to initialize a sub-vector with the elements of the main vector is by providing the addresses of the elements from a given range of elements. Here We will provide the addresses of the elements from a start and end range.let us see in the code below how this can be done.

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
    vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
							
    cout <<"Vector Elements are :";
    for (size_t i = 0; i < vec.size(); ++i) 
    {
        cout << vec[i] << " ";
    }
    cout << endl;
    vector<int> slicevec {&vec[2], &vec[6]};

    cout <<"Sliced Vector Elements are :";
    for (size_t i = 0; i < slicevec.size(); ++i) 
    {
        cout << slicevec[i] << " ";
    }

    cout << endl;

    return 0;
}

Output:

Vector Elements are :1 2 3 4 5 6 7 8 9 10
Sliced Vector Elements are :3 4 5 6

2. copy() Function to Extract a Sub vector From a Vector


In this method, we are going to use the copy() method to extract the sub-vector from a vector. In the copy() function, we provide the element from where we want to start extraction and up to which element we want extraction. This is like the start and end element for the new vector. Then we also specify to which index we want to copy this. This means what will be starting index from where it will start writing this slice of data in a new vector.

Let us see this with code example below:

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
    vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
							
    cout <<"Vector Elements are :";
    for (size_t i = 0; i < vec.size(); ++i) 
    {
        cout << vec[i] << " ";
    }
    cout << endl;
    int range = 0;
    int startindex = 0;
    int endindex = 10;
    cout << "Please enter the start index:";
    cin >> startindex;
    cout << "Please enter the end index:";
    cin >> endindex;
    range = endindex - startindex;
    vector<int> slicevec(range);

    copy(&vec[1], &vec[6], slicevec.begin());
	
    cout <<"Sliced Vector Elements are :";
    for (size_t i = 0; i < slicevec.size(); ++i) 
    {
        cout << slicevec[i] << " ";
    }

    cout << endl;

    return 0;
}

Output:

Please enter the start index:1
Please enter the end index:6
Sliced Vector Elements are :2 3 4 5 6