【问题标题】:C++ cout statements not displayed during a for loop when launched from command line从命令行启动时,在 for 循环期间不显示 C++ cout 语句
【发布时间】: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;
}

【问题讨论】:

标签: c++ for-loop visual-studio-2013 cout


【解决方案1】:

我敢打赌,当您在别处启动程序时,您的程序不会找到“dctnryWords.txt”……因为它会在当前目录中查找,而在 VS 之外运行它时可能会有所不同。

【讨论】:

  • 哦,对了。我将 .txt 文件移到了 Release 文件夹中,现在可以使用了。谢谢
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多