C++当使用某些数据结构时,代码在Visual Studio代码中未输出
我遇到了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的变量包括语句,例如map
,vector
等,代码无错误运行,但没有打印任何东西。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我测试了您的代码,并按照预期进行运行。
一些想法:
在线_judge
未定义时,freopen(“ output.txt”,“ w”,stdout);
使输出重定向到utput.txt < /代码>。也许您第二次运行程序时忘记了检查文件而不是控制台。
output.txt
在其他地方制造。另外,检查在线_judge
是由扩展名还是您的脚本设置。顺便说一句,如果您使用g ++,
#include&lt; bits/stdc ++。h&gt;
将包括标准库的所有标题,并且无需包含其他标题。但是这样做会大大增加您的汇编时间。I tested your code, and it ran as expected.
Some ideas:
ONLINE_JUDGE
is not defined,freopen("output.txt", "w", stdout);
makes the output redirected tooutput.txt
. Maybe you forgot checking the file instead of the console the second time you ran your program.output.txt
elsewhere. Also, check ifONLINE_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.