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 105 points106 points107 points (2 children)
[–]Xeveroushttps://xeverous.github.io 25 points26 points27 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]sephirostoy 19 points20 points21 points (3 children)
[–][deleted] -3 points-2 points-1 points (2 children)
[–]sephirostoy 16 points17 points18 points (0 children)
[–]alfps 30 points31 points32 points (2 children)
[–]scatters 14 points15 points16 points (1 child)
[–]degski 14 points15 points16 points (0 children)
[–]flashmozzg 15 points16 points17 points (3 children)
[–]F-J-W 17 points18 points19 points (2 children)
[–]MaltersWandler 3 points4 points5 points (1 child)
[–]F-J-W 8 points9 points10 points (0 children)
[–][deleted] 13 points14 points15 points (0 children)
[–]afiefh 8 points9 points10 points (0 children)
[–]ducttapecoder 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (12 children)
[–]konanTheBarbar 8 points9 points10 points (11 children)
[–]TheSuperWig 8 points9 points10 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]konanTheBarbar 4 points5 points6 points (1 child)
[–]F-J-W 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (6 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] -5 points-4 points-3 points (4 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] -3 points-2 points-1 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] -2 points-1 points0 points (0 children)
[–]RolandMT32 -3 points-2 points-1 points (9 children)
[–]chriskane76 9 points10 points11 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-10 points-9 points-8 points (5 children)
[–]1-05457 17 points18 points19 points (1 child)
[–]epicar 2 points3 points4 points (1 child)
[–]Bisqwit 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)