How to Extract a Subvector From a Vector in C++

  1. Use {} List Initialization Notation to Extract a Subvector From a Vector
  2. Use the copy() Function to Extract a Subvector From a Vector

This article will explain several methods of how to extract a subvector from a C++ vector.

Use {} List Initialization Notation to Extract a Subvector From a Vector

One way of extracting a subvector is to initialize a new vector with the original vector elements. The following method specifies elements with iterators pointing to desired locations (the first 5 elements from int_vec). Notice that, in this example, we use the std::copy method to output vector elements to the console.

#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;

int main() {
    vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
                            649, 1092, 110, 129, 40, 3024};

    vector<int> sub_vec {int_vec.begin(), int_vec.begin()+5};

    cout << std::left << setw(10) << "vec: ";
    copy(int_vec.begin(), int_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    cout << std::left << setw(10) << "subvec: ";
    copy(sub_vec.begin(), sub_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

vec:      1; 23; 43; 324; 10; 222; 424; 649; 1092; 110; 129; 40; 3024;
subvec:   1; 23; 43; 324; 10;

Alternatively, you can initialize a subvector variable by specifying the pointers to the desired element (e.g &int_vec[index]) of the original vector:

#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;

int main() {
    vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
                            649, 1092, 110, 129, 40, 3024};

    vector<int> sub_vec {&int_vec[0], &int_vec[5]};

    cout << std::left << setw(10) << "vec: ";
    copy(int_vec.begin(), int_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    cout << std::left << setw(10) << "subvec: ";
    copy(sub_vec.begin(), sub_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

vec:      1; 23; 43; 324; 10; 222; 424; 649; 1092; 110; 129; 40; 3024;
subvec:   1; 23; 43; 324; 10;

Use the copy() Function to Extract a Subvector From a Vector

The copy() function is a powerful tool to manipulate the range-based data. In this case, we will utilize it to literary copy specific element from one vector to another (meant as a subvector). Firstly we declare a sub_vec variable with some elements to be copied. Next, we pass the original vector range as arguments to the copy() function and the begin iterator to the destination vector.

#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;

constexpr int NUM_ELEMS_TO_COPY = 6;

int main()
{
    vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
                            649, 1092, 110, 129, 40, 3024};
    vector<int> sub_vec (NUM_ELEMS_TO_COPY);

    copy(&int_vec[0], &int_vec[NUM_ELEMS_TO_COPY], sub_vec.begin());

    cout << std::left << setw(10) << "subvec: ";
    copy(sub_vec.begin(), sub_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}
subvec:   1; 23; 43; 324; 10; 222;

Another powerful technique using the copy() method is to append a subvector to any existing vector variable. The following sample code demonstrates this by pushing 5 extracted elements back to sub_vec variable using std::back_inserter function template. Note that, copy method operates on element range as [first, last).

#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::setw;

int main()
{
    vector<int> int_vec {1, 23, 43, 324, 10, 222, 424,
                            649, 1092, 110, 129, 40, 3024};
    vector<int> sub_vec = { 1, 2, 3, 4, 5, 6 };

    copy(&int_vec[0], &int_vec[5], std::back_inserter(sub_vec));

    cout << std::left << setw(10) << "subvec: ";
    copy(sub_vec.begin(), sub_vec.end(),
         std::ostream_iterator<int>(cout,"; "));
    cout << endl;

    return EXIT_SUCCESS;
}
subvec:   1; 2; 3; 4; 5; 6; 1; 23; 43; 324; 10;

Related Article - C++ Vector

  • How to Initialize a Vector in C++
  • How to Return a Vector From a Function in C++