C & C++

C & C++

Made by DeepSource

Inefficient character look-up in std::string CXX-P2003

Performance
Major
Autofix

Found using sub-string based method find for a character look-up.

It is more efficient to use the single-character overload when searching for a single character within a std::string, because it avoids the overhead of iterating over character literal. This can be especially important if you are searching for many single characters, as the overhead can add up. This is applicable on the following std::string search methods :-

  • find
  • rfind
  • find_first_of
  • find_first_not_of
  • find_last_of
  • find_last_not_of

Bad practice

std::string message("header");
message.find("a");

Recommended

std::string message("header");
message.find('a');