which STL container do I need for storing a 2D-array?

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Hi,

I'm reading a number of double values from a file. It's a 2D-array:

1 2 3 4 5 6 7
-------------
1 3.2
2 0 2.1
3 9.3
4
5 4.5 etc.etc.
6


I'm looking in my book and can't figure out what is the best to use. I
don't think I need a random access iterator. So I can choose among:
vector, list, deque, set, multiset, map and multimap...

After I have read in all the numbers, I want to find those that are in
the upper 10 percentile group, I think it's called (those 10% of the
numbers that are highest).

So then I would probably need something like:

iter = find(array.begin(), array.end(), (10%-highest-number))

hmm. Then again, no, that seems not right... I have a couple of examples
for storing 1D-arrays in my book. I don't think I have any for storing
2D-arrays and am a bit confused.

Can anyone push me in the right direction?


Best regards
Martin Jørgensen
 
M

Martin Steen

Martin said:
>
hmm. Then again, no, that seems not right... I have a couple of examples
for storing 1D-arrays in my book. I don't think I have any for storing
2D-arrays and am a bit confused.

Can anyone push me in the right direction?

You can create an array with 10 rows and 10 columns with
the declaration:

vector<vector<float> > a(10,10);

To find the top 10% of the array, it's better to put it all into a
one-dimensional vector and then sort() that vector.

Best regards,
-Martin
 
T

Tom Smith

Martin said:
Hi,

I'm reading a number of double values from a file. It's a 2D-array:

I'm looking in my book and can't figure out what is the best to use.

std::vector<std::vector <float> > will do the job straightforwardly. (You'll
even be able to use [][] double-array syntax). If the array is likely to be very
big and sparse (ie, mostly zeros), you might want to consider using a
with the x and y indices as the key. That would be more work said:
After I have read in all the numbers, I want to find those that are in
the upper 10 percentile group, I think it's called (those 10% of the
numbers that are highest).

If you wanted to use standard algorithms, then this would probably be easier
with the std::map method above: it would be much more fiddly with a vector of
vectors.

hth,

Tom
 
J

Jacek Dziedzic

Martin said:
Hi,

I'm reading a number of double values from a file. It's a 2D-array:

1 2 3 4 5 6 7
-------------
1 3.2
2 0 2.1
3 9.3
4
5 4.5 etc.etc.
6


I'm looking in my book and can't figure out what is the best to use. I
don't think I need a random access iterator. So I can choose among:
vector, list, deque, set, multiset, map and multimap...

After I have read in all the numbers, I want to find those that are in
the upper 10 percentile group, I think it's called (those 10% of the
numbers that are highest).

So then I would probably need something like:

iter = find(array.begin(), array.end(), (10%-highest-number))

hmm. Then again, no, that seems not right... I have a couple of examples
for storing 1D-arrays in my book. I don't think I have any for storing
2D-arrays and am a bit confused.

Can anyone push me in the right direction?

I'd emulate the 2D-array using a 1D-array, ie. a std::vector.
Say, instead of having a 6x7 2D-array you may as well use a
42-element 1D-array. All you'd need would be a way to convert
row & column to index and the other way back using two
simple functions that I'll let you figure out yourself.

HTH,
- J.
 
S

Squeamizh

Martin said:
I'm reading a number of double values from a file. It's a 2D-array:

Do you actually need the numbers to be in a 2d-array after you've read
them, or is that just the format of the file? Based only on the
information you provided in your post, putting them all in a single
vector will make your life easier.
After I have read in all the numbers, I want to find those that are in
the upper 10 percentile group, I think it's called (those 10% of the
numbers that are highest).

If you just need to separate the top 10% in no particular order, then
you want std::nth_element: http://www.sgi.com/tech/stl/nth_element.html

If you want the top 10% to be ordered, then use std::partial_sort:
http://www.sgi.com/tech/stl/partial_sort.html
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Tom said:
std::vector<std::vector <float> > will do the job straightforwardly.

