some help required with maps , stucts and vectors.

Discussion in 'C++' started by LeTubs, Dec 5, 2005.

  1. LeTubs

    LeTubs Guest

    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.
     
    LeTubs, Dec 5, 2005
    #1
    1. Advertising

  2. LeTubs wrote:
    > 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<int, Layer> Layers; // all layers

    >
    > 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
     
    Jonathan Mcdougall, Dec 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Elliott
    Replies:
    4
    Views:
    1,166
    Simon Elliott
    Mar 10, 2005
  2. Sheep
    Replies:
    1
    Views:
    393
    Sheep
    Aug 14, 2006
  3. Marcus
    Replies:
    2
    Views:
    591
    Marcus
    Dec 9, 2005
  4. Guest
    Replies:
    0
    Views:
    445
    Guest
    Sep 14, 2005
  5. Edward Liu
    Replies:
    6
    Views:
    172
    Andre Nathan
    Jan 14, 2008
Loading...

Share This Page