【发布时间】:2015-11-05 22:00:09
【问题描述】:
我正在为我的计算机科学课程中的一个项目编写代码,并且我正在测试我的算法以查看它是否有效以及它的速度有多快。我知道该算法有效,因为当我在 Visual Studio 2013 中启动程序时,我得到了正确的输出。但是,当我从命令行中的 Visual Studio 项目文件夹或 Windows 资源管理器启动 .exe 时,前两个 cout 语句正确显示,但 for 循环期间的 cout 语句根本不显示。同样,这只发生在我在 Visual Studio 之外启动 .exe 时。这不是一个大问题,但我想知道这里发生了什么。谢谢。
这是 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()。 -
工作目录不同吗?它可能找不到文件
-
您可以尝试通过
cout.flush();刷新 cout 吗? cplusplus.com/reference/ostream/ostream/flush见stackoverflow.com/questions/22026751/… -
有趣,我试过 cout
-
您需要做的就是调用debugging。添加额外的
cout语句以更详细地跟踪程序状态及其数据是简单的第一步。
标签: c++ for-loop visual-studio-2013 cout