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;
}
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;
}
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.
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 << " ";
}