Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++]stl , string e \r \n

    ciao.
    Devo eliminare da una stringa i caratteri \n e \r.
    ho provato :

    std::remove(m_strCurrStr.begin(),m_strCurrStr.end( ),'\r');
    std::remove(m_strCurrStr.begin(),m_strCurrStr.end( ),'\n');

    ma i da quest errore:

    error C2660: 'remove' : function does not take 3 arguments

    eppure dovrebbero essere proprio 3 argomenti la funzione remove!
    come mai?
    grazie.

  2. #2
    Sicuro di aver incluso <algorithm>? Ricordati che poi devi cancellare dalla stringa tutti i caratteri dall'iteratore che ti restituisce remove alla fine della stringa.
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    grazie Mitaly , ho incluso algorithm e tutto ok .
    non riesco pero ' a capire di preciso cosa fa questra funzione:

    m_strCurrStr.erase(std::remove( m_strCurrStr.begin(),m_strCurrStr.end(),'\n'), m_strCurrStr.end());

    questa è la definizione di remove:
    Eliminates a specified value from a given range without disturbing the order of the remaining elements and returning the end of a new range free of the specified value.
    questo il valore di ritorno:

    Return Value
    A forward iterator addressing the new end position of the modified range, one past the final element of the remnant sequence free of the specified value.
    1)Erase
    Removes an element or a range of elements in a string from specified positions.

    Return Value
    For the first two member functions, an iterator addressing the first character after the last character removed by the member function. For the third member function, a reference to the string object from which the elements have been erased.
    non riesco ancora a enrare nel mondo degli iterator, cos è alla fine un iterator?un range? un puntatore? non capisco cosa fa la funzioneche ho postato:

    m_strCurrStr.erase(std::remove( m_strCurrStr.begin(),m_strCurrStr.end(),'\n'), m_strCurrStr.end());

    questo:
    std::remove( m_strCurrStr.begin(),m_strCurrStr.end(),'\n') ritorna un iterator o piu' di un iterator?
    se ci sono piu' '\n' ritorna vari range , quindi vari iterator?non capisco.

    std::remove( iteratorbegin,iterator end)
    la stessa cosa di sopra , cosa vuole dire eliminare un iterator?
    grazie , e scusate se posto qui ma in google non sono riuscito a capire bene il meccanismo.
    Ciao.

  4. #4
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    un iteratore grossomodo è un puntatore ad un elemento di un contenitore

    ci sono vari tipi di iteratori cmq..

    erase ritorna un iteratore che va all'indietro (reverse iterator) che punta al nuovo ultimo 8che potrebbe essere benissimo come il vecchio ma la erase non lo può sapere)

    credo che se ci sono più '\n' gli elimini tutti.. ma non ne ho la certezza..

    cmq erase ritorna sempre 1 iteratore

  5. #5
    Un iteratore è una generalizzazione di un puntatore
    Originariamente inviato da Stoicenko
    credo che se ci sono più '\n' gli elimini tutti..
    È così; il remove di fatto "ammucchia" tutti gli elementi da tenere all'inizio del container, e restituisce un iteratore che punta all'elemento subito dopo l'ultimo "buono". A quel punto erase può cancellare tutti gli elementi da questo all'attuale ultimo nel contenitore.
    Amaro C++, il gusto pieno dell'undefined behavior.

  6. #6
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    ma è possibile eliminare anche un insieme di caratteri?
    ad es io vorrei eliminare "*.*".
    grazie.

  7. #7
    Utente di HTML.it L'avatar di KrOW
    Registrato dal
    Feb 2009
    Messaggi
    281
    Ciao . . . Prova così:
    codice:
    string myString = "ciao*.* a tut*.*ti";
    string substringToFind = "*.*";
    string::size_type indexOfSubstring = myString.find( substringToFind );
    while( indexOfSubstring != string::npos )
    {
    myString.erase( indexOfSubstring, substringToFind.size() );
    indexOfSubstring = myString.find( substringToFind );
    }
    C++ 4ever
    496e2062696e6172696f206e6f6e2063692061767265737469 206e656d6d656e6f2020726f7661746f203a29

  8. #8
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.