Skip to main content
Code Review

Return to Answer

added 10 characters in body
Source Link
kraskevich
  • 5.7k
  • 18
  • 21

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 use boost::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. Use for (const T& x : vec) or for (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 in O(n). It's "easier" than sorting the input). One caveat: you need a hash function for T in this case. You can pass it as another argumenttemplate parameter, defaulted to std::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 use boost::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. Use for (const T& x : vec) or for (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 in O(n). It's "easier" than sorting the input). One caveat: you need a hash function for T in this case. You can pass it as another argument, defaulted to std::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 use boost::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. Use for (const T& x : vec) or for (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 in O(n). It's "easier" than sorting the input). One caveat: you need a hash function for T in this case. You can pass it as another template parameter, defaulted to std::hash.

Source Link
kraskevich
  • 5.7k
  • 18
  • 21

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 use boost::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. Use for (const T& x : vec) or for (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 in O(n). It's "easier" than sorting the input). One caveat: you need a hash function for T in this case. You can pass it as another argument, defaulted to std::hash.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /