C ++ strtoll () - Libreria standard C ++

La funzione strtoll () in C ++ interpreta il contenuto di una stringa come un numero intero della base specificata e restituisce il suo valore come un int long long.

Questa funzione imposta anche un puntatore in modo che punti al primo carattere dopo l'ultimo carattere valido della stringa, se presente, altrimenti il ​​puntatore viene impostato su null.

 Per la base 10 e la stringa "201xz $" Parte numerica valida -> 201 Primo carattere dopo la parte numerica valida -> x

prototipo strtoll () (come dallo standard C ++ 11)

 long long int strtoll (const char * str, char ** end, int base);

La funzione strtoll () accetta string, un puntatore a carattere e un valore intero - base come parametro, interpreta il contenuto di stringa come un numero intero della base data e restituisce un valore int lungo e lungo.

Questa funzione è definita nel file di intestazione.

strtoll () Parametri

  • str: una stringa con la rappresentazione di un numero intero.
  • end: riferimento a un oggetto già allocato di tipo char *. Il valore di end è impostato dalla funzione sul carattere successivo in str dopo l'ultimo carattere valido. Questo parametro può anche essere un puntatore nullo, nel qual caso non viene utilizzato.
  • base: la base del valore integrale. Il set di valori validi per base è (0, 2, 3,…, 35, 36).

strtoll () Restituisce il valore

La funzione strtoll () restituisce:

  • un valore int lungo lungo (che viene convertito dalla stringa).
  • 0 se non è stato possibile eseguire una conversione valida.

Esempio 1: come funziona la funzione strtoll ()?

 #include #include #include using namespace std; int main() ( int base = 10; char numberString() = "13.5ab_1x"; char *end; long long int number; number = strtoll(numberString, &end, base); cout << "String value = " << numberString << endl; cout << "Long long int value = " << number << endl; cout << "End String = " << end << endl; strcpy(numberString, "13"); cout << "String value = " << numberString << endl; number = strtoll(numberString, &end, base); cout << "Long long int value = " << number << endl; if (*end) ( cout << end; ) else ( cout << "Null pointer"; ) return 0; )

Quando esegui il programma, l'output sarà:

 Valore stringa = 13.5ab_1x Valore int lungo lungo = 13 Stringa finale = .5ab_1x Valore stringa = 13 Valore int lungo lungo = 13 Puntatore nullo

Un valore intero valido per la funzione strtoll () è costituito da:

  • Un segno + o - opzionale.
  • Un prefisso 0 per base ottale (si applica solo quando base = 8 o 0).
  • Un prefisso 0x o 0X per base esadecimale (si applica solo quando base = 16 o 0).
  • Una sequenza di cifre e / o alfabeti (se la base è maggiore di 10).

I valori validi per la base del parametro sono (0, 2, 3,…, 35, 36). Un insieme di cifre valide per la base 2 è (0, 1), per la base 3 è (0, 1, 2) e così via.

Per le basi che vanno da 11 a 36, ​​le cifre valide includono gli alfabeti. L'insieme di cifre valide per la base 11 è (0, 1,…, 9, A, a), per la base 12 è (0, 1,…, 9, A, a, B, b) e così via.

Esempio 2: funzione strtoll () con basi diverse

 #include #include using namespace std; int main() ( char *end; cout << "23ajz" << " to Long Long Int with base-7 = " << strtoll("23ajz", &end, 7) << endl; cout << "End String = " << end << endl << endl; cout << "23ajz" << " to Long Long Int with base-20 = " << strtoll("23ajz", &end, 20) << endl; cout << "End String = " << end << endl << endl; cout << "23ajz" << " to Long Long Int with base-36 = " << strtoll("23ajz", &end, 36) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 23ajz a Long Long Int con base-7 = 17 End String = ajz 23ajz a Long Long Int con base-20 = 17419 End String = z 23ajz a Long Long Int con base-36 = 3512879 End String =

La funzione strtoll () ignora tutti i caratteri di spazio vuoto iniziali finché non viene trovato il carattere principale diverso da spazi.

In generale, un argomento intero valido per la funzione strtoll () ha la seguente forma:

 (spazi) (- | +) (0 | 0x) (caratteri alfanumerici)

Quindi, a partire da questo carattere, prende il maggior numero di caratteri possibile che forma una rappresentazione intera valida e li converte in un valore int lungo e lungo. Tutto ciò che resta della stringa dopo l'ultimo carattere valido viene ignorato e non ha alcun effetto sul risultato.

Esempio 3: funzione strtoll () per spazi bianchi iniziali e conversione non valida

 #include #include using namespace std; int main() ( char *end; cout << " 25axbz" << " to Long Long Int with base-11 = " << strtoll(" 25axbz", &end, 11) << endl; cout << "End String = " << end << endl << endl; cout << " 110bcd" << " to Long Long Int with base-2 = " << strtoll(" 110bcd", &end, 2) << endl; cout << "End String = " << end << endl << endl; cout << "ax110.97" << " to Long Long Int with base-10 = " << strtoll("ax110.97", &end, 10) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 25axbz a Long Long Int con base-11 = 307 End String = xbz Da 110bcd a Long Long Int con base-2 = 6 End String = bcd ax110.97 a Long Long Int con base-10 = 0 End String = ax110.97

Se la base è 0, la base numerica viene determinata automaticamente guardando il formato della stringa. Se il prefisso è 0, la base è ottale (8). Se il prefisso è 0x o 0X, la base è esadecimale (16), altrimenti la base è decimale (10).

Esempio 4: funzione strtoll () con base 0

 #include #include using namespace std; int main() ( char *end; /* octal base */ cout << "025x" << " to Long Long Int with base-0 = " << strtoll("025x", &end, 0) << endl; cout << "End String = " << end << endl << endl; /* hexadecimal base */ cout << "0xf1x" << " to Long Long Int with base-0 = " << strtoll("0xf1x", &end, 0) << endl; cout << "End String = " << end << endl << endl; /* decimal base */ cout << "15ab" << " to Long Long Int with base-0 = " << strtoll("15ab", &end, 0) << endl; cout << "End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 Da 025x a Long Long Int con base-0 = 21 End String = x 0xf1x a Long Long Int con base-0 = 241 End String = x 15ab a Long Long Int con base-0 = 15 End String = ab

Articoli interessanti...