我在 Visual Studio Code 中遇到了一个奇怪的错误 - 我正在使用标准扩展(C/C++ 扩展包)在 C++ 中编写代码,如果我编写这样一个简单的程序,它可以正常工作:
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout << "Hello World!" << endl;
}
但是,第二次我使用 STD 包含语句(例如 a 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;
我测试了您的代码,它按预期运行。
一些想法:
ONLINE_JUDGE
未定义时,freopen("output.txt", "w", stdout);
使输出重定向到output.txt
. 也许您在第二次运行程序时忘记检查文件而不是控制台。output.txt
其他地方。另外,检查是否ONLINE_JUDGE
由扩展程序或您的脚本设置。顺便说一句,如果您使用 g++,#include <bits/stdc++.h>
将包含标准库的所有头文件,并且不需要包含其他头文件。但是这样做会大大增加你的编译时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句