473,696 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.begi n(), 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 4639
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 straightforward ly. (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.begi n(), 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_elemen t: http://www.sgi.com/tech/stl/nth_element.html

If you want the top 10% to be ordered, then use std::partial_so rt:
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 straightforward ly.
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_colum ns; 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<f loat 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<f loat 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_colum ns; column++)
{
linenumber++;
infile >read_value;

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

Gives:

In function 'void convert_data(un signed 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<st d::vector<float , std::allocator< float,
std::allocator< std::vector<flo at, std::allocator< float >
>::push_back(un signed int&)'
/usr/include/c++/4.0.0/bits/stl_vector.h:60 2: note: candidates are: void
std::vector<_Tp , _Alloc>::push_b ack(const _Tp&) [with _Tp =
std::vector<flo at, std::allocator< float, _Alloc =
std::allocator< std::vector<flo at, 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**********@h otmail.comwrote in message
news:45******** *************@d read12.news.tel e.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.begi n(), 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.
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<f loat 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_colum ns; 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<floatrow Vector;

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<CDa ta*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

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

Similar topics

2
3341
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 the user. I want that cookie should get deleted when user closes the browser without signing out. I think it is done in global.asa file . But i don;t know how to do it?
17
2596
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
12579
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 method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
12
6967
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 form in the App_MDI parent application) note: App_A will only contain one form. Thanks in Advance.
0
1172
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 to work in a .NET managed way. They make extensive use of pointer arithmetic, placement new and various other techniques that are obviously incompatible with managed code. If possible, I'd like to find a way of storing managed objects within...
3
1661
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 Pascal and C (when I say basic, I mean I can do all the if/while/storing local variables type stuff, but when it comes to messing with memory locations and parsing strings I get a bit lost). I guess the best option for me would be to utilise the...
1
4758
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. </QUOTE> I have an application that largely depends on the OLE Container control, and I do not want to leave it in VB6, especially since Microsoft stopped all support for VB6.
14
2035
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 <boost/shared_ptr.hpp> #include <vector> #include <iterator> #include <iostream> class trial {
14
3641
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 unsigned char or unsigned short like this: int nPixelType = ?; // unsigned short, or unsigned char? BYTE *pByte = NULL; switch (nPixelType) {
1
2413
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 class (not pointer, the class itself). But when the session terminates, I want the classes to commit suicide (using a drop /dispose method). And as soon as it commits suicide (i.e the session is closed by the user, and it calls suicide), I want it...
0
8666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8853
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.