当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。