Interface
There's no point in limiting your function inputs to a vector. You can take two input iterators instead. It'll make your function more flexible and more "standard library-like".
One function should do one focused thing. Your function finds such an element and prints it as the same time. These concerns are unrelated. You can return
std::optional<T>
instead to represent either such an element or its absence. Note: it works for C++17 only. You can useboost::optional
or return a smart pointer if it's not available.
Modern C++
There's no need for things like this anymore:
typename std::map<T, int>::iterator itr; for(itr = count.begin(); itr != count.end(); itr++)
for (const auto& kv : count)
is much better, isn't it?You can also use initialization list to create a vector with the elements you need:
std::vector<char> v({'r', 't',..., 's'})
Performance
for (T x : vec)
creates an unnecessary copy. Usefor (const T& x : vec)
orfor (const auto& T : vec)
instead.You can also use
std::unordered_map
to do the counting and then choose the smallest element (it's more efficient because one can find the minimum inO(n)
. It's "easier" than sorting the input). One caveat: you need a hash function forT
in this case. You can pass it as another argumenttemplate parameter, defaulted tostd::hash
.
Interface
There's no point in limiting your function inputs to a vector. You can take two input iterators instead. It'll make your function more flexible and more "standard library-like".
One function should do one focused thing. Your function finds such an element and prints it as the same time. These concerns are unrelated. You can return
std::optional<T>
instead to represent either such an element or its absence. Note: it works for C++17 only. You can useboost::optional
or return a smart pointer if it's not available.
Modern C++
There's no need for things like this anymore:
typename std::map<T, int>::iterator itr; for(itr = count.begin(); itr != count.end(); itr++)
for (const auto& kv : count)
is much better, isn't it?You can also use initialization list to create a vector with the elements you need:
std::vector<char> v({'r', 't',..., 's'})
Performance
for (T x : vec)
creates an unnecessary copy. Usefor (const T& x : vec)
orfor (const auto& T : vec)
instead.You can also use
std::unordered_map
to do the counting and then choose the smallest element (it's more efficient because one can find the minimum inO(n)
. It's "easier" than sorting the input). One caveat: you need a hash function forT
in this case. You can pass it as another argument, defaulted tostd::hash
.
Interface
There's no point in limiting your function inputs to a vector. You can take two input iterators instead. It'll make your function more flexible and more "standard library-like".
One function should do one focused thing. Your function finds such an element and prints it as the same time. These concerns are unrelated. You can return
std::optional<T>
instead to represent either such an element or its absence. Note: it works for C++17 only. You can useboost::optional
or return a smart pointer if it's not available.
Modern C++
There's no need for things like this anymore:
typename std::map<T, int>::iterator itr; for(itr = count.begin(); itr != count.end(); itr++)
for (const auto& kv : count)
is much better, isn't it?You can also use initialization list to create a vector with the elements you need:
std::vector<char> v({'r', 't',..., 's'})
Performance
for (T x : vec)
creates an unnecessary copy. Usefor (const T& x : vec)
orfor (const auto& T : vec)
instead.You can also use
std::unordered_map
to do the counting and then choose the smallest element (it's more efficient because one can find the minimum inO(n)
. It's "easier" than sorting the input). One caveat: you need a hash function forT
in this case. You can pass it as another template parameter, defaulted tostd::hash
.
Interface
There's no point in limiting your function inputs to a vector. You can take two input iterators instead. It'll make your function more flexible and more "standard library-like".
One function should do one focused thing. Your function finds such an element and prints it as the same time. These concerns are unrelated. You can return
std::optional<T>
instead to represent either such an element or its absence. Note: it works for C++17 only. You can useboost::optional
or return a smart pointer if it's not available.
Modern C++
There's no need for things like this anymore:
typename std::map<T, int>::iterator itr; for(itr = count.begin(); itr != count.end(); itr++)
for (const auto& kv : count)
is much better, isn't it?You can also use initialization list to create a vector with the elements you need:
std::vector<char> v({'r', 't',..., 's'})
Performance
for (T x : vec)
creates an unnecessary copy. Usefor (const T& x : vec)
orfor (const auto& T : vec)
instead.You can also use
std::unordered_map
to do the counting and then choose the smallest element (it's more efficient because one can find the minimum inO(n)
. It's "easier" than sorting the input). One caveat: you need a hash function forT
in this case. You can pass it as another argument, defaulted tostd::hash
.