2

I need to create a simple lookup function for a program and want to confirm the best way to accomplish the task. I have a two-column CSV file that represents a string (key) and double (value) pair. The list is about 3,000 rows / key-value pairs. I will be doing about 5,000 lookups on this table each time my program is executed. Some psuedo-code is below followed by a few questions:

 CSV file - columns are "Tenant" and "PD"

 // Declare an unordered map
 unordered_map<string,double> TenantPDLookup;

 // Read from CSV file into the map object - I can do this part
 void ReadTenantLookup(unordered_map<string,double> TenantPDLookup) {...}

 // Lookup the values (PD) based on a series of keys (Tenant)
 // Here is my code that is not working (note this is a type string, string)
 string GetTenantRating(string const& TenantName, Assumptions& Ass, 
               tenant_lookup_map const& TenantRatingLookup) {

      auto TenantRating = TenantRatingLookup.find(TenantName);

      if (TenantRating == TenantRatingLookup.end())
           return Ass.DefaultTenantRating;
      return TenantRating->second;
 }

My questions about how to implement this are as follows:

  1. How do I do the actual lookup? I am thinking a simple function that returns the value when passed (a) a reference to my map and (b) a key. Can someone provide a simple framework
  2. My string values are "orderable" in the sense that they are alpha terms - should I somehow make this into an ordered list to facilitate faster lookups?
  3. Does this approach make sense?

1 Answer 1

3
// Declare an unordered map
typedef std::unordered_map<std::string,double> pd_lookup_map;
pd_lookup_map TenantPDLookup;

// Read from CSV file into the map object - I can do this part
pd_lookup_map ReadTenantLookup() {
  pd_lookup_map retval;
  // read std::string and double from file
  std::string key_from_file;
  double value_from_file;
  retval[key_from_file] = value_from_file;
  // repeat for entire file

  return retval; // is very efficient to return std containers by value
}

// Lookup the values (PD) based on a series of keys (Tenant)
// How do I do this part?
double GetTenantPD(unordered_map const& TenantPDLookup, std::string const& Key, double default_value = 0.0) {
  auto it = TenatePDLookup.find(Key);
  if (it == TenatePDLookup.end())
    return default;
  return *it;
}

This assumes you'd rather have a default value than expose an error if the Key is not found.

If you want to indicate that the key was not found, you'll have to do something different when it == blah.end() after a find( ).

Sign up to request clarification or add additional context in comments.

10 Comments

This looks great - thanks. I will try to implement and post back with any problems or questions
Sorry got held up on this project - I have gotten the first part working (getting the values loaded from my CSV file into the unordered map), now working on the lookup. Will confirm but things look great so far. Thanks again.
Having some issues with the second part. I corrected what I believe are a few typos (Tenate vs. Tenant, default vs. default_value), but I am still having problems with the return value (*it) - my compiler is giving me a type conversion error (since I am supposed to be returning a double but it is an unordered map?) Any thoughts?
The map contains key value pairs. If you want the value, it->second.
Thanks again. Still not compiling for me - see my revised code above - now I am getting an 'unresolved external symbol' error - any idea what's going on?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.