Solved

STL map lookup table initialization

Posted on 2012-03-31
2
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…

710 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