阅读量:77
- 将字符串转换为长整型数值:
#include
#include
int main() {
const char* str = "1234567890";
long long num = atoll(str);
std::cout << "Converted number: " << num class="hljs-keyword">return 0;
}
- 处理带符号的字符串:
#include
#include
int main() {
const char* str = "-9876543210";
long long num = atoll(str);
std::cout << "Converted number: " << num class="hljs-keyword">return 0;
}
- 处理错误的输入情况:
#include
#include
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 class="hljs-keyword">else {
std::cout << "Converted number: " << num class="hljs-keyword">return 0;
}