class Times { const int factor; public: Times(int f) : factor(f) { } int operator()(int n) { return n*factor; } }; Times f(3); cout << f(10) << ' ' << f(12); // expect 30 36
factor
is neither public
nor private
.
factor
shouldn’t be const
, since it gets assigned.
operator()
should be const
.
auto f
instead of int f
for increased generality.
const char *cap = "James T. Kirk"; auto v = find(begin(cap), end(cap), 'T'); cout << v;
true
T
T. Kirk
E: end()
isn’t defined for a pointer
const char cap[] = "James T. Kirk"; auto v = find(begin(cap), end(cap), 'T'); cout << v;
true
T
T. Kirk
E: v
is a const char *
. It’s a C-style string—display chars until a null characters ('\0'
) is found.
int a[] = {11,22,33,44,55,66}; auto v = find(a, a+5, 42); cout << v;
false
nullptr
66