0

I am running the below program on Dev-C++, and I am not able to execute it, as it always give me error while compiling the program.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> v2{2,3,4,5};
    for(int x : v2)
    {
        cout<<x<<" ";
    }
    return 0;
}

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

It always give me error, I just wanted to confirm, whether this type of Vector Initialisation is right? Or I am doing it the wrong way? I am new to the community, sorry If this has been answered previously also, you could redirect me to the previous answer also.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    The message says that those lists were introduced in C++11, and you should probably use `-std=c++11` flag to explicitly opt-in for it. – HolyBlackCat Apr 11 '20 at 11:54
  • 2
    What you got is a warning telling you that what you are doing is allowed, but only if you specify the c++11 norm. It is not an error. Try adding `-std=c++11` to your compiler arguments. – Patrick Apr 11 '20 at 11:55
  • 2
    Your code needs `#include` and `#include` to compile. Then it works – P. PICARD Apr 11 '20 at 11:57

1 Answers1

2

Avoid use of <bits/stdc++.h>. It is not supported by some compilers and IDEs, plus it includes the entire STL library, which is unnecessary.

Instead, use specific templates such as <vector> in your case, since your using an std::vector. Don't forget to include <iostream> for std::cout as well.

[Warning] extended initializer lists only available with -std=c++11 or -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.

Your compiler clearly supports c++11 (in fact every compiler nowadays does, thats like the minimum) so just include that while compiling:

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'.

  • Should I use any other compiler to execute this type of codes, I am learning Competitive Programming and I saw in Tutorials they were using include. That''s why I think they use this library to execute their code, and it works fine with online IDEs, shall I use different IDE to execute this header file, or I should continue with and . – Himanshu Joshi Apr 13 '20 at 06:19
  • 1
    @HimanshuJoshi Although some online compilers do support, I suggest you to not use it. The tutorials or CP threads only focus on CP and not industry practices. In the long run considering your a software engineer, no one would want you to write this header in their product code as it has the unnecessary inclusion of all headers and makes compilation rather slow. Its better to follow good practices early on. Moreover, in most CP problems you don't require more than 5 headers, and in all of them you definitely don't require even half of the STL library, so its not a waste of time writing them. –  Apr 13 '20 at 07:36
  • 1
    @HimanshuJoshi Do consider accepting the answer if it is helpful :) –  Apr 15 '20 at 03:49
  • 1
    Thanks Buddy, I got it.. Helpful.. Yes you're right, I should practice industry practices from the beginning. I was only looking this from CP point of view, I thank you for this beautiful crafted answer. To the point.. – Himanshu Joshi Apr 15 '20 at 18:08
  • 1
    @HimanshuJoshi Anytime, and perhaps you misunderstood me as by accepting I meant to say that you can tap the tick next to my answer, (below the votes) which marks it as accepted! (with a green texture) –  Apr 16 '20 at 11:38