-2

The compile error says it was not declared in this scope, it does not name a type, expected ;

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vec;

    vec.push_back(1);
    vec.push_back(3);
    vec.push_back(5);

    for (auto it = vec.begin(); it != vec.end(); ++it) {
         cout << *it << endl;
    }

    cout << "size: " << vec.size() << endl;

    return 0;
}
David G
  • 94,763
  • 41
  • 167
  • 253
Sal Rosa
  • 551
  • 2
  • 8
  • 12

1 Answers1

2

As commented, you need to specify the -std=c++11 flag when compiling.

Just to mention an alternative syntax for iterating, the range-for statement:

for (auto& i: vec)
{
    std::cout << i << std::endl;
}

and for populating a vector with initial values using uniform initialization:

std::vector<int> vec {1, 3, 5};

Also see Why is "using namespace std" considered bad practice?

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252