473,387 Members | 1,453 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,387 software developers and data experts.

Data statements in C++

Just an old C programmer trying to learn a new trick
with C++ so excuse the dumb question:

The use of the "map" list seems quite useful to me for an
application updating an existing C system.

In the C system there is a simple structure array with the
first string being the key for lookup with linear search
(not a long list). The array is a global available to all
procedures and is initialized in a separate module as a
long data statement such as:

struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

This looks like a perfect application for C++ map container
but I cannot figure out how to do the equivalent
initialization. Every (*^% example I have of map applications
only shows a series of assignment statements in "main."

Is the equivalent usage and initialization possible?

If so, what does it look like?

Thanks for any help/suggestions.
Feb 4 '06 #1
6 1882

Jerry Evenden wrote:
Just an old C programmer trying to learn a new trick
with C++ so excuse the dumb question:

The use of the "map" list seems quite useful to me for an
application updating an existing C system.

In the C system there is a simple structure array with the
first string being the key for lookup with linear search
(not a long list). The array is a global available to all
procedures and is initialized in a separate module as a
long data statement such as:

struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

This looks like a perfect application for C++ map container
but I cannot figure out how to do the equivalent
initialization. Every (*^% example I have of map applications
only shows a series of assignment statements in "main."

Is the equivalent usage and initialization possible?


You cannot construct a map from a series of discrete key/value pairs.
You can construct a map from another map (copy construction), or you
can construct an empty map, and fill it with a series of assignments
('insert', or 'operator[]' plus assignment).

Feb 4 '06 #2
* Jerry Evenden:
[snip] struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

This looks like a perfect application for C++ map container
but I cannot figure out how to do the equivalent
initialization. Every (*^% example I have of map applications
only shows a series of assignment statements in "main."

Is the equivalent usage and initialization possible?

If so, what does it look like?


No such built-in thing.

In C++ you make such things, instead.

See <url: http://boost.org/libs/assign/doc/index.html#map_list_of>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 4 '06 #3
Alf P. Steinbach wrote:
* Jerry Evenden:
[snip]
struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

...
No such built-in thing.

In C++ you make such things, instead.

See <url: http://boost.org/libs/assign/doc/index.html#map_list_of>.


I think I see why. Unfortunately, the old fashion C method works
better in this case. I usually only do a lookup once for each
table and they are relatively short so a simple search is OK and a
make initialization would be a waste of time.

But I thought I had better check for sure. There may have been
some subtlety I missed.

Thanks all.

Feb 4 '06 #4
"Jerry Evenden" <ge************@verizon.net> wrote in message
news:I0VEf.56$PK2.43@trndny06...
Alf P. Steinbach wrote:
* Jerry Evenden:

[snip]
struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

...

No such built-in thing.

In C++ you make such things, instead.

See <url: http://boost.org/libs/assign/doc/index.html#map_list_of>.


I think I see why. Unfortunately, the old fashion C method works
better in this case. I usually only do a lookup once for each
table and they are relatively short so a simple search is OK and a
make initialization would be a waste of time.

But I thought I had better check for sure. There may have been
some subtlety I missed.

Thanks all.


It wouldn't be difficult to initialize the map either in a function or a
class.

Perhaps you can simply wrap the map in a class and initialize it in the
constructor. You could also store the map pares in a data file and read the
data file to initialize the map. But, realistically, it's one statement
for:
MyMap[Mykey] = MyValue;
and one statement for:
MyCStruct =
{ MyKey, MyValue }

So what's the difference?
Feb 4 '06 #5
On Sat, 04 Feb 2006 02:30:34 +0000, Jerry Evenden wrote:
Just an old C programmer trying to learn a new trick
with C++ so excuse the dumb question:

The use of the "map" list seems quite useful to me for an
application updating an existing C system.

In the C system there is a simple structure array with the
first string being the key for lookup with linear search
(not a long list). The array is a global available to all
procedures and is initialized in a separate module as a
long data statement such as:

struct PROJ_ELLPS
proj_ellps[] = {
{"nameA", "field1", "field2", "field3"},
{"nameB", "field1", "field2", "field3"},
...
};

where "name?" is the lookup key. The initialization is
contained in a separate file.

This looks like a perfect application for C++ map container
but I cannot figure out how to do the equivalent
initialization. Every (*^% example I have of map applications
only shows a series of assignment statements in "main."

Is the equivalent usage and initialization possible?

If so, what does it look like?

Thanks for any help/suggestions.


How about this:

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

// Not needed; just to clarify below.
typedef std::string Key;
typedef std::string Data;

typedef std::pair<Key, Data> DataPair;
static DataPair mapData[] =
{
DataPair("nameA", "fieldsA"),
DataPair("nameB", "fieldsb"),
DataPair("namec", "fieldsc"),
};

// Create and initialize the map.
static std::map<Key, Data>
m(mapData, mapData+sizeof(mapData)/sizeof(mapData[0]));

int main()
{
std::cout << m["nameB"] << std::endl;
return 0;
}

If you want the "mapped to" part to be something besides a string, then
you can change it (but it needs to be constructable with data):

struct Fields
{
Fields() {} // needs this for to be in the map.
Fields(const char* fA, const char* fB, const char* fC)
: fieldA(fA), fieldB(fB), fieldC(fC)
{
}

const char* fieldA;
const char* fieldB;
const char* fieldC;
};

typedef Fields Data;

static DataPair mapData[] =
{
DataPair("nameA", Fields("fieldA1","fieldA2", fieldA3")),
// ...
};

//...
int main()
{
std::cout << m["nameA"].fieldB << std::endl;
return 0;
}

- Jay

Feb 4 '06 #6
The basic technique in the parent is just what I use. One improvement,
IMO, is to declare a typedef for the map, and then use that type's
value_type (or a typedef thereof) as the type of the initialization
array. This change ties the initialization array element type
(DataPair, in the example) directly to the map type.

typedef std::map<Key, Data> M_map;
typedef M_map::value_type DataPair;

Feb 4 '06 #7

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

Similar topics

4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
5
by: Andrew Lowe | last post by:
Hi all, First off, I'm new to the Oracle world so please be gentle on me. Now down to the problem at hand. I support a CAD application that has an Oracle backend. I've been on site at my clients...
12
by: Ron | last post by:
What is the VB equivalent of the READ...DATA statements in QuickBasic? Thanks
9
by: Lior | last post by:
Hello . I know that the AndAlso and OrElse statements are short-circuiting And and Or statements , respectively . Should I always use (I don't like the word "always" ...) AndAlso instead of...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
6
by: Mike Wilson | last post by:
Dear Group, I have a heirarchical set of database tables, say - "order" and "order_type" and want to display a series of orders in a grid control, and in place of the order_type foreign key...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
3
by: aurora | last post by:
This is an entry I just added to ASPN. It is a somewhat novel technique I have employed quite successfully in my code. I repost it here for more explosure and discussions. ...
3
by: stronghold888 | last post by:
Hello everyone, I created a database in Access and on my form, I inserted a button that exports data into a ready Excel template. The export works fine, but I have one problem and it's from my...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.