在C / C的wcstoimax()和wcstoumax()函数++工作原理strtoimax完全相同()和strtoumax()在C函数++而是用于一个宽字符串(wstring的)的内容转换为指定底座的整体数量。此函数在cinttypes头文件中定义。
句法:
uintmax_t wcstoumax(const wchar* wstr, wchar** end, int base);
intmax_t wcstoimax(const wchar* wstr, wchar** end, int base);
参数:该函数接受三个强制性参数,如下所述:
- wstr:指定由整数组成的宽字符串。
- end:指定对wchar *类型的对象的引用。最终的值由函数的最后一个有效数字字符后WSTR接下来的字符集。如果不使用此参数,则它也可以是空指针。
- base:指定数字基数(基数),该数字基数确定有效字符及其在字符串的解释
返回类型:函数返回两个值,如下所示:
- 如果发生有效转换,则该函数将转换后的整数返回为整数值。
- 如果无法执行有效的转换,则返回零值(0)
下面的程序说明了上述函数:
程序1:
// C++ program to illustrate the
// wstoimax() function
#include
using namespace std;
// Driver code
int main()
{
wstring str = L"geeksforgeeks";
intmax_t val = wcstoimax(str.c_str(), nullptr, 36);
wcout << str << " in base 36 is " << val << " in base 10\n\n";
wchar_t* end;
val = wcstoimax(str.c_str(), &end, 30);
// 'w' does not lies in base 30 so string
// beyond this cannot be converted
wcout << "Given String = " << str << endl;
wcout << "Number with base 30 in string " << val << " in base 10" << endl;
wcout << "End String points to " << end << endl;
return 0;
}
输出:
geeksforgeeks in base 36 is 9223372036854775807 in base 10
Given String = geeksforgeeks
Number with base 30 in string 8759741037015451228 in base 10
End String points to
程序2:
// C++ program to illustrate the
// wcstoumax() function
#include
using namespace std;
// Driver code
int main()
{
int base = 10;
// L is a wide string literal
wstring str = L"12345abcd";
wchar_t* end;
uintmax_t num;
num = wcstoumax(str.c_str(), &end, base);
wcout << "Given String = " << str << endl;
wcout << "Value stored in num " << num << endl;
if (*end) {
wcout << "End string points to " << end << endl
<< endl;
}
else {
wcout << "Null pointer" << endl
<< endl;
}
// in this case no numeric character is there
// so the function returns 0
base = 10;
wstring str2 = L"abcd";
wcout << "Given String = " << str2 << endl;
num = wcstoumax(str2.c_str(), &end, base);
wcout << "Number with base 10 in string " << num << endl;
if (*end) {
wcout << "End String points to " << end;
}
else {
wcout << "Null pointer";
}
return 0;
}
输出:
Given String = 12345abcd
Value stored in num 12345
End string points to abcd
Given String = abcd
Number with base 10 in string 0
End String points to abcd
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。