错误:<bits/stdc++.h>,未找到“cstdalign”文件,正在运行 C++17

Shr*_*van 4 c++ xcode c++11 visual-studio-code c++17

我正在尝试在 macOS Catalina 上的Visual Studio Code中运行一段代码。代码:

#include <bits/stdc++.h>
using namespace std;

int main() 
{ 
    // Create an empty vector 
    vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30); 
  
    for (int x : vect) 
        cout << x << " "; 
  
    return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

当我尝试使用coderunner 扩展运行代码时,出现错误:

[Running] cd "/Users/VSC_Files/" && g++ -std=c++17 helloworld.cpp -o helloworld && "/Users/VSC_Files/"helloworld
In file included from helloworld.cpp:1:
/usr/local/include/bits/stdc++.h:57:10: fatal error: 'cstdalign' file not found
#include <cstdalign>
         ^~~~~~~~~~~
1 error generated.

[Done] exited with code=1 in 1.465 seconds
Run Code Online (Sandbox Code Playgroud)

显然这只是 C++11 的错误,那么为什么我会收到此错误?我也有最新更新的 Xcode 版本和最新稳定版本的 VSCode。

稍后编辑和添加

另外,我想补充一点,我手动添加了该bits/stdc++.h文件,并且以前不存在该文件。

另外,当我更改g++ -std=c++17为仅g++在运行时,程序会运行并显示正确的输出。带有如下所示的警告。
helloworld.cpp:13:15: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]

mt笔记本电脑默认的C++版本有问题吗?请帮忙!

yao*_*dav 5

#include<bits/stdc++.h>是 GCC 的内部标头,您不应该使用它,它不可移植。

删除#include<bits/stdc++.h> 已安装的写入#include<vector>#include<iostream> 删除使用名称空间 std它被认为是不好的做法,因此您的代码应该如下所示:

#include <vector>
#include <iostream>

int main() 
{ 
    // Create an empty vector 
    std::vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30); 
  
    for (int x : vect) 
        std::cout << x << " "; 
  
    return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

  • 是的,我意识到这确实有效。但我想知道为什么我在使用 &lt;bits/stdc++.h&gt; 时指出的错误会出现。任何想法? (2认同)