C++;代码在VS 2017中执行,但不在VS代码中执行 我有这个简单的C++代码。它在Microsoft Visual Studio 2017中正确执行 // Author: Herbert Schildt // Modified: Qiang Hu // File name: ~ftp/pub/class/cplusplus/Array/Array3.cpp // Purpose: Use sqrs[][] -- two dimentional integer // array to store integers from 1 to 10 and // their squares. // Ask user for a number, then look up this // number in the array and then print out its // corresponding square. #include "stdafx.h" #include <iostream> #include <vector> using namespace std; int main() { // C++ allows for the initialization of arrays. In the following, // we are initializing a 10x2 integer array. After initialization, // sqrs[0][0] = 1 // sqrs[0][1] = 1 // sqrs[1][0] = 2 // sqrs[1][1] = 4 // and so on vector<vector<int> > sqrs = { {1, 1}, {2, 4}, // The square of 2 is 4,and so on {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; int i, j; cout << "Enter a number between 1 and 10: "; cin >> i; // look up i for(j = 0; j < 10; j++) if(sqrs[j][0] == i) break; // break from loop if i is found cout << "The square of " << i << " is " ; cout << sqrs[j][1] << endl; return 0; }

C++;代码在VS 2017中执行,但不在VS代码中执行 我有这个简单的C++代码。它在Microsoft Visual Studio 2017中正确执行 // Author: Herbert Schildt // Modified: Qiang Hu // File name: ~ftp/pub/class/cplusplus/Array/Array3.cpp // Purpose: Use sqrs[][] -- two dimentional integer // array to store integers from 1 to 10 and // their squares. // Ask user for a number, then look up this // number in the array and then print out its // corresponding square. #include "stdafx.h" #include <iostream> #include <vector> using namespace std; int main() { // C++ allows for the initialization of arrays. In the following, // we are initializing a 10x2 integer array. After initialization, // sqrs[0][0] = 1 // sqrs[0][1] = 1 // sqrs[1][0] = 2 // sqrs[1][1] = 4 // and so on vector<vector<int> > sqrs = { {1, 1}, {2, 4}, // The square of 2 is 4,and so on {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; int i, j; cout << "Enter a number between 1 and 10: "; cin >> i; // look up i for(j = 0; j < 10; j++) if(sqrs[j][0] == i) break; // break from loop if i is found cout << "The square of " << i << " is " ; cout << sqrs[j][1] << endl; return 0; },c++,visual-studio,c++11,visual-studio-code,C++,Visual Studio,C++11,Visual Studio Code,我猜VSCode使用的编译器不是C++11,而是C++98。如果是这种情况,如何将编译器更改为C++11。(在这里,我一直在寻找解决方案,但从未找到有用的方法。)如果没有,如何解决此问题?在tasks.json文件中,您可以指定编译器的参数,如下所示: { "version": "0.1.0", "command": "g++", "isShellCommand": true, "args": ["-std=c++11", "example.cpp"], //<- here you can

我猜VSCode使用的编译器不是C++11,而是C++98。如果是这种情况,如何将编译器更改为C++11。(在这里,我一直在寻找解决方案,但从未找到有用的方法。)如果没有,如何解决此问题?

在tasks.json文件中,您可以指定编译器的参数,如下所示:

{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-std=c++11", "example.cpp"], //<- here you can specify c++ version as well as any other options
...

}
{
“版本”:“0.1.0”,
“命令”:“g++”,
“isShellCommand”:正确,

“args”:[“-std=c++11”,“example.cpp”]//你检查了你的C++的Env并确保它被添加到你的路径中吗?这取决于你使用的编译器是什么?<代码> VSCode <代码>要使用吗?看起来像G++,所以在最后一个警告中添加它到构建开关的内容。我安装了MinGW和我。但是我可以使用VS2017所使用的编译器吗?很多帖子:谷歌:“为c++设置visual studio代码”VSCode正确执行代码(如果我在上述代码中将
向量
更改为
数组
,它将正常工作)问题不是如何设置VSCode来运行C++代码。问题是VSCode使用的编译器不知道如何初始化<代码>向量 >我想它可能是C++版本的问题。
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["-std=c++11", "example.cpp"], //<- here you can specify c++ version as well as any other options
...

}