Two dimensional map

Hello,

I'm currently having the following data structure:

1
2
3
4
5
6
typedef struct {
   int a;
   int b;
} S;

<vector <vector<S> > v;


Now I also have different pointers that can be set in connection with one instance of the above shown vector. Therefore I was attempting to work with a map:

 
map<p*, <vector<vector <S> >, vector<sI> > m;


However, it seems like it doesn't work that way. Therefore I tried the following:

 
map<p*, map<<vector<vector <S> >, vector<S> > > m2;


With this attempt I do now, however, get another mistake here, that [] cannot be used here:

1
2
S s;
m2[p][0].push_back(s);


Do you have any tips for me?
Last edited on
You should not use those things that you do not understand. There is such a principle as KISS in programming that means Keep It Simpler, Stupid.
Last edited on
> I'm currently having the following data structure:

What are you using it for? And how do you want to use the map for look up?
Let's say it like that:

I need two vectors as I do not want to define array-sizes without knowing how much I'll eventually need.

So I'll have one vector representing the clients which holds a vector representing data of those clients organised in structs.

a) Therefore I now have a vector for clients holding a vector for data which is organised in structs. That's this part:

1
2
3
4
5
6
typedef struct {
   int a;
   int b;
} S;

<vector <vector<S> > v;


b) However, I am also having pointers representing instances of a certain script. I now have to combine those pointers with a) as I want to have the vectors organised with my instances. I thought I could do this with a map and later push_back new data somehow like that:

 
vec[pointer][client].push_back(s);


P.S: First day working with C++ - I work with Java and PHP more frequently.
Last edited on
What is the client? How is it identified? The phrase "Therefore I now have a vector for clients holding a vector for data which is organised in structs" is not clear,
Maybe you should keep the vector of data for a client inside the class defining the client as a private data-member.
Last edited on
> I do not want to define array-sizes without knowing how much I'll eventually need

YES.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <vector>
#include <map>
#include <cassert>

struct S
{
    int a;
    int b;
};

struct script { /* ... */ };

int main()
{
    typedef std::vector< std::vector<S> > client_data ;
    std::map< script*, client_data > look_up ;

    script scr ; script* pointer = &scr ;
    S ss ;
    std::size_t client_id = 0 ;

    assert( look_up[pointer].size() > client_id ) ; // validate (if reqd resize)
    look_up[pointer][client_id].push_back(ss) ;
}
Normally, I'd use a simple multi-dimensional array:

1
2
3
4
5
6
typedef struct {
   int a;
   int b;
} data;

clients[10][data];


The problem is that I am not able to know how often data will be needed in that array - that's why I am now using vectors.

Clients are therefore just numerously defined.

You're right, I could create a class holding the clientID as an attribute and create for every new player a new object. Question is, is it more efficent to create hundrets of objects with one single vector in each of them or one vector holding another vector inside of it?
Last edited on
> Question is, is it more efficent to create hundrets of objects with one single vector in each of them
> or one vector holding another vector inside of it?

In either case, you would have hundreds of vectors.

Lookup on client id would be easy and efficient with a nested vector - assuming that client ids are consecutive numbers starting at zero, of course.
Question is, is it more efficent to create hundrets of objects with one single vector in each of them or one vector holding another vector inside of it?

Actually, the real question is:

Which is going to help you more, right now, to produce the program you want, running reliably:

1) Making your code clearer and easier for you to understand, debug, and maintain, by properly encapsulating your client data inside a Client class

2) Fretting about efficiency.

Lots of programmers waste a lot of time and energy worrying about efficiency, to very little benefit, and make their code much harder to get working properly in doing it.
Last edited on
It seems to work now the way I want it to work. Though, there is an error occuring when attempting to compile the script:

1
2
data s;
s.a = 2;


The error has something to do with the instantiation and find(). Do you have any idea?
Last edited on
You know, I can understand why other posters on this forum are getting bored with having to say this over and over again.

Don't just tell us there's an error. TELL US WHAT THE ERROR IS. Why should we have to play guessing games, when you're asking us for help?
Well, it's all not in English. Shall I re-install the whole program now to get errors in English? I also cannot translate it as I do not fully understand it.
Last edited on
std::find() requires that the item to be found must be equality comparable.
Define operator==() for the struct (and ideally also operator!=())
Well, it's all not in English. Shall I re-install the whole program now to get errors in English? I also cannot translate it as I do not fully understand it.

If language is becoming a barrier to getting the help you're looking for, perhaps you'd be more able to get help from someone who speaks the same language as you?
Topic archived. No new replies allowed.