473,237 Members | 1,086 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

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

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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 23 '06 #1
20 4578
Martin Jørgensen wrote:
Can anyone push me in the right direction?
You may want to look at boost multi_array
Oct 23 '06 #2
Martin Jørgensen wrote:
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
std::map<float>, with the x and y indices as the key. That would be more work,
but could save (potentially, and only in this particular situation) lots of memory.
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
Oct 23 '06 #3
Martin Jørgensen wrote:
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.
Oct 23 '06 #4

Martin Jørgensen wrote:
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

Oct 23 '06 #5
Tom Smith wrote:
Martin Jørgensen wrote:
>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.
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<std::vector <float>
>, 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.
>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.
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 23 '06 #6
Martin Jørgensen wrote:
>
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





>

Best regards
Martin Jørgensen
Oct 23 '06 #7
Martin Steen wrote:
Martin Jørgensen wrote:
>
>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);
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
'std::vector<std::vector<float, std::allocator<float,
std::allocator<std::vector<float, std::allocator<float >
>::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[i][j] top_ten_percent_value)
// something happens
else
outfile << twodimens[i][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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 23 '06 #8
"Martin Jørgensen" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
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<intdata;
// 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.
Oct 23 '06 #9
Martin Jørgensen schrieb:
Martin Steen wrote:
>vector<vector<float a(10,10);

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<floatcan be a member of twoDimens.

This should work:

for(int i = 0; i < rows; i++)
{
vector<floatrowVector;

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








Best regards
Martin Jørgensen
Oct 23 '06 #10

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>
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);

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?

--
Bob R
POVrookie
Oct 23 '06 #11
Jim Langston wrote:
"Martin Jørgensen" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
-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)
Do you? Because it's a vector< vector < , that confuses me a bit...
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<intdata;
// 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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 24 '06 #12
Martin Steen wrote:
Martin Jørgensen schrieb:
-snip-
>
This can't work, because "linenumber" can't be a member of twoDimens.
Only a vector<floatcan 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<floatrowVector;

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<doublerow_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[i].size(); j++ )
{
cout << twoDimens[i][j] << ", ";

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

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

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

----------

>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.
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() );

>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
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 24 '06 #13

"Martin Jørgensen" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
Jim Langston wrote:
>"Martin Jørgensen" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk ...
-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)
My2DArray[n].Data.push_back( value );
Do you? Because it's a vector< vector < , that confuses me a bit...
>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<intdata;
// 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<intdata; 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.

}
>

Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

Oct 24 '06 #14

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>
----------

std::vector<std::vector <double twoDimens;
std::vector<doublerow_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[i].size(); j++ ){
cout << twoDimens[i][j] << ", ";

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

// if( j == twoDimens[i].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)
>----------
--
Bob R
POVrookie
Oct 24 '06 #15
Jim Langston wrote:
"Martin Jørgensen" <ho**********@hotmail.comwrote in message
news:45*********************@dread12.news.tele.dk. ..
-snip-
>>>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)


My2DArray[n].Data.push_back( value );
Oh, ofcourse... I also thought something was missing.

-snip-
>>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<intdata; 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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 24 '06 #16
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>>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);

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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 24 '06 #17
BobR wrote:
Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>>----------

std::vector<std::vector <double twoDimens;
std::vector<doublerow_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();
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[i].size(); j++ ){
cout << twoDimens[i][j] << ", ";

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

// if( j == twoDimens[i].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.
> } // 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)
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 24 '06 #18
Martin Jørgensen wrote:
>// There is a handy little function in vector to do that:

row_values.clear();

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<floatrowVector; // 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
Oct 25 '06 #19
Martin Steen wrote:
Martin Jørgensen wrote:
>>// There is a handy little function in vector to do that:

row_values.clear();


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:
I thought it should be completely outside the function... But probably not.
for(int i = 0; i < rows; i++)
{
vector<floatrowVector; // 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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Oct 25 '06 #20

Martin Jørgensen wrote in message
<45*********************@dread12.news.tele.dk>.. .
>
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??? */
std::cout << " abc=" << abc << std::endl; // no prob.
}
}

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
#include <iostream>
#include <ostream>

int main(){
int abc(0), bla(0), blabla(5);
std::cout << abc << " " << bla << std::endl;
int xyz(5);
cout <<"xyz="<<xyz<< std::endl;

for( int bla(0); bla < blabla; ++bla){
++abc;
std::cout << " abc=" << abc << std::endl;
std::cout << " bla=" << bla << std::endl;
int xyz(25); // it's inside a 'scope' {}. A separate object.
std::cout <<" xyz="<<xyz<< std::endl;
} // for(bla)

std::cout << abc << " " << bla << std::endl;
std::cout <<"xyz="<<xyz<< std::endl;

return 0;
} // main()

(like 'Vegas) In C++, what's in a for() stays in the for()! <G>
Note how there is an 'int bla' in main(), and *another* 'int bla' in the
for(), and no name clash.

/* -- output --
0 0
xyz=5
abc=1 // <--- Hmmm, seems to be 'in scope'
bla=0
xyz=25
abc=2
bla=1
xyz=25
<snip>
abc=5
bla=4
xyz=25
5 0 // <--- 'abc' changed, but, 'bla' is original
xyz=5 // <--- the outside 'xyz' is the same
*/

Does that clear up any 'C' to 'C++' cobwebs?
--
Bob R
POVrookie
Oct 25 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amit D.Shinde | last post by:
Hello Experts.. I need some help regarding cookies and session objects and also global.asa file I am creating one cookie when a user logs in on my website. The cookie stores the login name of...
17
by: tuvok | last post by:
How can objects of different types be stored in a collection? For example: struct S1 { /*...*/ }; struct S2 { /*...*/ }; struct Sn { /*...*/ }; template<typename T> class X { public:
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
12
by: Sajid Saeed | last post by:
Hi All, I want to know if there is any way a complied EXE created in C# , can be called by another EXE . The App_MDI, should be the container for the App_A. (In short App_A should be a child...
0
by: Stephen Horne | last post by:
I've only very recently started with .NET, and need to use some existing container libraries in new code. I'd like to minimise the amount of porting, but of course these containers are not designed...
3
by: Captain Tangent | last post by:
I'm about to start making a 2d freeware RPG and I wondered which language would best suit my needs. As an arts grad, my strengths really lie on the creative side but I have a basic knowledge of...
1
by: Martin | last post by:
The VS2005 help documentation says: <QUOTE> OLE Container Control There is no equivalent in Visual Basic 2005. Applications that depend on this control should be left in Visual Basic 6.0....
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
1
by: toton | last post by:
Hi, I need to store some suicidal class to a container like deque. ( I am not using boost container library for pointers at this moment, using stl containers). The container stores some session...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.