954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Vector from vector references

I have several vectors of int elements.
Now I want to create a vector of vectors, but I don't want to copy the vectors to the new one. If possible, I'd like to pass them by reference.
Please consider:

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
    vector<int> v1; 
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);

    vector<int> v2; 
    v2.push_back(4);
    v2.push_back(5);
    v2.push_back(6);

    vector<int> v3; 
    v3.push_back(7);
    v3.push_back(8);
    v3.push_back(9);

    vector<int>& v4 = v1.front();
    vector<int>& v5 = v2.front();
    vector<int>& v6 = v3.front();

    vector<vector<int> >vall;
    vall.push_back(v4);
    vall.push_back(v5);
    vall.push_back(v6);

    for (vector<int>::size_type i = 0; i != v1.size(); ++i) {
        cout << vall[0][i] << vall[1][i] << vall[2][i] << endl;
    }   
    return 0;
}


This attempt results in error.
Is there a way to do this?

luislupe
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I would be careful about doing this. The std::vector container stores its contents in a contiguous portion of memory. If you resize the vector at any point, either directly by calling std::vector::resize or indirectly, by calling std::vector::push_back and the space available in the currently allocated memory for the vector isn't big enough then the whole contents of the vector might be moved to a different portion of memory. This will leave your vector of references still cointaining the old memory addresses of the elements of the vector, which is almost certainly going to result in areally nasty run-time bug! If you want to do this safely, you'll probably have to manage the memory yourself in some kind of clever way that stops this from happening. If the order of the elements isn't important to you, I think that you might be able use std::set instead.

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

The order is of the elements is important.
Those vectors that are built are not going to be modified in any way.
If possible, I just could pass a const reference to each of them (as I would as parameters of a function).

luislupe
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I guess that:

vector<int>& v4 = v1;
vector<int>& v5 = v2;
vector<int>& v6 = v3;

does the trick. It's simpler than I thought. Or am I wrong?

luislupe
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

That will do what (I think) you want, and is also safe, since these are references to the actual vector, not the data in the vector, the vector object reference will always be valid (as long as the original vector is in scope).

ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: