|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2001
Messaggi: 782
|
[C++] Che funzione per leggere solo una parte di stringa?
Le cose stanno così:
una serie di valori interi vengono letti su di una stringa (sono separati da uno spazio). Necessito di memorizzarli su delle variabili locali intere per farci dei conti. Per la conversione stringa->intero pensavo di usare atoi, passando come argomento non l'intera stringa ma solo la parte relativa ad un singolo intero. Come parzializzo la selezione di una stringa? Nel senso la stringa tot mi interessa dal carattere numero 9 al carattere numero 11. Grazie in anticipo per la risposta
__________________
HP DV6-6170sl - Play 4 - PSVITA - Oneplus One |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jun 2008
Città: Treviso
Messaggi: 1026
|
puoi scorrere la stringa con un iteratore:
string str ("12 15 48 456 45 15 88"); string::iterator it; for ( it=str.begin() ; it < str.end(); it++ ) { ... } e per estrarre i singoli token usi questo metodo: http://www.cplusplus.com/reference/s...ng/substr.html |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jun 2001
Messaggi: 782
|
Grazie mille ma ho fatto con strtok() !
__________________
HP DV6-6170sl - Play 4 - PSVITA - Oneplus One |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
un' altra alternativa e' fare qualcosa come il seguente usando lo stream da stringa:
Codice:
#include <string>
#include <sstream>
using namespace std;
/* ... */
void foo()
{
int a,b,c,d,e;
string s = "1 2 3 5 5";
istringstream input( s.c_str() );
input >> a >> b >> c >> d >> e;
}
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jun 2001
Messaggi: 782
|
Molto carino ed elegante come metodo. Grazie!
__________________
HP DV6-6170sl - Play 4 - PSVITA - Oneplus One |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:57.



















