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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:44:34+00:00 2026-05-26T14:44:34+00:00

I have a problem in my code which i can’t understand. I want to

  • 0

I have a problem in my code which i can’t understand. I want to make a list of lists and use it like a two-dimensional associative array.

Something like this:

token["window"]["title"] = "Amazing!";
cout << token["window"]["title"]; //Amazing!

The second line works good. Data is readed from file. Problem is with the first instruction.

This is how i overload the second square brakcets:

TokenPair Token::operator[](string keyName){
    for( list<TokenPair>::iterator pair=keys.begin(); pair != keys.end();++pair){
        if(pair->key == keyName){
            return *pair;
        }
    }
}

As you see I return object of class TokenPair. To properly get the value from object (field TokenPair::value) i overload streaming and casting on string().

TokenPair::operator string() const{
    return value;
}
ostream & operator<< (ostream &stream, const TokenPair &pair){
    return stream << pair.value;
}

And as i say before getting value works great. Problem is with overloading operator of attribution:

TokenPair TokenPair::operator=( const string newValue ){
    value = newValue;
    return *this;
}

This method assing the value but it not remember that! For example:

token["window"]["title"] = "Ok";

will cause that inside method TokenPair::operator= variable newValue==”Ok” and after the first line even value is set to “Ok”; But when i later do:

cout << token["window"]["title"] ;

the field in TokenPair is still not changed. I want to ask: Why? Maybe iterators return a copy of that object? I don’t know. Please help.

  • 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-26T14:44:35+00:00Added an answer on May 26, 2026 at 2:44 pm

    Here is an example of how to use a map-of-maps:

    #include <map>
    #include <string>
    #include <iostream>
    #include <sstream>
    
    const char inputString[] =
      "President 16 Lincoln "
      "President 1 Washington "
      "State Best Illinois "
      "State Biggest Alaska ";
    int main () {
      std::map<std::string, std::map<std::string, std::string> > token;
      std::string primary, secondary, result;
      std::istringstream input(inputString);
      while( input >> primary >> secondary >> result )
        token[primary][secondary] = result;
      std::cout << "Abe " << token["President"]["16"] << "\n";
      std::cout << "Springfield, " << token["State"]["Best"] << "\n";
      std::cout << "Blank: " << token["President"]["43"] << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tricky problem which I can't completely understand. It's jQuery tabs with cookie
Problem Hello all! I have this code which takes my jpg image loops through
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
I'm training code problems like UvA and I have this one in which I
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a datatable being built in code which can dynamically have any given
I have a problem which I can't seem to be figuring out: I load
I have a problem which can't solve by myself. Here is timestamp 1308085200 taken
I have strange problem for which I can't think of a solution. I have
Evening :) I have a little problem... As you can see in my code

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.