0
A
回答
0
1
如果你想從一個角色到該角色開始的所有字符串的映射,你可能會發現ILookup<TKey, TElement>
合適。這是非常類似於一個Dictionary<TKey, TValue>
,有兩個主要差別:
代替1:1映射,它執行1:n的映射(即,可以存在每個鍵一個以上的值)。
無法實例(
new
),也不填充它(.Add(…)
)自己;相反,您讓.NET通過在後者上調用.ToLookup(…)
從另一個集合中派生出一個完全填充的實例。
以下是如何建立這樣一個1一個例子:N地圖:
using System.Collections.Generic; // for List<T>
using System.Linq; // for ILookup<TKey, TValue> and .ToLookup(…)
// This represents the source of your strings. It doesn't have to be sorted:
var strings = new List<string>() { "Foo", "Bar", "Baz", "Quux", … };
// This is how you would build a 1:n lookup table mapping from first characters
// to all strings starting with that character. Empty strings are excluded:
ILookup<char, string> stringsByFirstCharacter =
strings.Where(str => !string.IsNullOrEmpty(str)) // exclude empty strings
.ToLookup(str => str[0]); // key := first character
// This is how you would look up all strings starting with B.
// The output will be Bar and Baz:
foreach (string str in stringsByFirstCharacter['B'])
{
Console.WriteLine(str);
}
PS:爲
ILookup<…>
(接口)以上超鏈接指向你的幫助頁面Lookup<…>
(實現類)。這是有目的的,因爲我發現這個類的文檔更容易閱讀。不過,我會推薦在你的代碼中使用這個接口。
0
那麼你需要創建一個索引函數的字符串。
此ID的建議使用
Dictionary<string,List<string>>
數據結構。
ToLookup不太好,因爲它限制了你的數據結構的管理能力。
+0
我不能分開這樣的話。那麼它會使我的其他操作複雜化。 – Kumaran
相關問題
- 1. 字符串的最佳數據結構
- 2. 字符串集合的數據結構
- 3. 最佳實踐:10000+字符串的Angularfire數據庫結構
- 4. 比較字符串與多個字符串(最佳數據結構)
- 5. Objective-C的最佳數據結構排序字符串和布爾
- 6. 發行結束收據(字符串)數據庫在C#
- 7. C malloc數組的字符串結構
- 8. C - 結構數組的printf字符串
- 9. 將字符串映射到值的最佳數據結構是什麼?
- 10. 存儲特定結構化數據的最佳數據結構
- 11. C#字符串去結構
- 12. C字符串結構
- 13. 最佳控制逐步數據收集
- 14. 選擇最佳整數集合的數據結構和設計
- 15. 字符串索引的數據結構?
- 16. 爲字符串創建動態結構的最佳方法?
- 17. 收集PHP中的數據結構
- 18. 最佳C結構定義
- 19. C - 字符串的最佳實踐
- 20. 訂單的最佳數據庫結構
- 21. cudamemcopy的最佳數據結構
- 22. 最佳的樹數據結構
- 23. MicroStrategy的最佳數據庫結構
- 24. A *什麼是開放集合的最佳數據結構?
- 25. Java樹字符串數據結構
- 26. 字符串在結構用C
- 27. 在結構中操作字符串C
- 28. 最佳數據結構在C#中尋求
- 29. 用MPI_Gather收集字符串openmpi c
- 30. 在C++/C之間傳遞結構中的字符串/數組#
你在用什麼字符串? – Liath
有多少是「巨大的」? – Larry
您是否使用字符串集合作爲建議列表? – Jaihind