我有一個字符串,我需要給它添加一個數字,即一個int。像:我如何添加一個int到一個字符串
string number1 = ("dfg");
int number2 = 123;
number1 += number2;
這是我的代碼:
name = root_enter; // pull name from another string.
size_t sz;
sz = name.size(); //find the size of the string.
name.resize (sz + 5, account); // add the account number.
cout << name; //test the string.
這個工程...有點,但我只得到了 「名* 88888」 和...我不知道爲什麼。 我只需要一種方法來將int的值添加到字符串的末尾
「我不知道爲什麼」。 「resize」的第二個參數是一個char,並且resize重複使用它來填充它在字符串末尾創建的任何額外空間。在你的情況下'account'等於56(mod 256),所以你已經傳遞了字符'8'。 – 2010-03-07 02:22:59