Solved

STL map lookup table initialization

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

14 Experts available now in Live!

Get 1:1 Help Now