2012-01-19 14 views
1

アイテムと属性のリストがあります。C++は属性別にアイテムを並べ替えます

struct Item { double a, b; Item (double a_, double b_): a(a_), b(b_){}}; 

typedef std::vector <Item> TItems; 
typedef std::vector <double> TAttributes; 

私はペアのリストを使用して属性によってアイテムをソートしようとしています:

int main(int argc, char* argv[]) 
{ 
    TItems items; 
    items.push_back(Item (1.0, 2.0)); 
    items.push_back(Item (3.0, 4.0)); 
    items.push_back(Item (5.0, 6.0)); 

    TAttributes attributes; 
    attributes.push_back(8); 
    attributes.push_back(7); 
    attributes.push_back(9); 

    std::pair <TAttributes, TItems> pairs; 
    //No element has been coppied 
    std::copy (pairs.first.begin(), pairs.first.end(), std::back_inserter (attributes)); 
    //No element has been coppied 
    std::copy (pairs.second.begin(), pairs.second.end(), std::back_inserter (items)); 

    std::sort (pairs.first.begin(), pairs.first.end()); 
} 

2つの問題があります。

A]コピーの不適当実装は(ないellementがcoppiedされていません)

B] too "screwed" code。

属性の別のリストを使用してアイテムのリストを並べ替える簡単な方法はありますか?

コピー操作を正しく実装する方法は?

+0

「問題」を言い換えることができますか?あなたは何をしようとしていますか?問題は何ですか? –

答えて

2

実際には、pairsに何も入れていません。 std::pairのコンストラクタを参照してください。

std::pair <TAttributes, TItems> pairs(attributes_, items_); 

はまた、あなたは、属性値に基づいてItemsを並べ替えたい場合、あなたはおそらくstd::pair<double, Items>のリスト(ないリストのペア)にstd::sortを呼び出して、適切な比較関数を提供したいです。

関連する問題