Rule Definition
This algorithm is designed to be used in a case where you search for a limited number of candidates in a large set of data. If N is the set of searched elements and M the scope of elements to search, the time complexity of the method is 'O(N*M)'.
If the number of candidates becomes large, other methods exist whose complexity is 'O(N*log(M))' or even 'O(N)'. They use an associative container to store the list of possible candidates.
If the data type can only have a very limited number of values (such as 'char'), you can have another alternative with a bit set describing the values you are searching for. This is what a function tailored to 'char' might do (for instance, the C 'strpbrk' function).
Remediation
Depending on the situation, you can either:
- Use 'std::find_first_of' if you find yourself in the kind of situation for which it was designed
- Use an ad-hoc specialization for small types, such as the one described in the reference
- Use an alternative with 'std::set'
- Use an alternative with 'std::unordered_set' or another hash set implementation
Violation Code Sample
Fixed Code Sample
Reference
"Jim Xochellis: find_first_of: A performance pitfall among the STL algorithms":http://www.codeproject.com/KB/stl/find_first_of.aspx
Related Technologies
C++
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.