Write a program for template using member functions.
Answers
Answered by
1
#include <iostream>
#include <vector>
#include <algorithm>
struct Printer
{ // generic functor
std::ostream& os;
Printer(std::ostream& os) : os(os)
{
}
template<typename T>
void operator()(const T& obj) { os << obj << ' '; }
// member template
};
int main()
{
std::vector<int> v = {1,2,3};
std::for_each(v.begin(), v.end(), Printer(std::cout));
std::string s = "abc";
std::for_each(s.begin(), s.end(), Printer(std::cout));
}
#include <vector>
#include <algorithm>
struct Printer
{ // generic functor
std::ostream& os;
Printer(std::ostream& os) : os(os)
{
}
template<typename T>
void operator()(const T& obj) { os << obj << ' '; }
// member template
};
int main()
{
std::vector<int> v = {1,2,3};
std::for_each(v.begin(), v.end(), Printer(std::cout));
std::string s = "abc";
std::for_each(s.begin(), s.end(), Printer(std::cout));
}
Similar questions