2015-04-12 87 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()

相關問題