溫馨提示×

C++中atoll函數(shù)的使用示例有哪些

c++
小樊
84
2024-08-17 17:16:36
欄目: 編程語言

  1. 將字符串轉(zhuǎn)換為長整型數(shù)值:
#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;
}

0