當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ wcstoll()用法及代碼示例


C /C++中的wcstoll()函數將wide-character字符串轉換為長整數。它設置指針指向最後一個字符之後的第一個字符。

用法:

long long wcstoll( const wchar_t* str, wchar_t** str_end, int base )

參數:該函數接受三個強製性參數,如下所述:


  • str: 它指定一個以整數開頭的寬字符串。
  • str_end:該函數將str_end值設置為下一個字符(在最後一個有效字符之後,如果有的話),否則它將指向NULL。
  • base: 指定底數,底數的值可以為{0,2,3,…,35,36}。

返回值:該函數返回轉換後的long long整數。它返回0,字符指向NULL。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate 
// the function wcstoll() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // wide-character type array string starting 
    // with integral 'L' 
    wchar_t string1[] = L"888geekforgeeks"; 
    wchar_t string2[] = L"999gfg"; 
  
    // End pointer will point to the characters 
    // after integer, according to the base 
    wchar_t* End; 
  
    // Initializing base 
    int b = 10;  
    int value; 
  
    value = wcstoll(string1, &End, b); 
    value = wcstoll(string2, &End, b); 
  
    // prints the whole input string 
    wcout << "String Value = " << string1 << "\n"; 
    wcout << "Long Long Int value = " << value << "\n"; 
  
    // prints the end string after the integer 
    wcout << "End String = " << End << "\n"; 
  
    wcout << "String Value = " << string2 << "\n"; 
    wcout << "Long Long Int Value = " << value << "\n"; 
    wcout << "End String = " << End << "\n"; 
    return 0; 
}
輸出:
String Value = 888geekforgeeks
Long Long Int value = 999
End String = gfg
String Value = 999gfg
Long Long Int Value = 999
End String = gfg

注意:底數的值可以是{0,2,3,…,35,36}。以2為底的一組有效數字是{0,1},以3為底的一組有效數字是{0,1,2},依此類推。對於從11到36的基數,有效數字包括字母。底數11的有效數字集為{0,1,…,9,A,a},底數12的有效數字為{0,1,…,9,A,a,B,b},依此類推。

示例2:具有不同基礎的函數

// C++ program to illustrate the function wcstoll() 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // End pointer, will point to the characters 
    // after integer, according to the base 
    wchar_t* End; 
  
    // wide-character type array string 
    wchar_t string[] = L"1356geekforgeeks"; 
  
    // Prints the long long integer provided with base 5 
    wcout << "Long Long Int with base5 = "
    << wcstoll(string, &End, 5) << "\n"; 
  
    wcout << "End String = " << End << "\n"; 
  
    // Prints the long long integer provided with base 12 
    wcout << "Long Long Int with base12 = " 
    << wcstoll(string, &End, 12) << "\n"; 
  
    wcout << "End String = " << End << "\n"; 
  
    // Prints the long long integer provided with base 36 
    wcout << "Long Long Int with base36 = "
    << wcstoll(string, &End, 36) << "\n"; 
  
    wcout << "End String = " << End << "\n"; 
  
    return 0; 
}
輸出:
Long Long Int with base5 = 8
End String = 56geekforgeeks
Long Long Int with base12 = 2226
End String = geekforgeeks
Long Long Int with base36 = 9223372036854775807
End String =


相關用法


注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wcstoll() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。