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

Initialize const or static map

I should like to use a map as a look-up table from one string to
another. I know the look up I want to use at compile time, and I dont
want to spend time during running to construct the object with a new
map..

Is there any way I can declare a const or static map and fill it such
that this isn't done at run-time (at least, not on each construction of
the object)? There are about 20 entries, not hundreds.

cheers

shaun
Jan 20 '06 #1
3 12527
shaun wrote:
I should like to use a map as a look-up table from one string to
another. I know the look up I want to use at compile time, and I dont
want to spend time during running to construct the object with a new
map..

Is there any way I can declare a const or static map and fill it such
that this isn't done at run-time (at least, not on each construction of
the object)? There are about 20 entries, not hundreds.


No, this isn't possible. You may want to try something like gperf. See
http://www.gnu.org/software/gperf/

Jan 20 '06 #2
In article <dq*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.de> wrote:

Is there any way I can declare a const or static map and fill it such
that this isn't done at run-time (at least, not on each construction of
the object)? There are about 20 entries, not hundreds.


No, this isn't possible. You may want to try something like gperf. See
http://www.gnu.org/software/gperf/


If I have a class with a static map, won't that initialize at run time
but before the main body of the program? Even that would be a bonus.

Is the following possible in a class (this is nearly pseudo code,
derived from a "codecomments.com article):

pair<string, string> a[] = {
pair<string, string>("a","apple"),
pair<string, string>("b","banana"),
}

map<string, string> myMap(a, a + sizeof(a) / sizeof(a[0]) );
where a and myMap are declared static in the header? the strings were
char cont * in the original article, does this make a difference?
Jan 20 '06 #3
shaun wrote:
Is there any way I can declare a const or static map and fill it such
that this isn't done at run-time (at least, not on each construction of
the object)? There are about 20 entries, not hundreds.
No, this isn't possible. You may want to try something like gperf. See
http://www.gnu.org/software/gperf/


If I have a class with a static map, won't that initialize at run time
but before the main body of the program?


Yes, that's right.
Even that would be a bonus.

Is the following possible in a class (this is nearly pseudo code,
derived from a "codecomments.com article):

pair<string, string> a[] = {
pair<string, string>("a","apple"),
pair<string, string>("b","banana"),
}

map<string, string> myMap(a, a + sizeof(a) / sizeof(a[0]) );

where a and myMap are declared static in the header?
Yes, that should work.
the strings were char cont * in the original article, does this make a
difference?


Not regarding the correctness, but using std::string will increase the
initialization time, since the strings usually dynamically allocate memory
for the data and copy the contents of the string literals over. They might
even get copied again into the map.
I don't know if that makes a difference for your program. Just wanted to
note it.

Jan 20 '06 #4

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

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
2
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
7
by: Nan Li | last post by:
Hello, I got stuck in a situation I think many of you must have already experienced. I want to have a simple lookup map inside my class. All the instances share that map. The code is as follows....
6
by: nik | last post by:
hello friends, a class having two const variables ,how can we initialize these variables
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
6
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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...

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.