I want to write template function which can print container like std::vector
, std::list.
Below is my function, just overload <<
.
Declaring template
could be dangerous as this template includes 'all' variable types int
, char
etc. Due to this compiler does not know which operator<<
to use.
In order to take only container type variables use template of templates. Here is working code for you
template class Container>
std::ostream& operator<<(std::ostream& out, const Container>& c) {
for (auto item : c) {
out << item << " ";
}
return out;
}
int main()
{
cout << "Hello world" << endl;
int arr[] = { 0,3,6,7 };
vector v(arr, arr+4);
cout << v << endl;
return 0;
}