I write a print template function which prints "True"
or "False"
for bool type value.
I tried to apply it to the for_each
algorithm but it does not work.
The output is still 0
or 1
. It seems the template specialization does not work in for_each
. How can this happen?
How can I make the code work as expected?
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <ctime>
using namespace std;
template <typename T>
void _print(const T& i)
{
cout<<i<<endl;
}
template <>
void _print<bool>(const bool& i)
{
if (i == 0)
cout<<"False"<<endl;
else
cout<<"True"<<endl;
}
class print
{
public:
template <typename T>
void operator()(const T& val) const { _print(val); };
};
int main()
{
auto even = [&](int i)->bool{return (i%2==0);};
srand(time(NULL));
vector <int> test(3);
generate(test.begin(),test.end(),[]()->int{return rand()%100;});
for_each(test.begin(),test.end(),print());
vector <bool> flag(3);
transform(test.begin(),test.end(),flag.begin(),even);
for_each(flag.begin(),flag.end(),print());
return 0;
}
The output is:
34
23
3
1
0
0
but I expect:
34
23
3
True
False
False