Help with a map of maps

Apr 11, 2011 at 12:16pm
I dont know how to give data to a map of map.
I'm lost....
For example :
map <int, map<int,char>> map1;
Imagine this is row, col data....


- how can I give data , for example: row 1 col 1 value 243
- How can I retrieve the pair data for row 7, col 23
- How can I iterate about the data of row 7 ?
- How can I iterate about row-data?

I understand the basica Map but not this. (and I dont find anything clear on the web...)
Thanks
Apr 11, 2011 at 1:21pm
Well, map doesn't allow duplicates, so you will only be able to have one column in any given row, which probably isn't what you want. You'll really want a std::multimap< int, std::map< int, char > > if you want a "spreadsheet-like" data structure. But nesting STL containers is usually a little harder to read/maintain. I would probably do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct cell_coord_t {
    typedef size_t row_t;
    typedef size_t col_t;

    cell_coord_t() : row( 0 ), col( 0 ) {}
    cell_coord_t( row_t row, col_t col ) : row( row ), col( col ) {}

    bool operator<( const cell_coord_t& rhs ) const
        { if( row < rhs.row ) return true;
           if( rhs.row < row ) return false;
           return col < rhs.col;
         }

    row_t row;
    col_t col;
};

// What's held in a cell
struct cell_value_t
{
};

// and now use std::map:
std::map< cell_coord_t, cell_value_t > spreadsheet;


Apr 11, 2011 at 1:31pm
Note that the space is required:
1
2
std::multimap< int, std::map< int, char > >
//                                       ^ 


According to the standard, during the lexical analysis phase of compilation, the principle of "maximum munch" applies. Since >> is a valid operator, it will be tokenized as one, rather than two separate symbols unless there is whitespace between them.
Topic archived. No new replies allowed.