some help required with maps , stucts and vectors.

L

LeTubs

Hi

I'm having a bit of trouble with maps (I'm a c programmer making the
transistion).
Now I want to do it this way as I won't know how many files/names untill
runtime, not bothered
by access (ie logrithimic/constant/linear) time unless the program goes way
slow!.so trying to
use STL that can grow grow and shrink as required.

Now
The layer is unqiue (ie 0 -> say 100 )
the filename is unique (ie file01.txt is unique )
but each layer can have multiple files ie layer 1 can
have file01.txt, file02.txt,....
struct is a simple stuct which holds status
information which is unique to that file ( & hence
layer).

I've include some sample code below...which I have been trying to get to
work.

basically so I can address the data like

layer_info[0]["file01.txt"].condvar;
layer_info[0]["file02.txt"].condvar;
layer_info[0]["file02.txt"].condvar;

( 0 -> layer, file01.txt -> file, and the filled out variable).

Any tips or pointers would be greatly appreciated.

Many Thanks
David
-------- cut --------------- filename test.cxx

#include <map>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

typedef struct layer_struct{
int condvar;
int predicate;
} layer_struct;

typedef map< string, layer_struct> layer_map;
map < int, vector <layer_map> >a_layer;
layer_struct FilledOut;


int main(){

FilledOut.condvar = 1;
FilledOut.predicate = 1;
// load the record for file.txt into layer 1
a_layer[0, layer_map["File.txt",FilledOut]];

// ideally print out 1
cout << a_layer[0]["File.txt"].condvar << endl;
exit(0);

}
-------- cut-------------------
gcc test.cxx -o testw -lstdc++

OS : Red Hat.
 
J

Jonathan Mcdougall

LeTubs said:
Hi

I'm having a bit of trouble with maps (I'm a c programmer making the
transistion).

I can see that :)
basically so I can address the data like

layer_info[0]["file01.txt"].condvar;
layer_info[0]["file02.txt"].condvar;
layer_info[0]["file02.txt"].condvar;

What you want is

layer_info is a map of
integer -> a map of
string -> layer_struct

So doing layer_info[0] yields a map of string->layer_struct and doing
layer_info[0]["file"] yields a layer_struct.
( 0 -> layer, file01.txt -> file, and the filled out variable).

Any tips or pointers would be greatly appreciated.

Many Thanks
David
-------- cut --------------- filename test.cxx

#include <map>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

typedef struct layer_struct{
int condvar;
int predicate;
} layer_struct;

You don't need that in C++:

struct layer_struct
{
int condvar;
int predicate;
};

Both look like booleans, so you could make them "bool" instead of
"int".
typedef map< string, layer_struct> layer_map;
map < int, vector <layer_map> >a_layer;
layer_struct FilledOut;

This is a mess

typedef map<string, layer_struct> Layer; // one layer
typedef map said:
int main(){

FilledOut.condvar = 1;
FilledOut.predicate = 1;
// load the record for file.txt into layer 1
a_layer[0, layer_map["File.txt",FilledOut]];

// ideally print out 1
cout << a_layer[0]["File.txt"].condvar << endl;
exit(0);

layer_struct ls = {1, 1};

Layer layer;
layer.insert(std::make_pair("filename", ls));

Layers layers;
layers.insert(std::make_pair(0, layer));

std::cout << layers[0]["filename"].condvar;


Jonathan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top