Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

STL map lookup table initialization

Posted on 2012-03-31
2
Medium Priority
?
674 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

886 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