zoukankan      html  css  js  c++  java
  • std::string和int类型的相互转换(C/C++)

    字符串和数值之前转换,是一个经常碰到的类型转换。

    之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考:

    优缺点:C++的stringstream智能扩展,不用考虑字符数组长度等..;但C的性能高

    有性能要求的推荐用C实现版本。

    上测试实例:

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <string>
     4 #include <sstream>
     5 
     6 using namespace std;
     7 int main()
     8 {
     9     //C++ method
    10     {
    11         //int -- string 
    12         stringstream stream;
    13         
    14         
    15         stream.clear(); //在进行多次转换前,必须清除stream
    16         int iValue = 1000;
    17         string sResult;
    18         stream << iValue; //将int输入流
    19         stream >> sResult; //从stream中抽取前面插入的int值
    20         cout << sResult << endl; // print the string
    21         
    22         //string -- int 
    23         stream.clear(); //在进行多次转换前,必须清除stream
    24         string sValue="13579";
    25         int iResult;
    26         stream<< sValue; //插入字符串
    27         stream >> iResult; //转换成int
    28         cout << iResult << endl;
    29     }
    30     
    31     //C method
    32     {
    33         //int -- string(C) 1
    34         int iValueC=19000;
    35         char cArray[10]="";//需要通过字符数组中转
    36         string sResultC;
    37         //itoa由于它不是标准C语言函数,不能在所有的编译器中使用,这里用标准的sprintf
    38         sprintf(cArray, "%d", iValueC);
    39         sResultC=cArray; 
    40         cout<<sResultC<<endl;
    41         
    42         //int -- string(C) 2
    43         int iValueC2=19001;
    44         string sResultC2;
    45         //这里是网上找到一个比较厉害的itoa  >>  后文附实现
    46         sResultC2=itoa(iValueC2); 
    47         cout<<sResultC2<<endl;
    48         
    49         //string -- int(C) 
    50         string  sValueC="24680";
    51         int iResultC = atoi(sValueC.c_str());
    52         cout<<iResultC+1<<endl;
    53     }
    54     
    55     return 0;
    56 }
    test.cpp

    如下是网上找到的一片比较经典的itoa实现!

     1 #define INT_DIGITS 19        /* enough for 64 bit integer */
     2 
     3 char *itoa(int i)
     4 {
     5     /* Room for INT_DIGITS digits, - and '' */
     6     static char buf[INT_DIGITS + 2];
     7     char *p = buf + INT_DIGITS + 1;    /* points to terminating '' */
     8     if (i >= 0) {
     9         do {
    10             *--p = '0' + (i % 10);
    11             i /= 10;
    12         } while (i != 0);
    13         return p;
    14     }
    15     else {            /* i < 0 */
    16         do {
    17             *--p = '0' - (i % 10);
    18             i /= 10;
    19         } while (i != 0);
    20         *--p = '-';
    21     }
    22     return p;
    23 }
    itoa.c
  • 相关阅读:
    redis常用数据类型与命令
    bcb6重启应用程序
    MySQL 关联查询  外连接 { LEFT| RIGHT } JOIN
    MySQL 关联查询 内连接
    MySql子查询
    MySql单表查询
    表级操作语句
    库级操作语句
    14.正则表达式、re模块、元字符
    13.生成器、迭代器、 模块、包和包管理
  • 原文地址:https://www.cnblogs.com/book-gary/p/Basic_programming_0001.html
Copyright © 2011-2022 走看看