Ok, there were some pretty good suggestions in the thread untill now...
I have no experience with boost so it won't be that solution... Also the
suggestions about putting all numbers in a single vector is great...

But I think I'll pick this solution with std::vector said:
>, because that would be something completely new which I haven't tried
before... And I could probably learn something from that...
(You'll even be able to use [][] double-array syntax). If the array is

That's great...
likely to be very big and sparse (ie, mostly zeros), you might want to
consider using a std::map<float>, with the x and y indices as the key.

Also great idea... I haven't really much experience with this, but I
think I have a few examples about std::map in my book.
That would be more work, but could save (potentially, and only in this
particular situation) lots of memory.

Agreed... But it isn't that important here.
If you wanted to use standard algorithms, then this would probably be
easier with the std::map method above: it would be much more fiddly with
a vector of vectors.

Not more difficult than we can handle it here, is it? :)

But I still need a push in the right direction, I think, since this is
my first time with such a method :)


I have: std::vector<std::vector <float> > twoDimens;

And...
---
for(unsigned int column=1; column <= number_of_columns; column++)
{
linenumber++;
infile >> read_value;

twoDimens.push_back(linenumber);
}
---

But that doesn't work... Ofcourse since I just made it look like a 1D
array, but on the other hand I'm too unexperienced to figure out what to
do...

And do you have an example using [][] double-array syntax?


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Martin said:
You can create an array with 10 rows and 10 columns with
the declaration:

vector<vector<float> > a(10,10);

Is it necessary to tell the size from the beginning? I thought vectors
could grow... Actually it isn't a problem, because I know the columns
and rows before I start so should I use it or should I just let it grow?

My code doesn't work:


I have: std::vector<std::vector <float> > twoDimens(10,10);

And...
---
for(unsigned int column=1; column <= number_of_columns; column++)
{
linenumber++;
infile >> read_value;

twoDimens.push_back(linenumber); // << *Error this line*
}
---

Gives:

In function 'void convert_data(unsigned int&, unsigned int&, unsigned
int&, std::ifstream&, std::string&, unsigned int)':
output_to_latex.cpp:149: error: no matching function for call to
>::push_back(unsigned int&)'
/usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void
std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
std::vector<float, std::allocator<float> >, _Alloc =
std::allocator<std::vector<float, std::allocator<float> > >]
make: *** [output_to_latex] Error 1

To find the top 10% of the array, it's better to put it all into a
one-dimensional vector and then sort() that vector.

I don't want to sort it, because the data has to be written out in exact
the same order. Perhaps I should copy it then? Figure out the
top_ten_percent_value and delete the copy?

------

for(i=1; i<rows; i++)
{
for(j=1; j<columns; j++)
{
if( twodimens[j] > top_ten_percent_value)
// something happens
else
outfile << twodimens[j];
}
}

----

Immediately before I write out the data (to another file) I need to know
if this particular (current) number is in the top 10%. I'm mostly used
to C-programming.... As the above probably shows...



Best regards
Martin Jørgensen
 
J

Jim Langston

Martin Jørgensen said:
Hi,

I'm reading a number of double values from a file. It's a 2D-array:

1 2 3 4 5 6 7
-------------
1 3.2
2 0 2.1
3 9.3
4
5 4.5 etc.etc.
6


I'm looking in my book and can't figure out what is the best to use. I
don't think I need a random access iterator. So I can choose among:
vector, list, deque, set, multiset, map and multimap...

After I have read in all the numbers, I want to find those that are in the
upper 10 percentile group, I think it's called (those 10% of the numbers
that are highest).

So then I would probably need something like:

iter = find(array.begin(), array.end(), (10%-highest-number))

hmm. Then again, no, that seems not right... I have a couple of examples
for storing 1D-arrays in my book. I don't think I have any for storing
2D-arrays and am a bit confused.

Can anyone push me in the right direction?


