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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:30:05+00:00 2026-06-14T05:30:05+00:00

Why am I not allowed to read an object from a constant unordered_map? const

  • 0

Why am I not allowed to read an object from a constant unordered_map?

const unordered_map<int, int> z;
int val = z[5]; // compile error

The error under clang is the following:

error: no viable overloaded operator[] for type 'const
      unordered_map<int, int>'
                        int val = z[5];

Considering that the equivalent code using a const vector works fine I’m somewhat confused why we get this behavior.

  • 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-14T05:30:06+00:00Added an answer on June 14, 2026 at 5:30 am

    The expression z[5] calls a non-const member function of the map.

    This is because a map’s operator[] will insert a new element if the key isn’t found, so obviously it has to be non-const.

    For a vector nothing is inserted by operator[], the element must exist already (or you get undefined behaviour, so the equivalent code would access the 6th element of an empty vector, which is not fine!).

    To lookup a key without adding it use:

    int val = 0;
    auto it = z.find(5);
    if (it != z.end())
      val = it->second;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the error changes not allowed when unmanaged debugging is enabled Actually I
Why something like following is not allowed. I mean why the following method will
Here is my manifest file which shows error string types not allowed at android:configChanges
I have the following code and was wondering why I am not allowed to
Primitive types are not allowed in ArrayList, source . Partial solution: you can wrap
This is not allowed in Mysql: SELECT CAST(0 as DOUBLE) as ZERO How do
I realize that virtual template functions are not allowed in c++. Because of my
I thought null bytes were not allowed in shellcode. While understanding some shellcode, I
I've understood that a network operation is not allowed on the GUI thread. Ok
I recently learned that table valued functions are not allowed with Entity Framework 4.1.

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.