Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > some help required with maps , stucts and vectors.

Reply
Thread Tools

some help required with maps , stucts and vectors.

 
 
LeTubs
Guest
Posts: n/a
 
      12-05-2005
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.


 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-05-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MSN maps vs. Google maps Ralph Fox NZ Computing 0 07-30-2007 10:30 AM
Create a VD with required properties on a required Web Server (Windows Advanced Server 2000 has 3 Web Servers) Just D. ASP .Net 0 03-10-2006 06:02 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
std::maps within std::maps -- optimisation Simon Elliott C++ 4 03-10-2005 10:11 AM
Some help required: Different flavour of logon via FormsAuthentication? Mike Kingscott ASP .Net Security 0 07-24-2003 10:11 PM



Advertisments