鑑於東西的集合,被建模爲:集裝箱訂貨要求(在努力瞭解的boost :: multi_index)
struct Foo { int id; std::string name; };
其中id
是獨一無二的,name
是非唯一/非{NULL |空}。
如何使用類似的boost :: multi_index一個數據結構,這將讓我做等價的:
select id, name from Foo group by name order by id
標準STL容器沒有得到我應許之地。我使用了上面的一個sql變體來解釋我的觀點。我沒有真正處理任何數據庫。
UPDATE:
所以,顯然我需要的是:
typedef boost::multi_index_container<
Foo,
indexed_by<
ordered_unique<identity<Foo>>,
ordered_unique<member<Foo, std::string, &Foo::name>>
>
> MIC;
有人可以證實我有這個權利?它似乎工作。我還沒有測試過。
可能重複[std :: map,如何按值排序,然後按鍵](http://stackoverflow.com/questions/19842035/stdmap-how-to-sort-by-value-then-by -key) – Treycos
@Treycos它根本不重複。請再看看我的sql查詢。我沒有要求按ID排序,然後按名稱排序。我說我想按ID排序,同時將所有*相同的*名稱分組,並將它們分配給它們中的任何一個。所以{1,「Foo」} {2,「Bar」} {3,「Foo」}最終會返回{1,「Foo」} {2,「Bar」} **或** {2,「Bar」 } {3,「Foo」} – ForeverLearning
能有人比我更有效嗎?刪除那個「這個問題可能已經有了答案」的標籤?我的情況看起來不合適。 – ForeverLearning