Exceptional C++

【Exceptional C++(4)】临时对象

2018-01-26  本文已影响5人  downdemo

问题

string FindAddr(list<Employee> l, string name)
{
    for (list<Employee>::iterator i = l.begin(); i != l.end(); i++)
    {
        if ( *i == name)
        {
            return (*i).addr;
        }
    }
    return "";
}

解答

string FindAddr(const list<Employee>& l,const string& name)
{
    string addr;
    for (list<Employee>::const_iterator i = l.begin(); i != l.end(); ++i)
    {
        if ( (*i).name == name)
        {
            addr = (*i).addr;
            break;
        }
    }
    return addr;
}
上一篇下一篇

猜你喜欢

热点阅读