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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:36:08+00:00 2026-06-04T07:36:08+00:00

I have a map named valueMap as follows: typedef std::map<std::string, std::string>MAP; MAP valueMap; …

  • 0

I have a map named valueMap as follows:

typedef std::map<std::string, std::string>MAP;
MAP valueMap;
...
// Entering data.

Then I am passing this map to a function by reference:

void function(const MAP &map)
{
  std::string value = map["string"];
  // By doing so I am getting an error.
}

How can I get the value from the map, which is passed as a reference to a function?

  • 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-04T07:36:09+00:00Added an answer on June 4, 2026 at 7:36 am

    std::map::operator[] is a non-const member function, and you have a const reference.

    You either need to change the signature of function or do:

    MAP::const_iterator pos = map.find("string");
    if (pos == map.end()) {
        //handle the error
    } else {
        std::string value = pos->second;
        ...
    }
    

    operator[] handles the error by adding a default-constructed value to the map and returning a reference to it. This is no use when all you have is a const reference, so you will need to do something different.

    You could ignore the possibility and write string value = map.find("string")->second;, if your program logic somehow guarantees that "string" is already a key. The obvious problem is that if you’re wrong then you get undefined behavior.

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

Sidebar

Related Questions

I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
In my action class I have a Map<Long, Set<String>> attribute, named accountsMap. Lets say
I have a map declared as follows: map < string , list < string
I have a class like this: class MyEntity { @ElementCollection Map<String, String> properties; }
I have two maps of type Map<Long, Integer>, one named oldValues representing the old
I have a named route: map.find '/find/:category/:state/:search_term/:permalink', :search_term=>nil, :controller=>'find', :action=>'show_match' and the following URL
I have an object named Puzzle and I'm calling .map on it in order
I have this interface for using AutoMapper: public interface IMapper { object Map(object source,
In a project MyProject.Data, I have a folder named mappings with all mapping in.
In Rails 2.X we have: map.resources :posts, :controller => 'posts', :as => 'articles' This

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.