Solved

STL map lookup table initialization

Posted on 2012-03-31
2
654 Views
Last Modified: 2012-04-01
Hi Experts,

I have a map as below.
typedef map<std::string, IObject*> LookupTable_t;


I am initializing the map like below.
  m_lookupTable.insert(
            LookupTable_t::value_type(string1, 
                    new Runnable(this, &Parser::parseSomething)));

Open in new window

Basically, it serves as a table for( string, function).
Can I do this initialization in something like :

table = [{string1, new Runnable(this, &Parser::parseSomething)}, 
             {string2, new Runnable(this, &Parser::parserOther)}]

Open in new window

The reason I want to do this because I have many entries in the table and the second way seems to better(just for readability...)
0
Comment
Question by:ambuli
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 37792465
IN C++11, you can initialize std::map's as follows :

std::map<std::string, std::string, IObject*> lookupTable {
    { "object1", new Object(1) },
    { "object2", new Object(2) }
};

Open in new window


In C++03, you'll have to do a bit more work. Probably one of the easiest is to write a function that performs the initialization :

std::map<std::string, std::string, IObject*> createLookupTable() {
    std::map<std::string, std::string, IObject*> lookupTable;
    lookupTable["object1"] = new Object(1);
    lookupTable["object2"] = new Object(2);
    return lookupTable;
}
std::map<std::string, std::string, IObject*> lookupTable = createLookupTable();

Open in new window

0
 

Author Closing Comment

by:ambuli
ID: 37794393
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now