table with text and numbers

J

Johan

Dear experts,

I am new to c++ (arriving from java) and I am having to implement a
data table that contains cells with numbers and cells with text. I
would appreciate any general indications about what would be the best
approach, bearing in mind that performance will be critical.

Many thanks

Johan
 
V

Victor Bazarov

Johan said:
I am new to c++ (arriving from java) and I am having to implement a
data table that contains cells with numbers and cells with text. I
would appreciate any general indications about what would be the best
approach, bearing in mind that performance will be critical.

Using std::vector<std::vector<std::string> > seems the most straight-
forward. If you know the dimensions of the "table" ahead of its
creation, you could use a simple two-dimensional array of std::string.

Since you didn't say what you're going to do with the table or its
cells, "performance will be critical" has no meaning. Perhaps if you
state the requirements, as in "performance to resize" or "performance
to replace data" or "performance to search"...

V
 
U

Uenal Mutlu

Dear experts,

I am new to c++ (arriving from java) and I am having to implement a
data table that contains cells with numbers and cells with text. I
would appreciate any general indications about what would be the best
approach, bearing in mind that performance will be critical.

It depends on your requirements (ie. whether dupes ok or not, auto-sorted or not etc.).
But basically you define what a cell item is and then make an array of it.
You can also use std::vector, std::set, std::list, std::map, std::multimap etc. etc.
 
J

johankildal

Uenal said:
It depends on your requirements (ie. whether dupes ok or not, auto-sorted or not etc.).
But basically you define what a cell item is and then make an array of it.
You can also use std::vector, std::set, std::list, std::map,
std::multimap etc. etc.
 
J

johankildal

THanks for your answers.

A bit of info about what I need to implement. The size of the table
will be unknown until runtime. The table will be fairly big (a few
thousands of cells) and will contain both numbers and text strings at
the same time. Numbers will have to remain as such in order to do
maths with them.
Once the table has been acquired, regrouping of cells will be done: 1
cell from every 2x2, with a content (text of number) calculated through
certain algorithms on the 4 grouped cells. Then further groupings: 1
cell with every 4x4, 8x8...). The original table needs to be preserved
and change of views will be done, forth and back, responding to user
actions, in real time.

Many thanks for any further comments on how to implement this

J
 
U

Uenal Mutlu

THanks for your answers.

A bit of info about what I need to implement. The size of the table
will be unknown until runtime.

But when it is known is it then constant?
The table will be fairly big (a few
thousands of cells) and will contain both numbers and text strings at
the same time. Numbers will have to remain as such in order to do
maths with them.
Once the table has been acquired, regrouping of cells will be done: 1
cell from every 2x2, with a content (text of number) calculated through
certain algorithms on the 4 grouped cells. Then further groupings: 1
cell with every 4x4, 8x8...). The original table needs to be preserved
and change of views will be done, forth and back, responding to user
actions, in real time.

Many thanks for any further comments on how to implement this

From C++ point of view this is basic stuff.
I don't know what types your cell contains (ie. is it an integer or floating point
number, and can the string be arbitrarily long or is there a max length),
but you have multiple ways to chose from. The simplest but maybe not
that mighty would be something like:

struct Cell
{
int iVal;
char szStr[64];
};

Cell* paCell = new Cell[nCells];

where nCells is the number of cells to allocate dynamically.
This is a 1-dimensional array of Cells; you can also make it n-dimensional.

Here you must delete the resource when finished by using
delete [] paCell;
On the other hand, depending on the class used, STL does the allocation
either in the constructor or when a new item gets added, and frees the
resource(s) in its destructor.

You just need to specify your application requirements as complete
as possible, but this is of course obvious for any language used.
I personally would recommend to use STL classes.
 
H

Howard

Johan said:
Dear experts,

I am new to c++ (arriving from java) and I am having to implement a
data table that contains cells with numbers and cells with text. I
would appreciate any general indications about what would be the best
approach, bearing in mind that performance will be critical.

Many thanks

Johan

When you talk about "cells", are you just talking conceptually, or do you
mean some kind of graphical display, like you see in spreadsheet programs?
The standard C++ language does not provide graphical support (GUI, mouse,
etc.), if that's what you're ultimately looking for. For that you need a
third-party library (like MFC in Visual C++, for example). But if all you
want is the storage method, then something like std::vector (or one of the
other containers) might be appropriate. Which one depends on the type of
operations you need to do (sorting, inserts, deletes, etc.) (Or, perhaps
you're interested in a real database, such as SQL or Access? In that case,
you'd again need to go to some third-party software.)

-Howard
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top