473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

some help required with maps , stucts and vectors.

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.
Dec 5 '05 #1
1 1306
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

Dec 5 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Fred Ma | last post by:
Hello, I was looking at Meyers's "Effective STL", item 23 about choosing between vectors and maps (at least that the choice for me). In many cases, using sorted vectors is faster for lookups. ...
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
5
by: roberts.noah | last post by:
It is my understanding that if you contain vectors or maps you don't need to create copy constructors because the default calls that constructor. It is my understanding that if you use these types...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
5
by: DrLex | last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
7
by: mavrik | last post by:
Hi, I've written allocator for STL vectors, which take all memory from my heap. But I don't have any idea to implement the same for Maps. As maps take Key and Values. So should I use the same...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: abir | last post by:
I have a big dynamic sparse directed graph implementation where new nodes are added & old nodes are removed. Usually, at any moment of time the number of nodes in the graph and the number of edges...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.