[英]Is this the correct way to initialize a vector in CPP?
我在 Dev-C++ 上運行以下程序,但我無法執行它,因為它在編譯程序時總是給我錯誤。
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v2{2,3,4,5};
for(int x : v2)
{
cout<<x<<" ";
}
return 0;
}
[警告] 擴展初始化列表僅適用於 -std=c++11 或 -std=gnu++11
它總是給我錯誤,我只是想確認這種類型的向量初始化是否正確? 還是我做錯了? 我是社區的新手,抱歉如果之前也已回答過此問題,您也可以將我重定向到上一個答案。
避免使用<bits/stdc++.h>
。 一些編譯器和 IDE 不支持它,而且它包括整個 STL 庫,這是不必要的。
相反,在您的情況下使用特定模板,例如<vector>
,因為您使用的是std::vector
。 不要忘記為std::cout
包含<iostream>
。
[警告] 擴展初始化列表僅適用於 -std=c++11 或 -std=gnu++11
Provided your compiler supports C++ uptil a specific standard, you can set the standard using -std=standard
, where 'standard' can be c++11
, c++14
, c++17
or c++20
, taking into account of the recent versions of the C++ standard.
您的編譯器顯然支持c++11
(實際上現在每個編譯器都支持,這就像最低限度一樣)所以在編譯時只需包含它:
g++ -std=c++11 Filename.cpp -o Filename
This will compile your C++ file using g++ compiler with C++11 standard, and create an object file of the source file 'Filename.cpp'.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.