Larry Osterman said:
Dr Herbie said:
*snip*

Interesting. I was interviewing a lot of those graduates 15 years ago and I don't remember ever having that problem.  Most of the people I interviewed were bright, enthusiastic and knew the kinds of things I expected them to know. 

 

That might be the result of really good pre-screening on the part of HR though.

 

What worries me is the number of new developers I see that don't seem to have a grasp of fundimentals.  Developers who would write:

void MyFunction(string input)
{
    string dest;
    int dotIndex;
    while ((dotIndex = input.find(".")) != string::npos)
    {
        dest.push_back(input.substr(0, dotIndex-1);
        dest.push_back("\.");
        input.erase(0, dotIndex);
    }
}

instead of:

void MyFunction(string input)
{
string dest;
const char *inputString = input.c_str();
int dotIndex;
while (*inputString)
{
if (*inputString == '.')
{
dest.push_back('\');
}
dest.push_back(*inputString);
inputString += 1;
}
}

Note: Forgive the typos above - this is a real example of code someone wrote during an interview (resurrected from memory, so the code is likely to not work).  Even if the code isn't used in a performance critical area, IMHO the original example isn't nearly as clear as the second one (the first one also isn't correct since it doesn't copy the string if there aren't any '.' characters, but we won't go there).

 

I would've just written:

 

void MyFunction(const string &input)
{
}

 

Since both functions you wrote actually don't have any externally visible effects. Tongue Out Also you need to escape the backslash.

 

Seriously though, I can understand how someone might want to use find for this problem, but erase? Surely most people that know about find should know it accepts a second parameter to tell you where to start searching?

 

Also if this is really performance critical I wouldn't let dest auto-resize like that, I'd call string.reserve with at least the size of the input string. And I'd make input a const reference parameter. Smiley

 

And lastly, I'd use wstring. Death to ANSI code pages! Wink