zoukankan      html  css  js  c++  java
  • atoi函数——将字符串转换为整数

    atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势。

    还有一个atof,就是换成浮点数,实质上是一样的。

    例子:

    #include<cstdlib>

    #include<iostream>

    using namespace std;

    int main(void)
    {
        int n;
        char *str = "12345.67";
        n = atoi(str);
        printf("n=%d ",n);
        return 0;
    }
    说明:atoi函数里只有一个参数,就是字符串的地址。atoi在转换时,从字符串头开始搜索,从第一个数字开始转换,到之后第一个非数字结束。
    还是很好用的,而且在其他非Windows系统也可以使用。但另一个函数没有那么幸运了。。。
    itoa:将整数换成字符串。
    例子:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
    {
    int number=123456;
    char string[25];
    itoa(number,string,10);
    printf("integer=%d string=%s ",number,string);
    return0;
    }
    说明:
    char*itoa(int value,char*string,int radix);
    int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等。
    在评测系统不好用,所以平时还是不要用了。。。
     
    只想找一个不会伤害我的人
  • 相关阅读:
    Balanced Binary Tree
    Swap Nodes in Pairs
    Reverse Nodes in k-Group
    Reverse Linked List II
    Remove Nth Node From End of List
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Partition List
    Merge Two Sorted Lists
    【Yii2.0】1.2 Apache检查配置文件语法
  • 原文地址:https://www.cnblogs.com/DukeLv/p/7856052.html
Copyright © 2011-2022 走看看