Best regards
Martin Jørgensen

A common way to do a two dimentional array is to have a vector of vector
like:
std::vector<std::vector<int> > My2DArray;
but I find it a headache having to push vectors into the array. The way I
deal with it then is just by encapsulating the 2nd vector.

class VectorArray
{
public:
std::vector< int > Data;
};

Then I can say:
std::vector< VectorArray > My2DArray;

This gains me that I don't have to push a vector onto the array, just the
class, but I loose out in not being able to use [][] anymore. Of course I
could simply override [] in my VectorArray and get that ability back. It
all depends on what you are comfortable with I think and what you are
actually doing with the 2d array.

The way to do it with vector<vector...

std::vector<std::vector<int> > My2DArray;
repeat until all data is read
{
std::vector<int> data;
// read a line of data into this variable.
My2DArray.push_back( data );
}

At this point My2DArray would contain as many vector<int>s as there were
lines of data.
 
M

Martin Steen

Martin said:
Is it necessary to tell the size from the beginning?

No. You can write

vector<vector<float> > a;

this creates an empty vector.

I thought vectors
could grow... Actually it isn't a problem, because I know the columns
and rows before I start so should I use it or should I just let it grow?

My code doesn't work:


I have: std::vector<std::vector <float> > twoDimens(10,10);

And...
---
for(unsigned int column=1; column <= number_of_columns; column++)
{
linenumber++;
infile >> read_value;

twoDimens.push_back(linenumber); // << *Error this line*
}
---

This can't work, because "linenumber" can't be a member of twoDimens.
Only a vector<float> can be a member of twoDimens.

This should work:

for(int i = 0; i < rows; i++)
{
vector<float> rowVector;

for (int j = 0; j < columns; j++)
{
infile >> read_value;
rowVector.push_back(read_value);
}

twoDimens.push_back(rowVector);
}

I don't want to sort it, because the data has to be written out in exact
the same order. Perhaps I should copy it then? Figure out the
top_ten_percent_value and delete the copy?

Sounds good. To get the top 10%, you have to use a sort-function
somehow.
Immediately before I write out the data (to another file) I need to know
if this particular (current) number is in the top 10%. I'm mostly used
to C-programming.... As the above probably shows...

I tell you how I would do it:

- create a class which contains a float and a bool (class CData)
- Read the float-data, set bools to false.
- create a std::vector<CData*> array of pointers to the data
- sort() the array of pointers by value.
- Set bool-value of the top 10% of the array on "true". Use
the sorted pointer array for this.

Now, when you write the data, the bool-value tells you if
the float-value value is in the top 10%. After that, you can
delete the array of pointers.

Sorting an array of pointers is the most effective way to sort
large data structures, because no data has to be copied, but
only pointers.

Best regards,
-Martin
 
B

BobR

Martin Jørgensen wrote in message
I have: std::vector<std::vector <float> > twoDimens;

And...
// > twoDimens.push_back(linenumber);

twoDimens.at( column ).push_back(linenumber);
// but I don't think that's what you want. Probably more like:
twoDimens.at( row ).push_back(linenumber);

I just posted a snippet underneath:

-----Original Message-----
Newsgroups: comp.lang.c++
Date: Monday, October 23, 2006 3:26 AM
Subject: Re: 2D Array

Take a look at that, then come back with questions, OK?
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Jim said:
-snip-

A common way to do a two dimentional array is to have a vector of vector
like:
std::vector<std::vector<int> > My2DArray;
but I find it a headache having to push vectors into the array. The way I
deal with it then is just by encapsulating the 2nd vector.

class VectorArray
{
public:
std::vector< int > Data;
};

Then I can say:
std::vector< VectorArray > My2DArray;

Ok. But you don't access it with:

My2DArray.Data.push_back(value)


This gains me that I don't have to push a vector onto the array, just the
class, but I loose out in not being able to use [][] anymore. Of course I
could simply override [] in my VectorArray and get that ability back. It
all depends on what you are comfortable with I think and what you are
actually doing with the 2d array.

I'm not comfortable with 2D arrays in C++ at all :)

But in C I'm comfortable with [][].
The way to do it with vector<vector...

std::vector<std::vector<int> > My2DArray;
repeat until all data is read
{
std::vector<int> data;
// read a line of data into this variable.
My2DArray.push_back( data );
}

At this point My2DArray would contain as many vector<int>s as there were
lines of data.

Yes, of course... I see...

BTW: I'm using:

row_values.erase( row_values.begin(), row_values.end() );

For deleting the 1D vector... Is that the preferred method, for making
room for the next data line?


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Martin said:
Martin Jørgensen schrieb: -snip-


This can't work, because "linenumber" can't be a member of twoDimens.
Only a vector<float> can be a member of twoDimens.

Damn.... I didn't think about what I was doing... Linenumber was an
integer. Thanks.
This should work:

for(int i = 0; i < rows; i++)
{
vector<float> rowVector;

for (int j = 0; j < columns; j++)
{
infile >> read_value;
rowVector.push_back(read_value);
}

twoDimens.push_back(rowVector);
}

Great, thanks... It seem to work now. Unfortunately my number of rows =
number of columns in my data-file, so I'm not completely sure whether or
not I swapped some indices wrong? Can somebody verify that the following
is correct and what I expect?:


----------

std::vector<std::vector <double> > twoDimens;
std::vector<double> row_values;

for(unsigned int line=1; line<=number_of_lines; line++)
{


for(unsigned int column=1; column <= number_of_columns; column++)
{
infile >> read_value;
row_values.push_back(read_value); /* 1 row of data */
}

twoDimens.push_back(row_values); /* save all row_values */

/* erase row_values */
row_values.erase( row_values.begin(), row_values.end() );
}


/* testing - is the following okay? /*

cout << "Printing 2D array:\n";

for( unsigned int i = 0; i < twoDimens.size(); i++ )
{
for( unsigned int j = 0; j < twoDimens.size(); j++ )
{
cout << twoDimens[j] << ", ";

sorted_Array.push_back(twoDimens[j]); /* 1D-array */

if( j == twoDimens.size() - 1 )
cout << endl; /* new row = new line */
}
}

cout << "Press enter" << endl;
system("read");
exit(1);

----------

Sounds good. To get the top 10%, you have to use a sort-function
somehow.

Hmmm... Okay... I first tried making a copy of the 2D-array:

std::vector<std::vector <double> > sorted_Array(twoDimens);


Then I figured out that there probably doesn't exist such a sort command
that can work in 2 dimensions so here I used the "easy" solution: Copied
everything to a 1D-array and sorted that with:

sort(sorted_Array.begin(), sorted_Array.end() );

I tell you how I would do it:

- create a class which contains a float and a bool (class CData)
- Read the float-data, set bools to false.
- create a std::vector<CData*> array of pointers to the data

That's a good idea...
- sort() the array of pointers by value.
Yep.

- Set bool-value of the top 10% of the array on "true". Use
the sorted pointer array for this.

I think I almost can do that (sort the pointer array)... At least in C...
Now, when you write the data, the bool-value tells you if
the float-value value is in the top 10%. After that, you can
delete the array of pointers.

Delete, like erase in:

pointer_array.erase( pointer_array.begin(), pointer_array.end() );

???
Sorting an array of pointers is the most effective way to sort
large data structures, because no data has to be copied, but
only pointers.

I agree... It's a good method... It requires some dereferencing... And
debugging, last time I tried to do something like that (in C)...


Best regards
Martin Jørgensen
 
J

Jim Langston

Martin Jørgensen said:
Ok. But you don't access it with:

My2DArray.Data.push_back(value)

