Delete repeated lines in a string with getline

Hi!. I'm stuck in a function which in theory, should recibe a complete string, split per line (as they have a delimiter '\n') and eliminate duplicate, triplicate or quadruplicate lines. I can not remove the repeated lines :/ Here is the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string stringCleaner(string &str){
istringstream tempStr(str);
string dirtyLine, cleanLine;
unsigned current_line = 0;
string copyStr[current_line];

while (getline( tempStr, dirtyLine, '\n' )) {
	
   copyStr[current_line++] = dirtyLine; // Maybe something like that? An string vector? 
		
   //cleanLine+= dirtyLine+'\n'; // Resolve correctly the output
}

return cleanLine;	
}


The goal is to keep everything in one file but without repeated lines. An interesting fact is that the lines are repeated one after another, and will not be necessary to compare all the lines with all. Thanks in advance!!

Mac
Here is a suitable algorithm:

Open file for reading.
Open new file for writing.

Get first line - store in previousLine variable
Write first line.

repeat for all remaining lines:
{
Get next line - store in currentLine variable.
If currentLine is not the same as previousLine, write it.
previousLine = currentLine;
}

delete original file
rename output file to same name as deleted original file.



Hi. Moschopsm thanks!!.. Can you please be more especific?. Maybe the unique will work?? http://www.cplusplus.com/reference/algorithm/unique/

The string received is more or less as:
1
2
3
4
5
6
7
8
9
10
11
12
http://www.aaa.com
http://www.bbb.com
http://www.bbb.com
http://www.ccc.com
http://www.ddd.com
http://www.ddd.com
http://www.ddd.com
http://www.ddd.com
http://www.eee.com
. (more)
. (more)
. (more)

The desired output should be whit no repeated lines, like this..
1
2
3
4
5
http://www.aaa.com
http://www.bbb.com
http://www.ccc.com
http://www.ddd.com
http://www.eee.com 


Appreciate any help..
Last edited on
Here is how to check if two string objects are the same:

1
2
3
4
5
6
7
8
if (string1 == string2)
{
  cout << "They are the same";
}
else
{
  cout << "They are not the same";
}
the lines are repeated one after another, and will not be necessary to compare all the lines with all.

This makes it possible to simply use unique(), or, in your case, unique_copy()

If your input is indeed a list of URLs, you have no whitespace and it's safe to use the default iterators:

1
2
3
4
5
6
7
8
9
string stringCleaner(const string &str)
{
    istringstream tempStr(str);
    ostringstream cleanLine;
    unique_copy(istream_iterator<string>(tempStr),
                istream_iterator<string>(),
                ostream_iterator<string>(cleanLine, "\n"));
    return cleanLine.str();
}

demo: http://ideone.com/vVB8d

If you have whitespace, you must either use getline() (e.g. through a "line input iterator" http://ideone.com/CPxWU or modify the character classification table to only treat '\n' as whitespace: http://ideone.com/LOYO3

Hi Cubbi, thanks!! the unique_copy has solved the problem!!... Nice piece of code!!.. work like a charm!..


Thanks very much!!..
Topic archived. No new replies allowed.