Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Last active November 9, 2016 06:10
Show Gist options
  • Select an option

  • Save kkabdol/1eb2394bf4772a0b4edaa035bece0469 to your computer and use it in GitHub Desktop.

Select an option

Save kkabdol/1eb2394bf4772a0b4edaa035bece0469 to your computer and use it in GitHub Desktop.
over-eager evaluation
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Employee {
string m_name;
int m_cubicleNumber;
};
static const int numberOfEmployee = 2;
static Employee employeeList[numberOfEmployee] = {
{ "seonghyeon", 1 },
{ "shaun", 2 },
};
int findCubicleNumber(const string& employeeName)
{
typedef std::map<string, int> CubicleMap;
static CubicleMap cubes;
CubicleMap::iterator it = cubes.find(employeeName);
if (it == cubes.end()) {
int cubicle = -1;
for (int i=0; i < numberOfEmployee; ++i)
{
if( employeeList[i].m_name == employeeName )
{
cubicle = employeeList[i].m_cubicleNumber;
cout << employeeName << " found." << endl;
}
}
if( cubicle == -1 )
{
cout << employeeName << " not found." << endl;
}
cubes[employeeName] = cubicle;
return cubicle;
}
else
{
return (*it).second;
}
}
int main() {
cout << findCubicleNumber("seonghyeon") << endl;
cout << findCubicleNumber("seonghyeon") << endl;
cout << findCubicleNumber("seonghyeon") << endl;
cout << findCubicleNumber("shaun") << endl;
cout << findCubicleNumber("baby") << endl;
/* output
seonghyeon found.
1
1
1
shaun found.
2
baby not found.
-1
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment