Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ list remove duplicates strings

Tags:

c++

list

unique

I have a little problem when using lists.

What I have: I am reading lines from a chatbox where new lines of text come now and then. I always fetch the last 20 lines from the box, then i want to compare them to all the lines i have fetched before. If a new line is discovered it is sent to an external function which disassembles the line for further processing. Before I used arrays and vectors, but list seem to be the better way of doing it.

My Idea: I have one list called usedlines which contains all the old allready used lines. The list fetchedLines containes the newest 20lines fetched from the chatbox.

No I simply want to loop trough both of them to find out if fetched lines containes a new line not seen before. After the loop the remains in fetchedlines are handled over to the next function.

Problem: When I loop throug this loop i get a badpointer after a while. Why? Bonus: Does anyone have a better idea to solve this task?

typedef list<string> LISTSTR;
LISTSTR::iterator f;
LISTSTR::iterator u;
LISTSTR fetchedlines;                 
LISTSTR usedLines;                



fetchedlines.insert(fetchedlines.end(), "one");
fetchedlines.push_back("two");
fetchedlines.push_back("three");
fetchedlines.push_back("four");
fetchedlines.push_back("three");

usedLines.push_back("three");
usedLines.push_back("blää");
usedLines.push_back("lumpi");
usedLines.push_back("four");


 for (u =  usedLines.begin(); u != usedLines.end(); u++)
 {
 for (f =  fetchedlines.begin(); f != fetchedlines.end(); f++)
   {
   if(*u==*f)
    fetchedlines.remove(*f);
  }

}
like image 363
Lumpi Avatar asked Jan 20 '11 15:01

Lumpi


1 Answers

The call to fetchedlines.remove(*f) is invalidating your iterator.

EDIT:

A possible solution to the problem you are having is to instead just iterate usedLines and remove all elements in fetchedlines that are contained.

for (u = usedLines.begin() u != usedLines.end(); u++)
    fetchedLines.remove(*u);

//Process all of fetchedLines
like image 81
James Avatar answered Sep 28 '22 10:09

James