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

  • Home
  • SEARCH
  • 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 8318997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:10:13+00:00 2026-06-08T22:10:13+00:00

Inspired by this answer , I tried next example : #include <map> #include <string>

  • 0

Inspired by this answer, I tried next example :

#include <map>
#include <string>
#include <iostream>

int main()
{
  const std::map< int, std::string > mapping = {
      1, "ONE",
      2, "TWO",
    };

  const auto it = mapping.find( 1 );
  if ( mapping.end() != it )
  {
    std::cout << it->second << std::endl;
  }
  else
  {
    std::cout << "not found!" << std::endl;
  }
}

and the compilation failed with next error message (g++ 4.6.1) :

gh.cpp:11:5: error: could not convert '{1, "ONE", 2, "TWO"}' from '<brace-enclosed initializer list>' to 'const std::map<int, std::basic_string<char> >'

I know how to fix it :

  const std::map< int, std::string > mapping = {
      {1, "ONE"},
      {2, "TWO"},
    };

but why the compilation fails in the top example?

  • 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-08T22:10:15+00:00Added an answer on June 8, 2026 at 10:10 pm

    Because the map is a non-aggregate, and contains non-aggregate elements (std::pair<key_type, mapped_type>), so it requires an initializer-list full of initializer-lists, one for each pair.

    std::pair<int,int> p0{ 1,2 }; // single pair
    std::map<int, int> m { { 1,2 } }; // map with one element
    std::map<int, int> m { { 1,2 }, { 3,4} }; // map with two elements
    

    Bear in mind that the rules for brace elision apply to aggregates, so they do not apply here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

inspired by this answer I'm trying to map a property on a model class
Inspired by std::string in a multi-threaded program and another answer (seen somewhere), that STL
Inspired by the encoding scheme of the answer to this question , I implemented
I created a simple application inspired by this example in order to test all
Inspired by this answer .. Can you guys point me to something similar--something that
Inspired by this answer about dynamic cast to void* : ... bool eqdc(B* b1,
This question was inspired by this answer to another question, indicating that you can
G'day, Inspired by a comment I made in my answer to this question on
This question was inspired by this answer . I've always been of the philosophy
Inspired by this question and answer , how do I create a generic permutations

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.