Syntax Question Re: Vectors

Hello everyone,

In the first code example I am just trying to print out a simple vector. But I get compilation errors. In the second code, the compiler gives me errors with respect to the assert statements. I suspect that the syntax of both my code examples vis-a-vis vectors is erroneous. I would appreciate feedback. Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> v1{0,1,2};
    cout << v1 << endl;
}


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

int main()
{
    vector<int> v1{1,2,3};
    assert(v1 == {1,2,3});
}
Last edited on
Look at http://www.cplusplus.com/reference/vector/vector/

There is no output operator.

The op== takes only vectors: http://www.cplusplus.com/reference/vector/vector/operators/


For the output, a good old loop:
1
2
3
4
std::vector<int> v1{0,1,2};
for ( auto e : v1 ) {
  std::cout << e << '\n';
}
Topic archived. No new replies allowed.