我正在爲計算機科學課程中的項目編寫代碼,並正在測試我的算法以查看它是否有效以及它的速度。我知道該算法的工作原理,因爲當我在Visual Studio 2013中啓動程序時,我會得到正確的輸出。但是,當我從命令行的Visual Studio項目文件夾或從Windows資源管理器啓動.exe時,前兩個cout語句顯示正確,但for循環中的cout語句完全不顯示。當我在Visual Studio之外啓動.exe時,只會發生這種情況。這不是一個大問題,但我想知道這裏發生了什麼。謝謝。從命令行啓動時,在for循環中不顯示C++ cout語句
這裏爲int main()的(第2個COUTS工作和別人不一樣):
int main() {
// declare input stream for reading dctnryWords.txt
ifstream inFile;
// create a pointer to memory in the heap
// where each word in the dictionary will be stored
string* words = new string[DICTIONARY_SIZE];
// create a vector of forward_lists to hold
// adjacent words for each word in the dictionary
vector< list<string> > adjacents(DICTIONARY_SIZE);
// open dctnryWords.txt
inFile.open("dctnryWords.txt");
// load words into RAM
cout << "Loading words into RAM took: "
<< time_call([&] { copyDictionary(inFile, words); })
<< "ms\n";
cout << "Finding adjacent words took: "
<< time_call([&] { searchAdjacents(words, adjacents); })
<< "ms\n";
for (int i = 0; i < DICTIONARY_SIZE; i++) {
if (adjacents[i].size() >= 25) {
cout << words[i] << "(" << adjacents[i].size()
<< "): ";
for (list<string>::const_iterator j = adjacents[i].cbegin(); j != adjacents[i].cend(); j++) {
cout << *j << " ";
}
cout << endl << endl;
}
}
return 0;
}
嘗試在'if'之前打印'adjacents [i] .size()'。 – Barmar
工作目錄是否有所不同?它可能沒有找到文件 – Kevin
你可以嘗試用'cout.flush();'刷新cout嗎? http://www.cplusplus.com/reference/ostream/ostream/flush/查看http://stackoverflow.com/questions/22026751/c-force-stdcout-flush-print-to-screen – francis