C++中atoll函数的使用示例有哪些

avatar
作者
筋斗云
阅读量:0

  1. 将字符串转换为长整型数值:
#include <iostream> #include <cstdlib>  int main() {     const char* str = "1234567890";     long long num = atoll(str);          std::cout << "Converted number: " << num << std::endl;          return 0; } 
  1. 处理带符号的字符串:
#include <iostream> #include <cstdlib>  int main() {     const char* str = "-9876543210";     long long num = atoll(str);          std::cout << "Converted number: " << num << std::endl;          return 0; } 
  1. 处理错误的输入情况:
#include <iostream> #include <cstdlib>  int main() {     const char* str = "abcd1234";     char* endptr;     long long num = atoll(str);          if (*endptr != '\0') {         std::cerr << "Error converting string to long long" << std::endl;     } else {         std::cout << "Converted number: " << num << std::endl;     }          return 0; } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!