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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:56:29+00:00 2026-06-03T08:56:29+00:00

I have a map<string, list<int> > which I want to iterate over the list

  • 0

I have a map<string, list<int> > which I want to iterate over the list and print out each number. I keep getting a compile error talking about conversion between a const_iterator and iterator. What am I doing wrong with this?

for (map<string, list<int> >::iterator it = words.begin(); it != words.end(); it++)
{
   cout << it->first << ":";
   for (list<int>::iterator lit = it->second.begin(); lit  != it->second.end(); lit++)
      cout << " " << intToStr(*lit);
   cout << "\n";
}

error: conversion from
  ‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<int, std::allocator<int> > > >’
to non-scalar type
  ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<int, std::allocator<int> > > >’
requested|
  • 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-03T08:56:30+00:00Added an answer on June 3, 2026 at 8:56 am
    map<string, list<int> >::iterator
    

    should be

    map<string, list<int> >::const_iterator
    

    Either your map is const, or your map is a member of a class and you’re calling this code in a const function, which also makes your map const. Either way, you can’t have a non-const operator on a const container.

    EDIT: Am I the only one who prefers to use explicit types over auto?

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

Sidebar

Related Questions

I have an unordered map: class O(val a: Int) Map[String, List[O]] which I'd like
If i have a stl map from string to int and i want to
I have a map declared as follows: map < string , list < string
i have a member like this in a serializable class: private final Map<String, List<T>>
I have this: map<string,int> a; int b; And i'd like to make this: switch(b)
I have a HashMap<String,String> and there is a static method which returns this map
I have a List and each entry has a dynamic count of attributes which
I have a list of parent keys, each of which could possibly have zero
I have a Map to sort as follows: Map<String, String> map = new HashMap();
let's say I have std::map< std::string, std::string > m_someMap as a private member variable

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.