C ++ wcstoll () - Libreria standard C ++

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

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

 Per la base 10 e la stringa larga L "31ÛÕÕ" Parte numerica valida -> 31 Primo carattere dopo la parte numerica valida -> Û

È definito nel file di intestazione.

prototipo di wcstoll ()

 wcstoll lungo lungo (const wchar_t * str, wchar_t ** str_end, int base);

La funzione wcstoll () accetta una stringa ampia str, un puntatore al carattere ampio str_end e un valore intero - base come parametro.

Quindi interpreta il contenuto di una stringa ampia come un numero intero della base data e restituisce un valore int lungo lungo.

Parametri wcstoll ()

  • str: una stringa ampia con la rappresentazione di un numero intero.
  • str_end: puntatore a un puntatore a un carattere largo. Il valore di str_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 la base è (0, 2, 3,…, 35, 36).

wcstoll () Restituisce il valore

La funzione wcstoll () 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 wcstoll ()?

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t str1() = L"41u0166u0124xx"; wchar_t str2() = L"127"; wchar_t *end; long long value; int base = 10; value = wcstoll(str1, &end, base); wcout << L"String value = " << str1 << endl; wcout << L"Long Long Int value = " << value << endl; wcout << L"End String = " << end << endl; value = wcstoll(str2, &end, base); wcout << L"String value = " << str2 << endl; wcout << L"Long Long Int value = " << value << endl; wcout << L"End String = " << end << endl; return 0; )

Quando esegui il programma, l'output sarà:

 Valore stringa = 41ŦĤxx Long Long Int value = 41 End String = ŦĤxx String value = 127 Long Long Int value = 127 End String =

Un valore intero valido per la funzione wcstoll () è 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 wcstoll () con basi diverse

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wchar_t str() = L"311bzu03feu03ff"; wcout << str << L" to Long Long Int with base-5 = " << wcstoll(str, &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Long Long Int with base-12 = " << wcstoll(str, &end, 12) << endl; wcout << L"End String = " << end << endl << endl; wcout << str << L" to Long Long Int with base-36 = " << wcstoll(str, &end, 36) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 311bzϾϿ a Long Long Int con base-5 = 81 End String = bzϾϿ 311bzϾϿ a Long Long Int con base-12 = 5351 End String = zϾϿ 311bzϾϿ a Long Long Int con base-36 = 5087231 End String = ϾϿ

La funzione wcstoll () ignora tutti i caratteri di spazio vuoto iniziali fino a quando non viene trovato il carattere principale diverso da spazi.

In generale, un argomento intero valido per la funzione wcstoll () 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.

Tutto ciò che resta della stringa dopo l'ultimo carattere valido viene ignorato e non ha alcun effetto sul risultato.

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

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wcout << L" 205u03e2x to Long Long Int with base-5 = " << wcstoll(L" 205u03e2x", &end, 5) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"xu019cz201 to Long Long Int with base-12 = " << wcstoll(L"xu019cz201", &end, 12) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 205Ϣx a Long Long Int con base-5 = 10 End String = 5Ϣx xƜz201 a Long Long Int con base-12 = 0 End String = xƜz201

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 wcstoll () con base 0

 #include #include #include using namespace std; int main() ( setlocale(LC_ALL, "en_US.UTF-8"); wchar_t *end; wcout << L"0539u1e84 to Long Long Int with base-0 = " << wcstoll(L"0539u1e84", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"0xa31u05e2 to Long Long Int with base-0 = " << wcstoll(L"0xa31u05e2", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; wcout << L"119xu060f to Long Long Int with base-0 = " << wcstoll(L"119xu060f", &end, 0) << endl; wcout << L"End String = " << end << endl << endl; return 0; )

Quando esegui il programma, l'output sarà:

 0539Ẅ a Long Long Int con base-0 = 43 End String = 9Ẅ 0xa31 ע a Long Long Int con base-0 = 2609 End String = ע 119x ؏ a Long Long Int con base-0 = 119 End String = x ؏

Articoli interessanti...