Hello,

Suppose I have a stl vector of ints, and I want to pass a sub-range of that vector as an argument to a function. One way of doing that would be to copy the sub-range, and then pass that copy by reference, as follows:

Code:
#include <vector>

using namespace std;

int MyFunction(vector<int> &a_vector)
{
    // Do something

    return 0;
};

int main()
{
    // Create a vector and assign its elements;

    vector<int> my_vector(4);
    my_vector[0] = 10;
    my_vector[1] = 20;
    my_vector[2] = 50;
    my_vector[3] = 100;

    // Create a sub-vector and assign its elements

    int sub_vector_length = 2;
    vector<int> sub_vector(sub_vector_length);
    for (int i = 0; i < sub_vector_length; i++)
    {
        sub_vector[i] = my_vector[i];
    }

    // Pass the sub-vector by reference to MyFunction
    MyFunction(sub_vector);

    return 0;
}
However, it can be time-consuming to copy the elements between the two vectors if my_vector is large. So I'm wondering if it is possible to directly pass a sub-range of my_vector by reference, without having to create a new vector and manually copy over all of the relevant elements?

Thanks!

Ed.