1

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

Alex
  • 315
  • 1
  • 6
  • 19

2 Answers2

2

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);
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • This worked fine, just adding the &. But still I don't quite understand why. Now I dont have a copy of the list, but a reference (pointer?) to it. So why can I still do things like lista.begin() instead of lista -> begin() ? – Alex Jun 21 '12 at 15:35
  • here it is: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c – Alex Jun 21 '12 at 15:43
1

Does the variable lista have the same list I can get from the hash_map or a copy of it?

Yes lista is a copy, you're doing an assignment.

However, I still dont understand why it did work inserting elements.

With this code, you don't use a copy of (*it).second but the reference directly.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190