这个 strotimax() C++中的函数将字符串的内容解释为指定基的整数,并将其值作为最大宽度整数返回。此函数还设置一个结束指针,该指针指向字符串最后一个有效数字字符后的第一个字符,如果没有这样的字符,则指针设置为null。此函数在中定义 cinttypes 头文件。 语法:
null
intmax_t strtoimax(const char* str, char** end, int base)
参数:
- str :指定由整数组成的字符串。
- 终止 :它是对char*类型的对象的引用。end的值由函数设置为str中最后一个有效数字字符之后的下一个字符。如果不使用,此参数也可以是空指针。
- 基数: 它代表t
确定字符串中有效字符及其解释的基数 返回类型 函数的作用是:返回以下两个值:
- 如果发生有效转换,则函数将转换后的整数作为整数值返回。
- 如果无法执行有效转换,则返回零值(0)
以下程序说明了上述功能: 项目1:
CPP
// C++ program to illustrate the // strtoimax() function #include <bits/stdc++.h> using namespace std; // Driver code int main() { int base = 10; char str[] = "1000xyz" ; char * end; intmax_t num; num = strtoimax(str, &end, base); cout << "Given String = " << str << endl; cout << "Number with base 10 in string " << num << endl; cout << "End String points to " << end << endl << endl; // in this case the end pointer points to null // here base change to char16 base = 16; strcpy (str, "ff" ); cout << "Given String = " << str << endl; num = strtoimax(str, &end, base); cout << "Number with base 16 in string " << num << endl; if (*end) { cout << end; } else { cout << "Null pointer" ; } return 0; } |
输出:
Given String = 1000xyzNumber with base 10 in string 1000End String points to xyzGiven String = ffNumber with base 16 in string 255Null pointer
项目2: 在不同的基础上转换多个值的程序
CPP
// C++ program to illustrate the // strtoimax() function #include <bits/stdc++.h> using namespace std; // Driver code int main() { char str[] = "10 50 f 100 " ; char * end; intmax_t a, b, c, d; // at base 10 a = strtoimax(str, &end, 10); // at base 8 b = strtoimax(end, &end, 8); // at base 16 c = strtoimax(end, &end, 16); // at base 2 d = strtoimax(end, &end, 2); cout << "The decimal equivalents of all numbers are " ; cout << a << endl << b << endl << c << endl << d; return 0; } |
输出:
The decimal equivalents of all numbers are 1040154
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END