0

i write this simple program below to use vector but here is error can anyone help ?

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void main()
{
    vector<int>a(10,1);
    cout<<a<<endl;
}
Theolodis
  • 4,977
  • 3
  • 34
  • 53

2 Answers2

0

There is no operator<<(ostream&, vector const&). You can provide one yourself:

ostream& operator<<(ostream& os, vector<int> const& v) {
    for (int i=0; i<v.size(); ++i) {
        os << v[i] <<  ", ";
    }
    return os;
}

Put this code before your main function, and it should work.

morten
  • 392
  • 1
  • 11
0

Try this:

std::ostream& operator<<(std::ostream& stream, std::vector<int> const& vec) {
    for (auto it = vec.begin(); it != vec.end(); it++) {
        stream << *it <<  " ";
    }
    return stream;
}

Or:

template <typename T>
std::ostream& operator<<(std::ostream& stream, std::vector<T> const& vec) {
    for (auto it = vec.begin(); it != vec.end(); it++) {
        stream << *it <<  " ";
    }
    return stream;
}

(but make sure the << operator is overloeaded for T)

And if you don't want to overload the << operator:

for (auto& item : a) {
    std::cout << item << " ";
}
vincentp
  • 1,433
  • 9
  • 12