// kjellkod playing with temporary objects. 
// From Herb's Gotw: http://h...content-available-to-author-only...r.com/2013/05/09/gotw-2-temporary-objects-510/
//
// Cheers
// KjellKod (.wordpress.com)

#include <iterator>
#include <list>
#include <string>
#include <iostream>

using namespace std;

// no explicit keyword makes the implicit conversion possible. which can be avoided of course
// with explicit employee(string str) ...
struct employee {
   string addr;

  employee(std::string str) : addr{str}{} // oops no explicit
  bool operator==(const employee& other) const { return (addr.compare(other.addr) == 0); }
};



// using unnecessary copies. Possible scenario if employee is created as above
// 1) i++   (probably close to zero overhead?)
// 2) == operator  implicit conversion from string to employee
// 3) return value can be written {""}
// 4) const reference to the arguments 
// 5) why not just use for(auto& employee : emps) instead of for(begin, !=end, ++) to avoid temp iterators
//
string find_addr( list<employee> emps, string name ) {  // 4 
    for( auto i = begin(emps); i != end(emps); i++ ) {  // 1, 5
        if( *i == name ) {                              // 2
            return i->addr;
        }
    }
    return "";                                          // 3
}


string find_addr_improved( const list<employee>& emps, const string& name ) { 
      for(auto& employee : emps) {  
        if ( employee.addr == name)     return employee.addr;
    }
    return {""};   
}

int main() {
    auto colleagues = list<employee>{{"Kjell"}, {"Jeanine"}, {"Nathaniel"}, {"John"}}; 

    cout << find_addr(colleagues, {"Jeanine"}) << endl;
    cout << find_addr_improved(colleagues, {"Kjell"}) << endl;

    return 0;
    
}