-
December 18th, 2012, 04:49 PM
#1
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.
-
December 18th, 2012, 04:55 PM
#2
Re: Passing a sub-range of a vector by reference
Originally Posted by ejohns85
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
-
December 18th, 2012, 04:57 PM
#3
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).
-
December 18th, 2012, 05:02 PM
#4
Re: Passing a sub-range of a vector by reference
Originally Posted by ejohns85
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 05:05 PM.
-
December 19th, 2012, 03:24 AM
#5
Re: Passing a sub-range of a vector by reference
Originally Posted by ejohns85
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 07: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|