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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:48+00:00 2026-05-26T09:06:48+00:00

If I have a structure like std::map<string, int> myMap; myMap["banana"] = 1; myMap["apple"] =

  • 0

If I have a structure like

std::map<string, int> myMap;
myMap["banana"] = 1;
myMap["apple"] = 1;
myMap["orange"] = 1;

How can I access myMap[0]?

I know that the map sorts internally and I’m fine with this, I want to get a value in the map by index. I’ve tried myMap[0] but I get the error:

Error   1   error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)   

I realise I could do something like this:

string getKeyAtIndex (map<string, int>& myMap, int index){
    map<string, int>::const_iterator end = myMap.end(); 

    int counter = -1;
    for (map<string, int>::const_iterator it = myMap.begin(); it != end; ++it) {
        counter++;
        
        if (counter == index)
            return it->first;
    }
}

But surely this is hugely inefficient? Is there a better way?

  • 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-26T09:06:48+00:00Added an answer on May 26, 2026 at 9:06 am

    Your map is not supposed to be accessed that way, it’s indexed by keys not by positions. A map iterator is bidirectional, just like a list, so the function you are using is no more inefficient than accessing a list by position. If you want random access by position then use a vector or a deque.

    Your function could be written with help from std::advance(iter, index) starting from begin():

    auto it = myMap.begin();
    std::advance(it, index);
    return it->first;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure like struct category { int id,newID; std::string name; }; and
I have a similar data structure like this: struct Data { std::string id; Blob
There is some class which have methods like: int getSomething1(); std::string getSomething2(); someClass getSomething3();
Say I have a structure like: class SomeObject Public Name as String Public Created
I have the following data-structure as a class named Task: private: string name; int
This is kind of last resort.. So I have two maps. typedef std::map <string,
I have a structure like this: <ul> <li>text1</li> <li>text2</li> <li>text3</li> </ul> How do I
I have a structure like this.... UITableViewController -> UITableViewCell -> UIView I need the
I have a structure like the following: <form> <input type=text /> <input type=text />
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five

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.