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 8678025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:36:27+00:00 2026-06-12T20:36:27+00:00

Possible Duplicate: Why does a class used as a value in a STL map

  • 0

Possible Duplicate:
Why does a class used as a value in a STL map need a default constructor in …?

When I’m using a map, do values definitely get initialized to defaults or should I not rely on this?

For example, say I have the following code:

map<string, int> myMap;
cout << myMap["Hey"];

This will output “0” with my compiler. Is this guaranteed behavior? Is it possible that this wouldn’t always initialize to 0?

  • 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-12T20:36:28+00:00Added an answer on June 12, 2026 at 8:36 pm

    Quoth the standard:

    ISO/IEC 14882 §23.4.4.3

    T& operator[](const key_type& x);

    1. Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.
    2. Requires: key_type shall be CopyConstructible and mapped_type shall be DefaultConstructible.
    3. Returns: A reference to the mapped_type corresponding to x in *this.
    4. Complexity: logarithmic.

    So, not only is it guaranteed, but evaluating myMap["Hey"] also causes the value 0 to be inserted into the map, if there was no entry for it before.

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

Sidebar

Related Questions

Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: What does a colon following a C++ constructor name do? Class construction
Possible Duplicate: Why does a class implements Serializable interface? I'm using a tutorial found
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: What does :_* (colon underscore star) do in Scala? I’m using the
Possible Duplicate: Does the order of class definition matter in PHP? class a {
Possible Duplicate: Does C# have extension properties? I have the following: public static class
Possible Duplicate: Why does defining getitem on a class make it iterable in python?
Possible Duplicate: ImproperlyConfigured: Middleware module “django.middleware.csrf” does not define a “CsrfResponseMiddleware” class I am
Possible Duplicate: Does a const reference class member prolong the life of a temporary?

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.