Major rail disruption after train breaks down leaving station
Major rail disruption after train breaks down leaving station
Snow White star Rachel Zegler mocked over bizarre jaw movements at Met Gala 2026
When are results of elections are expected to be announced across Essex?
When are results of elections are expected to be announced across Essex?
Sarajevo Safari hunters who paid to shoot people 'competed to kill the most beautiful women'
Who was the worst dressed at the Met Gala: Nearly half a million votes have already been cast, now give YOUR verdict
The 2026 local elections DEEP DIVE: How the two-party system could be torn apart - see what's happening in your area with our ultimate guide
Experts pinpoint the exact heartbeat ranges that signal stroke risk... how does YOURS measure up?
CO2 Levels In the Atmosphere Hit 'Depressing' New Record
Read more of this story at Slashdot.
Police officer's alleged 'discreditable conduct' sparks misconduct and criminal investigations
Nigel Farage responds to claims Reform UK would sell off NHS if elected
Obama makes subtle dig about 'bar' for President and reveals extra terrestrial political ambitions while speaking at his monstrosity of a library
Couple discovers man LIVING in basement after noticing food missing, items moved around home
Faces of two men who faked banknotes in Essex workshop in 'sophisticated' £20m counterfeit operation
Trump announces pause on breaking Iran's blockade of Strait of Hormuz as he signals hope for 'complete and final agreement'
Race to 10k: I've an inkling who'll win - and I hate to admit but it won't be me!, says SIMON LAMBERT
Hillary Clinton tried to BAN comedian from asking about her controversial emails before agreeing to cringe interview
CodeSOD: Please Find, Rewind
As previously discussed, C++ took a surprisingly long time to get a "starts with" function for strings. It took even longer to get a function called "contains". In part, that's simply because string::find solves that problem.
Nancy sends us a… different approach to solving this problem.
bool substringInString(string str, string::iterator &it) { string tmp; bool result = false; int size = str.length(); int count = 0; while (count < size) { tmp += *it; it++; count++; if (tmp.find(str) != string::npos) { result = true; it -= size; break; } } if ( !result) { it -= size; } return result; }This function iterates across a string, character by character. In this iteration, we copy one character at a time into tmp. Then we see if tmp contains our search str. If it does, we break out of the loop after rewinding the iterator. Outside of the loop, we check if we found the substring, and if we did, we rewind the iterator. Then we return true or false based on whether on not we found the substring.
So wait a second. str is our search string. it is where we're searching. And we copy from it up to our search string's length into a temporary string. We then do a find in that temporary string- hey! This is just a startsWith check written in the most insane way possible.
Why even bother with the while loop? While tmp is shorter than the search string, the answer is always "no, we haven't found it". And the developers knew that- that's why they always rewind size characters on the iterator. They're always searching exactly that many characters. Of course, since we always rewind the same amount, we can also just move the it -= size statement out of the loop and out of the if statement and do it once.
Nancy calls this "a little gem" in a "large codebase". Yeah, a real gem.
[Advertisement] Plan Your .NET 9 Migration with ConfidenceYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. Download Free Guide Now!