我在這裏要做的是寫一個函數repeat
接受一個字符串和一個正整數n並返回該字符串重複n次,因此repeat("fho", 3)
將返回字符串「hohoho」。下面的測試程序運行,但不顯示結果或掛起。我試圖添加一個系統暫停,但沒有幫助。我缺少什麼?問題與中繼器:(
#include <string>
#include <iostream>
std::string repeat(const std::string &word, int times) {
std::string result ;
result.reserve(times*word.length()); // avoid repeated reallocation
for (int a = 0 ; a < times ; a++)
result += word ;
return result ;
}
int main() {
std::cout << repeat("Ha" , 5) << std::endl ;
return 0 ;
}
你嘗試,以及如何爲你預期的那樣,他們不起作用怎麼投入? – sarnold 2012-04-23 04:14:16
這個代碼將正常運行..在這裏看到:http://ideone.com/eISah – Naveen 2012-04-23 04:14:50
我敢打賭,你使用的是Windows。嘗試打開cmd.exe並從命令行調用您的程序,而不是雙擊它。這樣窗口在程序終止時不會消失。 – zmccord 2012-04-23 04:23:56