Strategy to save pairs of data

I want to implement a container to store values in a 'pair' style.
The structure are going to be something like a spreadsheet, some cells have data and others not. The 'pairs' are row and col.


In example, I'm going to have values
0,1 0,2 0,5 (0,3 0,4 are empty and I dont want to reserve space for it)
1,7 1,9 ... (1,0 to 1.6 empty)

0,1 has a value
0,2 has a value
0,3 0,4 has no value
1.7 has a value
etc.




What container can I use ? Map, a combined ?
A map with float index (So, can be an idea to convert row col to row.col?)
I want to deal with millions of points...

Any idea ? Thanks
You could have a std::map<Coordinate, Value> and overload bool operator >(Coordinate, Coordinate) (or was it <..?) so that it first compares rows and if they are equal, compares columns.
Last edited on
Can you explain me it a little more ?
I'd say, map<int, map<int, value> > One int is the row, the other is the column, the value is the value at the cell. I suppose that's what you wanted?
Ok, Thank you very much.
In your opinion, is this an appropiate start point to have spreadsheet data stored ?
Thanks
Can you explain me it a little more ?


1
2
3
4
5
6
7
8
9
10
11
12
struct Coordinate{
   int x, y;
   Coordinate(int x, int y) : x(x), y(y) {}
};

//map uses operator < to compare keys
bool operator < (Coordinate a, Coordinate b){
   if( a.y < b.y ) return true;
   if( a.y > b.y ) return true;
   //else a.y == b.y, then
   return a.x < b.x;
}

1
2
std::map<Coordinate, Value> spreadsheet;
spreadsheet[ Coordinate(5, 2) ] = some_value;


Though hanst99's solution is fine too.
tonnot wrote:
I want to deal with millions of points...

If you have that many points, std::map won't cut it.
A hashmap would be a better approach.

Here's a trivial example -> http://cplusplus.com/forum/beginner/31394/#msg170354

I think I forgot to #include <cstdlib> though...
And I'm sure there are better implementations on the web.

Last edited on
Topic archived. No new replies allowed.