Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C++] Ricerca inversa in una stringa

    Desidero trovare la posizione del primo carattere che non equivalga ad uno qualsiasi dei seguenti:
    codice:
    ' ' - '\t' - '\n'
    e che precede la posizione che passerò come input:

    Per fare un esempio: se ho una stringa contenente i caratteri:
    codice:
    string str("ciao foo=m");
    e la posizione è:
    codice:
    size_t pos = 8;
    Vorrei poter disporre di un metodo che data la mia stringa e la mia posizione mi restituisca la posizione del carattere ' ' (spazio - successivo alla parola "ciao").

    Esiste un metodo del genere nella libreria standard? In caso negativo, come posso implementarlo?
    Grazie.
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Hai provato a vedere i metodi find, find_first_of, find_last_of di std::string?
    http://www.cplusplus.com/reference/string/string/

    Io comunque opterei per un Tokenizer.
    Qui c'è una possibile versione (su e giù nella pagina ce ne sono diverse altre per tutti i gusti).
    http://stackoverflow.com/questions/5...-c/53862#53862
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    I metodi elencati forniscono una ricerca verso la fine della stringa.
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Se non ho capito male devi partire da: m e risalire la stringa fino a trovare lo spazio. Giusto?
    Se è così usando std::find_if e un puoi ricavare l'iteratore corrispondente al carattere che stai cercando, poi giocando con gli iteratori puoi ricavare le altre parti della stringa o la posizione numerica corrispondete a quell'iteratore.

    (Metto il trattino per evidenziare).

    codice:
    // predicato per la ricerca del carattere o caratteri 
    class Pred {
    	std::string chars;
    	public:
    		Pred(const std::string& str) : chars(str) {}
    		Pred(char c) { chars.push_back(c); }
    		bool operator()(char c) {
    			return chars.find(c) != std::string::npos;
    		}
    };
    
    ...
    
    // versione C++98
    		string prova = "ciao-fooooo=m";
    
    		typedef string::iterator str_iterator;
    		typedef string::reverse_iterator str_riterator;
    
    		str_riterator rit = find_if(prova.rbegin(),prova.rend(), Pred("-"));
    		std::size_t len = std::distance(rit,prova.rend());
    		str_iterator it = prova.begin();
    		std::advance(it,len);
    		std::string res(it,prova.end());
                    cout << res << endl;
    
    // Versione C++11 
    
    		string prova = "ciao-fooooo=m";
    		auto rit = find_if(prova.rbegin(),prova.rend(), Pred("-"));
    		auto len = std::distance(rit,prova.rend());
    		auto it = prova.begin();
    		std::advance(it,len);
    		std::string res(it,prova.end());
    		cout << res << endl;
    In Pred() inserisci i caratteri che vuoi escludere, find_if restituisce l'iteratore corrispondente al primo carattere della stringa di Pred() incontrato; std::distance() restituisce la posizione numerica del reverse_iterator e quindi del trattino (in questo caso partendo da c di ciao).
    Il resto serve per convertire da reverse iterator a iterator.
    Se hai un compilatore che supporta il C++11 (in parte almeno) puoi usare la seconda versione più corta.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Grazie, davvero gentile ed esaustivo.
    Fracty - The Fractal Generator



    If you cannot choose a concise name that expresses what the method does, it is possible that your method is attempting to perform too many diverse tasks.

  6. #6
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Prego.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

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 © 2025 vBulletin Solutions, Inc. All rights reserved.