首页 > 代码库 > c++ int string互转

c++ int string互转

1.

    if(i<9){            monthNumber = "0";            itoa(i+1,iCharArray,10);            monthNumber += iCharArray;        } else {            itoa(i+1,iCharArray,10);            monthNumber = iCharArray;        }    

2.

int转string
int n = 0;
std::stringstream ss;
std::string str;
ss<<n;
ss>>str;
string转int
std::string str = "123";
int n = atoi(str.c_str());

#include "stdafx.h"#include <string>#include <sstream>using namespace std;void main(){    // int 转 string    stringstream ss;    int n = 123;    string str;    ss<<n;    ss>>str;    // string 转 int    str = "456";    n = atoi(str.c_str());}

 

c++ int string互转