Convert a number to a string with specified length in C++
问题 I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492'). In other words, I need to add correct amount of leading zeros to the number. int n = 999; string str = some_function(n,6); //str = '000999' Is there a function like this in C++? 回答1: or using the stringstreams: #include <sstream> #include <iomanip> std::stringstream ss; ss