Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V817. It is more efficient to search...
menu mobile close menu
Additional information
toggle menu Contents

V817. It is more efficient to search for 'X' character rather than a string.

Nov 16 2016

The analyzer detected a function that looks for a character in a string and can be optimized.

Consider the following example of inefficient code:

bool isSharpPresent(const std::string& str)
{
  return str.find("#") != std::string::npos;
}

In this code, it is better to use an overridden version of the find function that receives a character instead of a string.

Optimized code:

bool isSharpPresent(const std::string& str)
{
  return str.find('#') != std::string::npos;
}

The following example also uses inefficient code that can be optimized:

const char* GetSharpSubStr(const char* str)
{
  return strstr(str, "#");
}

In this code, it is better to use the strchr function to search for a character instead of a string:

const char* GetSharpSubStr(const char* str)
{
  return strchr(str, '#');
}

Take
a chance!

Take a chance —
spin a wheel.

Prize circle Prize circle Outer ring

Congrats! You've won a 30-day trial license!

You tricked the system, well done! Get a 30-day trial as your prize!