How to store values in two dimensional vector

I need to read data from a matrix and store it in two dimensional vector.
Say the matrix file is
4
0 1 1 0
1 0 0 0
1 3 1 1
1 0 0 1
The first number 4 indicates the size of the matrix, say 4X4 matrix.
I have to read this data from the file and store it in a two dimensional vector.
I initialized the vector as vector<vector<int> > myVector;
I tried storing like this
for (rows = 0; rows < size; rows++) {
for (columns = 0; columns < size; columns++) {
inputFile >> value;
myVector[rows][columns] = value; // not working
cout<<rows<<" "<<columns<<" "<<myVector[rows][columns]<<endl; // not working
}
}

Also I need help in how should I access the elements in the vector after inserting.
Please let me know how to do this.
BeebutterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi Beebutter,

in a 'vector' you can't access elements by index with []-operator which don't already exist. I don't know of any array-implementation which does this.

I attached sample code which demonstrates how to use vector as I think you need.

Hope that helps,

ZOPPO
int n = 4;
 
vector < vector < int > > v;
 
v.resize( n ); // set row size to 'n'
 
for ( int i = 0; i < n; i++ )
{
	v[i].resize( n ); // set col size of row 'i' to 'n'
 
	for ( int j = 0; j < n; j++ )
	{
		v[i][j] = i * j; // put a value at row 'i', col 'j'
	}
}
 
// output the matrix
cout << "Rows: " << v.size() << endl;
 
for ( vector < vector < int > >::iterator itRow = v.begin(); itRow != v.end(); itRow++ )
{
	for ( vector < int >::iterator itCol = itRow->begin(); itCol != itRow->end(); itCol++ )
	{
		cout << *itCol << " ";
	}
 
	cout << endl;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.