Solved

STL map lookup table initialization

Posted on 2012-03-31
2
659 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 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