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!
Lauren Sanchez mercilessly mocked after boasting about losing two pounds ahead of the Met Gala: 'That's just water weight'
Ministers launch 'Trojan Horse' plot to smuggle Andy Burnham back to Westminster to prepare for leadership challenge against Keir Starmer
Orlando Bloom, 49, steps out with scantily-clad Katy Perry lookalike, 27, at Met Gala afterparty... but there's a twist
Mother who died in Bristol explosion is hailed a hero for saving her child seconds before gangster ex-boyfriend's 'grenade attack'
Sell in May? Not anymore as 'Taco Trump' reverses the seasonal trend
British influencer issues a grovelling apology over 'tone-deaf' Anzac Day stunt - as she returns to the UK: 'I'm sorry'
AWS lets agents drive its virtual cloudy desktops – which could cost 500,000 tokens per click
AWS lets agents drive its virtual cloudy desktops - which could cost 500,00 tokens per click
Amazon Web Services has let AI agents loose in its cloudy WorkSpaces virtual PCs.…