-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
  • 90,891
  • 40
  • 158
  • 247
Sal Rosa
  • 491
  • 1
  • 8
  • 10

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
  • 117,013
  • 19
  • 199
  • 247