[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to store values in two dimensional vector

Posted on 2008-11-06
1
Medium Priority
?
1,398 Views
Last Modified: 2009-07-02
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.
0
Comment
Question by:Beebutter
1 Comment
 
LVL 31

Accepted Solution

by:
Zoppo earned 1500 total points
ID: 22895921
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question