1

I am trying to learn STL (vectors, maps, sets) using Dev C++. But it is giving me error. Please look at the given code and error.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    vector <int> A = {11, 84, 1, 57};

    cout << A[2] << endl;

    sort(A.begin(), A.end());  //1, 11, 57, 84

    bool present = binary_search(A.begin(), A.end(), 57);  //true
    present = binary_search(A.begin(), A.end(), 5);  //false

    A.push_back(158);  //1, 11, 57, 84, 158
    A.push_back(158);  //1, 11, 57, 84, 158, 158
    A.push_back(158);  //1, 11, 57, 84, 158, 158, 158
    A.push_back(285);  //1, 11, 57, 84, 158, 158, 158, 285
  
    vector<int>::iterator it = lower_bound(A.begin(), A.end(), 158);   //>=
    vector<int>::iterator it2 = upper_bound(A.begin(), A.end(), 158);  //< (returns 1st element 
    which is strictly greater than 158)
 
    cout << *it << " " << *it2 << endl;  //
    cout << it2 - it <<endl;  //

    //return 0;
}

Error:

9 33 G:\C++\STL.cpp [Error] in C++98 'A' must be initialized by constructor, not by '{...}'

9 33 G:\C++\STL.cpp [Error] could not convert '{11, 84, 1, 57}' from '' to 'std::vector'

This is the error that it is throwing. When I am using same code in online IDE then it is running properly.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 5
    This requires C++11, and your compiler seems to use C++98. Enable a newer standard in the compiler settings. I also suggest updating the compiler (it using C++98 by default suggests that the compiler is very old). – HolyBlackCat Sep 17 '20 at 06:58
  • 2
    It looks like you're compiling to an older standard. This should compile in C++11 or higher. What are your compiler settings? – jkb Sep 17 '20 at 06:58
  • I found another SO question that may fix your issue: https://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran – Frodyne Sep 17 '20 at 07:00
  • @Frodyne Looks good, you should flag the question as duplicate then. Allthough it's not really a duplicate it will most likely solve OP's issue. – Lukas-T Sep 17 '20 at 07:03
  • Does this answer your question? [How to change mode from c++98 mode in Dev-C++ to a mode that supports C++0x (range based for)?](https://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran) – Lukas-T Sep 17 '20 at 07:03

0 Answers0