Dies ist die Textdatei hat ich geschrieben:benachbarte doppelte Zeichenfolgen aus einem Array von Zeichenfolgen entfernen?
this is the first line
this is the first line
this is the second line
this is the second line
this is the second line
this is the third line
this is the first line
Ich versuche, die benachbarten doppelten Strings zu entfernen, so wäre die Ausgabe:
this is the first line
this is the second line
this is the third line
this is the first line
Das ist, was ich bisher geschrieben habe:
for(int i = 0; i < n; i++)
getline(infile,arr[i]);
for(int i=0; i<n; i++)
{
int j = i+1;
if(arr[i] == arr[j])
{
for(int k = i; k<n; k++)
arr[k] = arr[k+1];
n--;
}
}
dies ist die Ausgabe erhalte ich:
this is the first line
this is the second line
this is the second line
this is the third line
this is the first line
Wie behebt man das? P.S .: Es muss iterativ gelöst werden, weshalb ich es auf diese Weise versuche.
Was ist der Typ von 'arr'? –
Kennen Sie STL? –
Das richtige Werkzeug, um solche Probleme zu lösen, ist Ihr Debugger. Sie sollten Schritt für Schritt durch Ihren Code * gehen, bevor Sie auf Stack Overflow nachfragen. Für weitere Hilfe lesen Sie bitte [Wie kleine Programme zu debuggen (von Eric Lippert)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Zumindest sollten Sie Ihre Frage bearbeiten, um ein [minimales, vollständiges und verifizierbares] (http://stackoverflow.com/help/mcve) Beispiel einzufügen, das Ihr Problem zusammen mit den Beobachtungen, die Sie in der Debugger. –