2
我需要爲程序創建一個簡單的查找函數,並且要確認完成任務的最佳方式。我有一個表示字符串(鍵)和雙(值)對的兩列CSV文件。該列表大約有3000行/鍵值對。每次執行程序時,我都會在該表上執行大約5,000次查找。一些僞代碼如下跟着幾個問題:在C++中實現無序映射
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;
}
我有關如何實現這個問題如下:
- 我該怎麼做實際的查詢?我正在考慮一個簡單的函數,它在傳遞(a)對我的映射的引用和(b)一個鍵時傳回值。有人可以提供一個簡單的框架
- 我的字符串值是「可定義的」,因爲它們是字母術語 - 我應該以某種方式將它變成有序列表以便更快地查找嗎?
- 這種方法有意義嗎?
這看起來不錯 - 謝謝。我會嘗試實現並回發任何問題或問題 – brentf
對不起,在這個項目上舉行 - 我已經得到了第一部分工作(從我的CSV文件中加載值到無序映射),現在正在查找。將確認但事情看起來很棒。再次感謝。 – brentf
與第二部分有一些問題。我糾正了我認爲是一些錯別字(Tenate與Tenant,默認與default_value),但我仍然有問題的返回值(* it) - 我的編譯器給我一個類型轉換錯誤(因爲我被假定要返回一個雙,但它是一個無序的地圖?)任何想法? – brentf