我需要搜索由指向名爲Person的結構的指針鏈表組成的數據庫。內部人有一堆數據 - 名字,姓氏,社會保障等等。這些都是虛構的,無關緊要的。我的問題是,我需要根據用戶輸入進行搜索,這決定了搜索結果的哪個部分正在進行比較。由於所有的數據都是作爲struct Person的成員存儲的,我認爲做這件事的最好方法(就像不寫8個搜索函數一樣)是通過映射,但是我對映射的掌握很差,幾乎不存在。下面是相關代碼:結構/類使用映射的C++訪問成員
List * find(List * database, //mapping stuff, string name)
{
//run search
return database;
}
void search(List * database)
{
string field, searchtype, userinput;
cout << "To search for a person, enter information in this format: 'field equal
value' or 'field begins value'. Type 'clear' to
return to original database. Type 'exit' to leave the program\n";
while(field != "exit")
{
cin >> field >> searchtype >> userinput;
if(userinput == "firstname") //this is just for example, I would have to write one of these out for each parameter.
{
List * smallerdb = find(database, map(//mapping stuff?), string userinput);
}
}
}
這是學校,所以請不要認爲我只是用另一個庫,因爲我不能。謝謝!
您不需要映射。您需要一個參數化的*比較器*,該參數化的比較器*被初始化爲要比較的字段的「狀態」。然後在比較項目時使用該比較器(可能是仿函數)。 – WhozCraig 2013-04-24 18:24:43
也許你想傳遞一個指向成員的指針到你的搜索函數中,然後用它從列表中的每個項目中獲取數據。 – antlersoft 2013-04-24 18:24:50
我認爲我錯誤地將函數映射到了函數上,但我仍然對函數非常模糊。你認爲你可以指出我正確的方向嗎? – musicman1007 2013-04-24 18:31:32