Passing a sub-range of a vector by reference
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Passing a sub-range of a vector by reference

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    98

    Passing a sub-range of a vector by reference

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Passing a sub-range of a vector by reference

    Quote Originally Posted by ejohns85 View Post
    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?
    Just pass a pointer to the first item in the vector you want to use in the function. A vector is a wrapper for a contiguous, dynamic array of T, so there is no need to be copying anything.
    Code:
    int MyFunction(int *a_vector)
    {
        // Do something
    
        return 0;
    };
    //...
    // Pass the sub-vector by reference to MyFunction
    MyFunction(&my_vector[2]);
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,568

    Re: Passing a sub-range of a vector by reference

    If you can change the function, you can design it similar to the way the algorithms
    work: pass a start iterator and an end iterator (one past last element).

  4. #4
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Passing a sub-range of a vector by reference

    Quote Originally Posted by ejohns85 View Post
    However, it can be time-consuming to copy the elements between the two vectors if my_vector is large.
    Also, there is really no need to write the loop yourself.
    Code:
    int sub_vector_length = 2;
    vector<int> sub_vector(myvector.begin(), myvector.begin() + sub_vector_length);
    If the compiler's optimizer is a good one, and the compiler recognizes that "int" is a POD, then the copying of a vector of POD type to another could be optimized by issuing a memcpy(). So the copying of even large areas of memory for POD types becomes almost a moot point, as memcpy() is usually optimized to a few assembly language calls.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 18th, 2012 at 04:05 PM.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Passing a sub-range of a vector by reference

    Quote Originally Posted by ejohns85 View Post
    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?
    You could pass a start-of-range iterator and a range length to the function. The function can then use the iterator for either sequential or random access to the elements within the range.

    More ambitious would be to introduce a class called say Subrange which is used to access a vector subrange while hiding the details. A Subrange object would then be passed to the function. Clean, simple, safe and efficient (at least potentially ).
    Last edited by nuzzle; December 19th, 2012 at 06:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center