Skip to main content
gamedev.net
🔒 Locked

stl issue

Started by denethor Jun 13, 2002 at 6:09 PM 4 replies 400+ views
Original Post
denethor
denethor

  
list<string> lst;
lst.push_back("hello");
lst.push_back("hello");
lst.push_back("hello");
lst.push_back("hello");

list<string>::iterator iter;
for( iter=lst.begin; iter!=lst.end(); iter++)
{
    (*iter).erase();    //empty the string

    lst.erase(iter);    //removed the node from the list

}
  
In the above code, do I need the [(*iter).erase();] line, or will the string's destructor be called and erase itself when the node is destoyed? *the list is of type string [edited by - denethor on June 13, 2002 7:21:11 PM] [edit: added source tags] [edited by - Magmai Kai Holmlor on June 13, 2002 10:43:54 PM]
felonius
felonius
Yes, the strings destructor will automatically be called for each element in the list. You do not need that first line.

In fact, you don''t even have to erase all elements of the list manually. When the list goes out of scope its elements will automatically be deleted and their destructors will be called.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Fruny
Fruny
When you remove elements from a STL container, their destructor automatically gets called. Therefore, a std::string would automatically get cleaned up.

Note however, that a pointer doesn''t have a destructor (i.e. the element doesn''t get destroyed when the pointer gets out of scope, you have to manually delete them).

Your list seems to be a list of char pointers that point to statically allocated string constants (i.e. you didn''t allocate memory for the "hello" strings). Therefore there is no cleanup to be done (no pointers to delete).

Therefore, in your case, you do not have to call erase on the strings contained in you list. You would have to call delete if the strings were dynamically allocated (list of pointer to string or pointer to char*), though.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Shannon Barber
Shannon Barber
It''s a list of std::string, I added source tags so that it shows up. By default < and > are used to indicate html tags, so stuff between them is not displayed.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
IndirectX
IndirectX
When you use erase on an iterator, the iterator becomes invalid. Use

  
for( iter=lst.begin; iter!=lst.end(); ){
(*iter).erase(); //empty the string

iter = lst.erase(iter); //removed the node from the list

}

---visit #directxdev on afternet <- not just for directx, despite the name
Stoffel
Stoffel
or just lst.clear ().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.