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

 

 

相关文章:

  • 2021-05-30
  • 2021-07-06
  • 2021-12-04
猜你喜欢
  • 2021-11-08
  • 2022-12-23
  • 2021-10-24
  • 2021-12-25
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
相关资源
相似解决方案