# Vector of vectors?

Page 1 of 1

## 4 Replies - 92815 Views - Last Post: 16 November 2007 - 12:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=37527&amp;s=46ddc675a2cac653e961356daf23877c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ShinX

Reputation: 0
• Posts: 22
• Joined: 14-November 07

# Vector of vectors?

Posted 15 November 2007 - 02:00 PM

heh i am asking a lot of questions today XD love the help thanks guys

Was just wondering how would I go about looking at a value
within a vector.... which is inside another vector?

would it be vector[vector[i]];???

completely confused....
Is This A Good Question/Topic? 0

## Replies To: Vector of vectors?

### #2 jjhaag

• me editor am smartastic

Reputation: 48
• Posts: 1,789
• Joined: 18-September 07

## Re: Vector of vectors?

Posted 15 November 2007 - 02:04 PM

Nope. Using the subscript operator, it's just like accessing a multidimensionaly array, so you'll need two indices, such as i and j. Then, to access, you would write myVector[i][j].

There is a pair of vector tutorials that I wrote in the C++ tutorials section you may want to take a look at. The second one has a short section with some example code on using multi-dimensional (i.e. nested) vectors, and it could probably be of some help to you.

-jjh

*edit - decided this might call for a little more.

When you access a vector using myVector[i], it returns a reference to the element found at position i within the vector (the exact same thing can be done using myVector.at(i), except the at method is access-safe). It doesn't really matter what is found at position i, a reference is still returned.

So when you have a nested/multidimensional vector, myVector[i] will return a reference to a vector. So just as you accessed a non-nested vector, you can now access element j within the vector that was returned using [j].
```vector<vector<double> > myVector(4,vector<double>(4,0.0)); //creates 4x4 nested vector
vector<double> myRow=myVector[1];	   //returns a reference to myVector[1], and assigns this vector to myRow
double myValue=myRow[3];	   //returns a reference to myRow[1], and assigns it to myValue

```

The last two lines could be accomplished by writing:
```double myValue=myVector[1][3];
```

This post has been edited by jjhaag: 15 November 2007 - 02:14 PM

### #3 ShinX

Reputation: 0
• Posts: 22
• Joined: 14-November 07

## Re: Vector of vectors?

Posted 15 November 2007 - 02:40 PM

Thanks dude, the tutorial is really already coming in handy :D lengend...... :D:D

Edit: Legend just read your edit zomg that was so interesting... i will keep in touch and let you know what my end product of what I produce looks like ....

This post has been edited by ShinX: 15 November 2007 - 02:44 PM

### #4 ShinX

Reputation: 0
• Posts: 22
• Joined: 14-November 07

## Re: Vector of vectors?

Posted 16 November 2007 - 08:25 AM

ok I have this input storing into... vectors within a vector:

```void input(vector<vector<int> > &votes)
{
string next;
int myint;
while(getline (cin,next))
{
stringstream(next) >> myint;
if(!isspace(myint)){
}
}

}

```

This is how i am trying to out put the values within the vectors:

```int main()
{

for(int i = 0; i< votes.size(); i++){
int myValue=myRow[i];
cout << myValue;
}

```

If input is:

1 2 3 4
2 3 1 4
2 3 1 4

This the output produced is:

000

So we have a problem the right output is not being produced, I cannot tell wether its the input going wrong or my output being produced wrong.

Any clues?

This post has been edited by ShinX: 16 November 2007 - 08:26 AM

### #5 jjhaag

• me editor am smartastic

Reputation: 48
• Posts: 1,789
• Joined: 18-September 07

## Re: Vector of vectors?

Posted 16 November 2007 - 12:54 PM

Well, your input step is a little weird. How are you terminating the input? And every time the user enters a number, you're push_back'ing an entire vector with the number of elements that the user typed in.

To assign to a multidimensional vector, you'll need to be using a nested loop
```vector<vector<int> > myVector;
int num;
for (int i=0; i<4; ++i) {//controls number of rows to be inputted
myVector.push_back(vector<double>);//adds an empty row to the end of the vector
for (int i=0; i<4; ++i) {//controls number of columns to be inputted
cin >> num;
myVector.push_back(num);//adds element to the current row
}
}
```

And when you output from a vector, you need to output element by element - and assigning an entire row from your array to an integer is going to cause you problems. The output is basically the same as when using a multidimensional array, except using the vector access method:
```	for (int i = 0; i< votes.size(); i++) {
for (int j = 0; j< votes.size(); j++) {
cout << myVector.at(i).at(j) << " ";
}
cout << endl;
}
```

Hope that helps,

-jjh