C++中int,float,string,char*的转换(待续)

//float转string

char a[100];
float b = 1.234;
sprintf(a, "%f", b);
string result(a);

 

//int转string,利用sprintf
int main(){
    int mm = 2414;
    char *ch = new char; //或者char ch[256];
    string tmp;
    sprintf(ch,"%d",mm); //sprintf(ch, "%f", mm)将float转string
    tmp = ch;
    cout << tmp + "124124";
    cin >> mm;
}

 

//利用c_str将sting 转为 const char*, 一般不会要求将const char*转为char*,如果要转,先考虑函数设计问题

string s = "test";
const char* = s.c_str();

 

//char* 或者char数组转string,直接利用构造函数char *offpath = new char[100];
    ifstream inff("C:\\model.txt",ios::in);
    while(!inff.eof()){
        inff.getline(offpath,100);
        string stmp(offpath);
    }

 

 

posted @ 2013-12-14 21:54  Key_Ky  阅读(2313)  评论(0编辑  收藏  举报