2015-04-12 86 views

回答

1

如果你的意思是你必须要格式化成字符串一些数值变量,使用一个字符串流:

std::stringstream ss; 
ss << "1" << lapcounter << ":" << seconds"; 

现在你可以从提取的字符串:

std::string s = ss.str(); 

,如果你真的想出于某种原因字符数组(我敢肯定,你不这样做)

char const * cs = s.c_str(); 
1

使用sprintfsnprintf。此功能的作用类似于printf,但不是标准输出,输出将转到您指定的字符数组。例如:

char buffer[32]; 
snprintf(buffer, sizeof(buffer), "1%d:%d", lapcounter, seconds); 
0

to_string这样使用:

#include <iostream> 
#include <string> 

int main() 
{ 
    int lapcounter = 23; 
    std::string str("1"); 
    str.append(std::to_string(lapcounter)); 
    str.append(":seconds"); 
    std::cout << str << std::endl; 
} 

打印

123:seconds 

如果你真的需要一个字符数组你从ss.c_str()

相关问题