atoi、strtol、stoi、atol、strtoll、strtoul、strtoull、atof、strtof、strtod、sscanf、sprintf、snprintf、strtok

字符串转整数

atoi、strtol 和 std::stoi 都是用于将字符串转换为整数的函数,但它们在功能和使用方面有一些不同之处:

atoi(ASCII to Integer):
    是 C 标准库中的函数,不支持错误处理。
    当解析失败时(例如,遇到非数字字符),会返回 0。
    不提供机制来判断解析是否成功或失败。
<stdlib.h> or <cstlib>

const char *str = "12345";
int value = atoi(str); // value = 12345

const char *nonNumeric = "abc";
int nonNumericValue = atoi(nonNumeric); // nonNumericValue = 0
strtol(String to Long Integer):
    是 C 标准库中的函数,支持错误处理和更多的控制。
    可以指定基数(例如,十进制、十六进制等)。
    提供了额外的指针参数,可用于检查解析是否成功,以及存储未解析部分的指针。
    推荐在 C 代码中使用,以便进行更严格的错误处理。
<stdlib.h> or <cstlib>

const char *str = "12345";
char *endptr;
long value = strtol(str, &endptr, 10); // value = 12345, endptr 指向字符串的下一个字符

const char *nonNumeric = "abc";
long nonNumericValue = strtol(nonNumeric, &endptr, 10); // nonNumericValue = 0, endptr 指向 'a'
std::stoi(String to Integer):
    是 C++ 标准库中的函数,支持错误处理和更多的控制。
    是 strtol 的 C++ 版本,返回 int 类型。
    推荐在 C++ 代码中使用,以便使用更现代的 C++ 特性。
std::string str = "12345";
size_t pos;
int value = std::stoi(str, &pos); // value = 12345, pos = 5
int val = std::stoi(str); // val = 12345

// std::string nonNumeric = "abc";
// int nonNumericValue = std::stoi(nonNumeric, &pos); // 抛出 std::invalid_argument 异常

atol(ASCII to Long):将字符串转换为长整型。

const char *str = "123456";
long value = atol(str); // value = 123456

strtoll(String to Long Long):将字符串转换为长长整型。

const char *str = "123456";
char *endptr;
long long value = strtoll(str, &endptr, 10); // value = 123456, endptr 指向字符串的下一个字符

strtoul(String to Unsigned Long):将字符串转换为无符号长整型。

const char *str = "123456";
char *endptr;
unsigned long value = strtoul(str, &endptr, 10); // value = 123456, endptr 指向字符串的下一个字符

strtoull(String to Unsigned Long Long):将字符串转换为无符号长长整型。

    const char *str = "123456";
    char *endptr;
    unsigned long long value = strtoull(str, &endptr, 10); // value = 123456, endptr 指向字符串的下一个字符

字符串转浮点数

atof(ASCII to Float):将字符串转换为双精度浮点数。

<stdlib.h> or <cstlib>

const char *str = "3.14";
double value = atof(str); // value = 3.14

strtof(String to Float):将字符串转换为单精度浮点数。

<stdlib.h> or <cstlib>

const char *str = "3.14";
char *endptr;
float value = strtof(str, &endptr); // value = 3.14, endptr 指向字符串的下一个字符

strtod(String to Double):将字符串转换为双精度浮点数。

<stdlib.h> or <cstlib>

const char *str = "3.14";
char *endptr;
double value = strtod(str, &endptr); // value = 3.14, endptr 指向字符串的下一个字符

字符串格式化

sscanf(String Scan Format):从字符串中根据格式化字符串提取数据,类似于 scanf。

char input[] = "42 3.14 Hello";
int num;
float floating;
char word[20];
sscanf(input, "%d %f %s", &num, &floating, word);

sprintf(String Print Format):将格式化的数据输出到字符串中,类似于 printf。

char buffer[100];
int num = 42;
sprintf(buffer, "The number is: %d", num); // buffer = "The number is: 42"

snprintf(Safe String Print Format):与 sprintf 类似,但可以指定输出字符串的最大长度,防止缓冲区溢出。

char buffer[20];
int num = 123456789;
snprintf(buffer, sizeof(buffer), "Number: %d", num); // buffer = "Number: 123456789"

strtok(String Tokenize):将字符串分割成多个标记(token)。

char str[] = "apple,banana,cherry";
char *token = strtok(str, ",");
while (token != NULL) {
    printf("Token: %s\n", token);
    token = strtok(NULL, ",");
}
// Output:
// Token: apple
// Token: banana
// Token: cherry
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邱邱玩编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值