Vector of vectors?

Looking at a value within a vector of vectors?

Page 1 of 1

4 Replies - 59175 Views - Last Post: 16 November 2007 - 12:54 PM Rate Topic: -----

#1 ShinX  Icon User is offline

  • New D.I.C Head

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

Vector of vectors?

Post icon  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.... :blink:
Is This A Good Question/Topic? 0
  • +

Replies To: Vector of vectors?

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 ShinX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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:D lengend...... :D: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

Was This Post Helpful? 0
  • +
  • -

#4 ShinX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)){
		votes.push_back(vector<int>(myint));
	}
	}

}




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

int main()
{
	vector<vector<int> > votes;
	input(votes);


	//cout << "Total Votes: " << votes.size();


	for(int i = 0; i< votes.size(); i++){
	vector<int> myRow=votes[i];
	int myValue=myRow[i];	   
	//int myValue=votes.at(i)[j];	
	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

Was This Post Helpful? 0
  • +
  • -

#5 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1