C++当使用某些数据结构时,代码在Visual Studio代码中未输出

发布于 2025-02-01 05:22:24 字数 1179 浏览 2 评论 0原文

我遇到了Visual Studio代码中的一个奇怪的错误 - 我正在使用标准扩展(C/C ++扩展包)在C ++中编写代码,如果我编写这样的简单程序,则可以正常工作:


int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cout << "Hello World!" << endl;
}

但是,第二个我声明了第二个使用std的变量包括语句,例如mapvector等,代码无错误运行,但没有打印任何东西。

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    vector<int> test;
    cout << "Hello World!" << endl; // this no longer prints
}

有什么想法为什么发生此错误?我还有所有我需要的陈述:

#include <bits/stdc++.h>  
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

I'm running into a weird bug in Visual Studio Code - I'm writing code in C++ using standard extensions (C/C++ extension pack) and if I write a simple program like this, it works fine:


int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    cout << "Hello World!" << endl;
}

However, the second I declare a variable using STD include statements, such as a map, vector, etc., the code runs without errors but doesn't print anything.

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    vector<int> test;
    cout << "Hello World!" << endl; // this no longer prints
}

Any ideas why this error is occurring? I have all the include statements I need as well:

#include <bits/stdc++.h>  
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>

using namespace std;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2025-02-08 05:22:24

我测试了您的代码,并按照预期进行运行。

一些想法:

  • 在线_judge未定义时,freopen(“ output.txt”,“ w”,stdout);使输出重定向到utput.txt < /代码>。也许您第二次运行程序时忘记了检查文件而不是控制台。
  • 此外,由于您使用了Visual Studio代码,也许您使用了扩展程序来运行程序。扩展程序可能不会使用您当前的工作目录来运行该程序,这使output.txt在其他地方制造。另外,检查在线_judge是由扩展名还是您的脚本设置。

顺便说一句,如果您使用g ++,#include&lt; bits/stdc ++。h&gt;将包括标准库的所有标题,并且无需包含其他标题。但是这样做会大大增加您的汇编时间。

I tested your code, and it ran as expected.

Some ideas:

  • When ONLINE_JUDGE is not defined, freopen("output.txt", "w", stdout); makes the output redirected to output.txt. Maybe you forgot checking the file instead of the console the second time you ran your program.
  • Besides, since you were using Visual Studio Code, maybe you used an extension to run your program. The extension may not use your current working directory to run the program, which made the output.txt elsewhere. Also, check if ONLINE_JUDGE is set by the extension or your script.

BTW, if you use g++, #include <bits/stdc++.h> will include all headers of standard library, and there is no need for including other headers. But doing so will dramatically increase your compilation time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文