My2DArray[n].Data.push_back( value );
This gains me that I don't have to push a vector onto the array, just the
class, but I loose out in not being able to use [][] anymore. Of course
I could simply override [] in my VectorArray and get that ability back.
It all depends on what you are comfortable with I think and what you are
actually doing with the 2d array.

I'm not comfortable with 2D arrays in C++ at all :)

But in C I'm comfortable with [][].
The way to do it with vector<vector...

std::vector<std::vector<int> > My2DArray;
repeat until all data is read
{
std::vector<int> data;
// read a line of data into this variable.
My2DArray.push_back( data );
}

At this point My2DArray would contain as many vector<int>s as there were
lines of data.

Yes, of course... I see...

BTW: I'm using:

row_values.erase( row_values.begin(), row_values.end() );

For deleting the 1D vector... Is that the preferred method, for making
room for the next data line?

Thats why I had the std::vector<int> data; insside the brackets. Meaning
each line it gets recreated so you don't have to clear it.

More pseudo code:

std::vector<std::vector<int> > My2DArray;
ifstream MyFile("SomeFile.txt");
if ( MyFile.is_open() )
{
std::string Line;
while ( getline( MyFile, Line ) )
{
std::vector< int > data;
std::stringstream MyStream;
MyStream << Line;
// Maybe std::stringstream MyStream( Line ); ???
int Value;
while ( MyStream >> Value )
data.push_back( Value );
My2DArray.push_back( data );
}
}

Since data is inside the while block, it's scope is local to that block, so
it gets recreated every cycle, no need to clear it.

}
 
B

BobR

Martin Jørgensen wrote in message
----------

std::vector<std::vector <double> > twoDimens;
std::vector<double> row_values;

for(unsigned int line=1; line<=number_of_lines; line++){

for(unsigned int column=1; column <= number_of_columns; column++){
infile >> read_value;
row_values.push_back(read_value); /* 1 row of data */
}

twoDimens.push_back(row_values); /* save all row_values */

/* erase row_values */
// row_values.erase( row_values.begin(), row_values.end() );

// There is a handy little function in vector to do that:

row_values.clear();

// Prove it for yourself:
cout << row_values.size() << std::endl; // S/B '0'
cout << row_values.capacity() << std::endl; // clear() doesn't resize
}


/* testing - is the following okay? /*

cout << "Printing 2D array:\n";

for( unsigned int i = 0; i < twoDimens.size(); i++ ){
for( unsigned int j = 0; j < twoDimens.size(); j++ ){
cout << twoDimens[j] << ", ";

sorted_Array.push_back( twoDimens[j] ); /* 1D-array */

// if( j == twoDimens.size() - 1 )
// cout << endl; /* new row = new line */
} // for( j )


// You could just put the 'endl' here ( no if() ):
cout << endl; /* new row = new line */
} // for ( i )

cout << "Press enter" << endl;
system("read");
// exit(1);

Generally NOT a good idea to use 'exit()' without good reason. If this was in
a function called from 'main()', just let it return to 'main()'. If it's in
'main()', use:

return 0;
// or
// return EXIT_SUCCESS;
// or
// return EXIT_FAILURE; // == exit(1); (sort of)
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Jim said:
-snip-
Ok. But you don't access it with:

My2DArray.Data.push_back(value)


My2DArray[n].Data.push_back( value );

Oh, ofcourse... I also thought something was missing.

-snip-
Thats why I had the std::vector<int> data; insside the brackets. Meaning
each line it gets recreated so you don't have to clear it.

Oh, I get it...
More pseudo code:

std::vector<std::vector<int> > My2DArray;
ifstream MyFile("SomeFile.txt");
if ( MyFile.is_open() )
{
std::string Line;
while ( getline( MyFile, Line ) )
{
std::vector< int > data;
std::stringstream MyStream;
MyStream << Line;
// Maybe std::stringstream MyStream( Line ); ???
int Value;
while ( MyStream >> Value )
data.push_back( Value );
My2DArray.push_back( data );
}
}

