Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:52:42+00:00 2026-06-01T03:52:42+00:00

I am initializing map<string, vector<string> > as follows: map <string, vector<string> > dict; dict[USA].push_back(NYC);

  • 0

I am initializing map<string, vector<string> > as follows:

map <string, vector<string> > dict;

dict["USA"].push_back("NYC");
dict["USA"].push_back("LA");
dict["USA"].push_back("Chicago");
dict["USA"].push_back("Dallas");

dict["India"].push_back("Delhi");
dict["India"].push_back("Bombay");

dict["Australia"].push_back("Melbourne");
dict["Australia"].push_back("Sydney");
dict["Australia"].push_back("Adelaide");

I find this cumbersome. The same thing can be done in tcl as follows which is cleaner:

array set dict {
USA {NYC LA Chicago Dallas}
India {Delhi Bombay}
Australia {Melbourne Sydney Adelaide}
}

Is there a more cleaner way to initialize in C++? My compiler is gcc 3.4.6

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:52:44+00:00Added an answer on June 1, 2026 at 3:52 am

    If you’re not afraid of using a bit of C-style macros and some helper constructs you might find this slightly less irritable; the initialization of the map is done in one line; you only need to fill in the data (which you must do anyway).

    #include <iostream>
    #include <map>
    #include <vector>
    #include <string>
    #include <utility>
    
    using namespace std;
    
    struct entry {
        string  key;
        string* vals;
        size_t  nvals;
    };
    #define NVAL(x) (sizeof(x)/sizeof(x[0]))
    
    int main( void ) {
        // Create your data 
        string      usa[]      = {"NYC", "LA"};
        string      india[]    = {"Delhi", "Mumbai"};
        entry       dd[] = {
                              {"USA", usa, NVAL(usa)},
                              {"India", india, NVAL(india)}
                            };
        map<string, vector<string> > dict;
    
        // initialize the map
        for(unsigned int i=0; i<NVAL(dd); i++)
            dict.insert( make_pair(dd[i].key, vector<string>(dd[i].vals, dd[i].vals+dd[i].nvals)) );
    
        // Verify
        for( map<string,vector<string> >::const_iterator ptr=dict.begin();
             ptr!=dict.end(); ptr++) {
            cout << ptr->first << ": ";
            for( vector<string>::const_iterator eptr=ptr->second.begin();
                 eptr!=ptr->second.end(); eptr++)
                    cout << *eptr << " ";
            cout << endl;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am initializing a map map<string,int> ex; in C++. I could not find contains_key
I am initializing a mutable string and then logging it as follows. NSMutableString* mStr
Is there some way of initializing a Java HashMap like this?: Map<String,String> test =
std::map< std::string , std::string > matrix_int; typedef std::pair< std::string , std::string > lp_type; BOOST_FOREACH(
Guava offers a nice shortcut for initializing a map. However I get the following
I have a class in C++ with the following member: map< someEnum, vector<SomeObject*>* >
I am having an issue initializing a map in the page. The code within
When initializing a new Date object in JavaScript using the below call, I found
I'm initializing an unsigned short int with a = 0xff (all bits are set).
I have a problem initializing an array whose size is defined as extern const.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.