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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:50:49+00:00 2026-06-06T03:50:49+00:00

Im using the following structure: hash_map<string, list<time_t>> When I initially fill the hash map

  • 0

Im using the following structure:

hash_map<string, list<time_t>>

When I initially fill the hash map with the information I read from a text file, I have no problem inserting elements to those time_t lists.

hash_t::iterator it = hash.find(origen);

if (it != hash.end())
{
    (*it).second.push_front(fecha);         
}
else
{ 
    list<time_t> lista(1, fecha);
    hash.insert(make_pair(origen, lista));          
}

As you can see, if the key string is not in the table, I create a list with one time_t value and insert the pair on the table. On following apeareances of that same key, I just push the new time_t element on the already existing list and works fine.

I want to do the opposite now: erase elements of those lists.

hash_t::iterator it = hash.find(origen);            

if (it != hash.end())
{
    list<time_t> lista = (*it).second;  
    list<time_t>::iterator it2 = lista.begin();
    bool found = false;

    while(it2 != lista.end() && !found)
    {
        time_t fecha2 = *it2;           
        if (abs((int) difftime(fecha, fecha2)) <= 2) 
        {
            found = true;   
            lista.erase(it2);
        }
        else ++it2;
    }
}

This code is not eliminating elements from those lists.

I suppose the problem starts on this line:

list<time_t> lista = (*it).second; 

Does the variable lista have the same list I can get from the hash_map or a copy of it? If it is a copy, I undestand the reason it’s not working. However, I still dont understand why it did work inserting elements.

(*it).second.push_front(fecha);

Is there a way of erasing elements from the list using an aproach similar to what I’m doing, or would I have to change the whole structure of the hash_map to something like

hash_map<string, list<time_t>*>

Thank you very much in advance

  • 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-06T03:50:52+00:00Added an answer on June 6, 2026 at 3:50 am

    The erase() code is operating on a copy of the lists, not on the actual lists in the hashmap. This creates a copy:

    list<time_t> lista = (*it).second;
    

    Use a reference instead:

    list<time_t>& lista = (*it).second;
    

    The push_front() operates correctly because no copy is being made, the code accesses the list in the hashmap directly:

    (*it).second.push_front(fecha);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using following code I try to get updated list of checkbuttons' corresponding text values,
Can anyone help? I have the following structure using associations, and as you can
Using Model->find('all') returns an array with the following structure: array( 0 => array('Model1' =>
I am using a MySQL table called login with the following structure: loginid, username,
I am creating package using cmake I am having following structure bin/ bin1 lib/
I'm using Hibernate 3.3.1 and am following along in modelling this sample table structure
I have a HashMap of a following structure: HashMap<Integer, int[]> map = new HashMap<Integer,
I am using following code to convert string to sha1 string but i am
I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I
I am using following structure. <div style=background:#000 url(images/heading/bg.gif) repeat-x 0 0; height:28px; color:#FFF;> <div

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.