Yep, agreed. With small modifications here and there.
Since data is inside the while block, it's scope is local to that block, so
it gets recreated every cycle, no need to clear it.

}

Nice... Thanks.


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

BobR said:
Martin Jørgensen wrote in message


// > twoDimens.push_back(linenumber);

twoDimens.at( column ).push_back(linenumber);
// but I don't think that's what you want. Probably more like:
twoDimens.at( row ).push_back(linenumber);

Thanks... This "at" seems to help. I get the idea...
I just posted a snippet underneath:

-----Original Message-----
Newsgroups: comp.lang.c++
Date: Monday, October 23, 2006 3:26 AM
Subject: Re: 2D Array

Take a look at that, then come back with questions, OK?

I saw the code and ran it.... It's really great and also pretty
relevant. Thanks for the reference...

Actually I don't have any more questions right now, but I'll probably
return when more problems arises...


Best regards
Martin Jørgensen
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

BobR said:
Martin Jørgensen wrote in message



// There is a handy little function in vector to do that:

row_values.clear();

Thanks. That's better.
// Prove it for yourself:
cout << row_values.size() << std::endl; // S/B '0'
cout << row_values.capacity() << std::endl; // clear() doesn't resize

It works... Nice.
}


/* testing - is the following okay? /*

cout << "Printing 2D array:\n";

for( unsigned int i = 0; i < twoDimens.size(); i++ ){
for( unsigned int j = 0; j < twoDimens.size(); j++ ){
cout << twoDimens[j] << ", ";

sorted_Array.push_back( twoDimens[j] ); /* 1D-array */

// if( j == twoDimens.size() - 1 )
// cout << endl; /* new row = new line */
} // for( j )



// You could just put the 'endl' here ( no if() ):
cout << endl; /* new row = new line */


Oh, yes... That's better.
Generally NOT a good idea to use 'exit()' without good reason. If this was in
a function called from 'main()', just let it return to 'main()'. If it's in
'main()', use:

return 0;
// or
// return EXIT_SUCCESS;
// or
// return EXIT_FAILURE; // == exit(1); (sort of)

Yep, agreed... It was also just temporarily... I just wanted to make
sure that the data array and everything was okay before returning to
main()...

But I think I'm done now... The program works :)

If anyone has other suggestions, I'll still read them and else thanks
again to those that contributed with suggestions...


Best regards
Martin Jørgensen
 
M

Martin Steen

Martin said:
Thanks. That's better.

Even better (IMHO):
If you declare row_values in the scope of the outer loop,
there is no need to clear the vector:


for(int i = 0; i < rows; i++)
{
vector<float> rowVector; // put declaration into the for-loop
for (int j = 0; j < columns; j++)
{
infile >> read_value;
rowVector.push_back(read_value);
}
twoDimens.push_back(rowVector);

// outer for-loop ends here - rowVector becomes invalid
// and gets deleted automatically!
}

Best regards,
-Martin
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Martin said:
Even better (IMHO):
If you declare row_values in the scope of the outer loop,
there is no need to clear the vector:

I thought it should be completely outside the function... But probably not.
for(int i = 0; i < rows; i++)
{
vector<float> rowVector; // put declaration into the for-loop
for (int j = 0; j < columns; j++)
{
infile >> read_value;
rowVector.push_back(read_value);
}
twoDimens.push_back(rowVector);

// outer for-loop ends here - rowVector becomes invalid
// and gets deleted automatically!
}

There's a small difference compared to programming in C, isn't there?

I mean: In C I think the following is possible:

void func()
{
int abc, bla, blabla;

blabla = 5;
abc = 0;

for( bla=0; bla< blabla; bla++)
{
printf("Hi");
abc++; /* abc is in scope in C, I think - am I right? */
/* abc is not in scope in C++, or is it??? */
}
}

So I think abc isn't in scope in C++... But I'm not sure... I've
debugged a little in C++... Is it right or wrong?


Best regards
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top