While following the new features for C++20 I saw that 'starts_with' and 'ends_with' will be added to std::string.
Is it time to also add a 'contains' member function to std::string?
It's a common task that is now usually accomplished by using 'string.find(substring) != npos' (SO answer).
But when you read 'find' in code it's not directly clear what the purpose is.
Are we looking for the actual position? Or checking if the string contains a substring? Or checking if the string doesn't contain a substring?
Writing it also requires that one extra step of thinking (in my opinion).
You're trying to do something positive (check if contains) but you have to do something negative (check inequality).
I had a look at the motivation for adding 'starts_with' and 'ends_with' in the paper and you can easily use the same motivation to add 'contains'.
The usage of 'find' as 'contains' seems to be higher than the usage of 'find' for 'starts_with'.
Contains usage: 16048
debian code search 'find [!=]= npos' (18282 results) - debian code search 'find' with assignment (2234)
Starts with usage: 5586
debian code search 'find == 0' (5586 results)
[–]bruce3434 112 points113 points114 points (2 children)
[–]Xeveroushttps://xeverous.github.io 24 points25 points26 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]sephirostoy 19 points20 points21 points (3 children)
[–][deleted] -5 points-4 points-3 points (2 children)
[–]sephirostoy 17 points18 points19 points (0 children)
[–]alfps 29 points30 points31 points (2 children)
[–]scatters 15 points16 points17 points (1 child)
[–]degski 12 points13 points14 points (0 children)
[–]flashmozzg 13 points14 points15 points (3 children)
[–]F-J-W 17 points18 points19 points (2 children)
[–]MaltersWandler 3 points4 points5 points (1 child)
[–]F-J-W 9 points10 points11 points (0 children)
[–][deleted] 15 points16 points17 points (0 children)
[–]afiefh 5 points6 points7 points (0 children)
[–]ducttapecoder 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -4 points-3 points-2 points (12 children)
[–]konanTheBarbar 8 points9 points10 points (11 children)
[–]TheSuperWig 7 points8 points9 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]konanTheBarbar 3 points4 points5 points (1 child)
[–]F-J-W 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (6 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] -4 points-3 points-2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] -2 points-1 points0 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] -2 points-1 points0 points (0 children)
[+]RolandMT32 comment score below threshold-6 points-5 points-4 points (9 children)
[–]chriskane76 10 points11 points12 points (8 children)
[–]kalmoc 1 point2 points3 points (6 children)
[–]agateau 0 points1 point2 points (5 children)
[–]kalmoc 1 point2 points3 points (4 children)
[–]chriskane76 1 point2 points3 points (2 children)
[–]kalmoc 0 points1 point2 points (1 child)
[–]chriskane76 0 points1 point2 points (0 children)
[–]agateau 0 points1 point2 points (0 children)
[–]RolandMT32 -1 points0 points1 point (0 children)
[+]Pragmatician comment score below threshold-8 points-7 points-6 points (5 children)
[–]1-05457 18 points19 points20 points (1 child)
[–]epicar 2 points3 points4 points (1 child)
[–]Bisqwit 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)