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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:44:41+00:00 2026-05-14T07:44:41+00:00

Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void

  • 0

Consider this program:

#include <map>
#include <vector>
typedef std::vector<int> IntVector;
typedef std::map<IntVector,double> Map;
void foo(Map& m,const IntVector& v)
{
   Map::iterator i = m.find(v);
   i->first.push_back(10);
};
int main()
{
   Map m;
   IntVector v(10,10);
   foo(m,v);
   return 0;
}

Using g++ 4.4.0, I get his compilation error:

test.cpp: In function 'void foo(Map&, const IntVector&)':
test.cpp:8: error: passing 'const std::vector<int, std::allocator<int> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]' discards qualifiers

I would expect this error if I was using Map::const_iterator inside foo but not using a non-const iterator.

What am I missing, why do I get this error?

  • 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-05-14T07:44:42+00:00Added an answer on May 14, 2026 at 7:44 am

    The keys in a map are constant. A map is a tree, and you can’t just going around changing the keys or you’ll break its invariants. The value_type of a map with Key and Value is std::pair<const Key, Value>, to enforce this.

    Your design needs some changing. If you really need to modify the key, you need to remove the element, change its key, and re-insert it with the new key.

    Also concerning your example specifically, you will get undefined behavior (if this did work). Your map is empty when you call foo, so the iterator returned by find will be m.end(); the element doesn’t exist. But then you’d go on to modify this non-existent element: ka-boom. Whenever you find something, you should check that it was found before you attempt to use it.

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

Sidebar

Related Questions

Consider the following program: #include <cstddef> #include <cstdio> void f(char const*&&) { std::puts(char const*&&);
Consider the following program: #include <iostream> typedef void (*fptr)(...); void foo(fptr func) { (*func)(12);
Consider this program int main() { float f = 11.22; double d = 44.55;
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
Consider the following program, which is obviously buggy: #include <cstdio> double test(int n) {
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider this little program: #include <stdio.h> int main() { char c = 0xFF; printf(%d\n,
Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void callptr(); static void
Consider this pointless program: /* main.c */ #include <stdlib.h> #include <unistd.h> int main(int argc,

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.