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());
}

 

posted on 2014-10-23 18:01  蒂其之死  阅读(213)  评论(0编辑  收藏  举报

导航