473,809 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.condv ar = 1;
FilledOut.predi cate = 1;
// load the record for file.txt into layer 1
a_layer[0, layer_map["File.txt",Fill edOut]];

// 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 1338
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.condv ar = 1;
FilledOut.predi cate = 1;
// load the record for file.txt into layer 1
a_layer[0, layer_map["File.txt",Fill edOut]];

// ideally print out 1
cout << a_layer[0]["File.txt"].condvar << endl;
exit(0);
layer_struct ls = {1, 1};

Layer layer;
layer.insert(st d::make_pair("f ilename", ls));

Layers layers;
layers.insert(s td::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
1668
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. The two reasons are (1) that vector elements are smaller, since map elements need at least 3 pointers, and (2) contiguity of vector elements in memory ensure fewer page faults.
3
61104
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. Size the form to your liking...
5
1555
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 you don't have to worry about them at all...even if the contents of your map or vector may contain other maps or vectors. Am I mistaken in any way? We are running into a very strange bug that is avoiding detection by hiding in the default...
4
2212
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
5
1785
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 before, but it's nearly impossible to find. I'm having trouble using STL vectors, maps, ... in templated functions, when the templated class is a template parameter of the map too. Below is a (cannibalized) example of a class 'Model' which contains...
5
1892
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, NodePtr>::iterator NodeMapItr; NodeMap nmap; In my classes, I often find myself copying the second arguments of
7
11432
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 allocator for both data types, or the allocator for map should ahve different signature??? -Thanks -Mavrik
1
2067
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 function that handles vectors of vectors of <typename T(which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated...
0
1344
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 are approximately constant. The nodes and associated data are stored in a quasi-static dynamic memory (quasi_buffer) which reserves a minimum memory, but on demand can grow upto an user defined maximum memory. The no of nodes in the graph are...
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.