c++におけるVectorなどのSTLコンテナのカスタムソート

11076 ワード

STLコンテナの要素クラスを自分で定義するには、STLコンテナの要素に対する要求を満たすことが望ましい:1、Copyコンストラクタ2、付与=オペレータ3、オブジェクトを破棄できるコンストラクタ別:1、使用可能なデフォルトコンストラクタ数、シーケンス型コンテナは、要素2、==オペレータ定義を初期化するために使用する必要があります.等しい3、<オペレータ定義を判断するには、関連コンテナが必要で、デフォルトのソートに使用されます.
 
structにoperator<を加えるとstructをソートすることができます.あなたのpcd structにはポインタがないので、copy constructorとcopy assignmentは必要ありません.コンパイラはあなたに提供します.自分でする必要はありません.ソートするときはsort(obj.begin()、objと書きます.end()でいいです.
 
投稿:http://bbs.csdn.net/topics/40228627
もう一つの参照先:http://blog.csdn.net/tigernana/article/details/7293758
投稿:http://blog.csdn.net/guang11cheng/article/details/7556697
 
vectorのカスタムソートを3つの方法で実現
メソッド1:演算子の再ロード
 #include <vector>
 #include <algorithm>
 #include <functional>

using namespace std;
struct TItem
{
    int m_i32Type;
    int m_i32ID;

    bool operator <(const TItem& rhs) const //            
    {
        return m_i32Type < rhs.m_i32Type;
    }
    bool operator >(const TItem& rhs) const //            
    {
        return m_i32Type > rhs.m_i32Type;
    }
};
int main()
{
    vector<TItem> stItemVec;


    TItem stItem1;
    stItem1.m_i32Type = 1;
    stItem1.m_i32ID = 1;

    TItem stItem2;
    stItem2.m_i32Type = 2;
    stItem2.m_i32ID = 2;

    TItem stItem3;
    stItem3.m_i32Type = 3;
    stItem3.m_i32ID = 3;

    TItem stItem4;
    stItem4.m_i32Type = 2;
    stItem4.m_i32ID = 4;

    stItemVec.push_back(stItem1);
    stItemVec.push_back(stItem2);
    stItemVec.push_back(stItem3);
    stItemVec.push_back(stItem4);

    //     
    sort(stItemVec.begin(), stItemVec.end(), less<TItem>()); 
    //   sort(ctn.begin(), ctn.end());          

    for (size_t i = 0; i < stItemVec.size(); i++)
        printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); printf("--
"); // sort(stItemVec.begin(), stItemVec.end(), greater<TItem>()); for (size_t i = 0; i < stItemVec.size(); i++) printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); return 0; }

メソッド2:グローバル比較関数
 #include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

bool lessmark(const TItem& stItem1, const TItem& stItem2)
 {
     return stItem1.m_i32Type < stItem2.m_i32Type;
 }
 

bool greatermark(const TItem& stItem1, const TItem& stItem2)
 {
     return stItem1.m_i32Type > stItem2.m_i32Type;
 }
 

int main()
 {
     vector<TItem> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(stItem1);
     stItemVec.push_back(stItem2);
     stItemVec.push_back(stItem3);
     stItemVec.push_back(stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), lessmark); //    
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); printf("--
"); sort(stItemVec.begin(), stItemVec.end(), greatermark); // for (size_t i = 0; i < stItemVec.size(); i++) printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); return 0; }

メソッド3:関数オブジェクト
#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

class CompLess
 {
 public:
     bool operator ()(const TItem& stItem1, const TItem& stItem2)
     {
         return stItem1.m_i32Type < stItem2.m_i32Type;
     }
 };
 

class CompGreater
 {
 public:
     bool operator ()(const TItem& stItem1, const TItem& stItem2)
     {
         return stItem1.m_i32Type > stItem2.m_i32Type;
     }
 };
 

int main()
 {
     vector<TItem> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(stItem1);
     stItemVec.push_back(stItem2);
     stItemVec.push_back(stItem3);
     stItemVec.push_back(stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess()); //    
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); printf("--
"); sort(stItemVec.begin(), stItemVec.end(), CompGreater()); // for (size_t i = 0; i < stItemVec.size(); i++) printf("type: %d, id: %d
", stItemVec[i].m_i32Type, stItemVec[i].m_i32ID); return 0; } /* : type: 1, id: 1 type: 2, id: 2 type: 2, id: 4 type: 3, id: 3 -- type: 3, id: 3 type: 2, id: 2 type: 2, id: 4 type: 1, id: 1 vector sort 。 */

質問:
1,サンプルコードには>と<関係処理,==関係はどのように導出されるのか.
2、ソート時に要素を移動するには、効率はどうですか?
3、カスタム構造がクラスの内部に定義され、関数オブジェクトを使用してソートされている場合、この関数オブジェクトはクラスのメンバー関数として使用できますか?
4.上記の例では、vectorに格納されているのはすべて構造(オブジェクト)そのものであり、格納されているのが構造ポインタである場合、どのようにソートすればよいのでしょうか.この場合、グローバルな比較関数または関数オブジェクトのみで行うことができ、比較関数のパラメータはポインタタイプであれば、以下のようになります.
(1)グローバル比較関数
#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

bool CompLess(const TItem* pstItem1, const TItem* pstItem2)
 {
     return pstItem1->m_i32Type < pstItem2->m_i32Type;
 }
 

bool CompGreater(const TItem* pstItem1, const TItem* pstItem2)
 {
     return pstItem1->m_i32Type > pstItem2->m_i32Type;
 }
 

int main()
 {
     vector<TItem*> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(&stItem1);
     stItemVec.push_back(&stItem2);
     stItemVec.push_back(&stItem3);
     stItemVec.push_back(&stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess); //    
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d
", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID); printf("--
"); sort(stItemVec.begin(), stItemVec.end(), CompGreater); // for (size_t i = 0; i < stItemVec.size(); i++) printf("type: %d, id: %d
", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID); return 0; }

(2)関数オブジェクト
#include <vector>
 #include <algorithm>
 #include <functional>
 

using namespace std;
 

struct TItem
 {
     int m_i32Type;
     int m_i32ID;
 };
 

class CompLess
 {
 public:
     bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
     {
         return pstItem1->m_i32Type < pstItem2->m_i32Type;
     }
 };
 

class CompGreater
 {
 public:
     bool operator ()(const TItem* pstItem1, const TItem* pstItem2)
     {
         return pstItem1->m_i32Type > pstItem2->m_i32Type;
     }
 };
 

int main()
 {
     vector<TItem*> stItemVec;
 

    TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 

    TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 

    TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 

    TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 

    stItemVec.push_back(&stItem1);
     stItemVec.push_back(&stItem2);
     stItemVec.push_back(&stItem3);
     stItemVec.push_back(&stItem4);
 

    sort(stItemVec.begin(), stItemVec.end(), CompLess()); //    
 

    for (size_t i = 0; i < stItemVec.size(); i++)
         printf("type: %d, id: %d
", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID); printf("--
"); sort(stItemVec.begin(), stItemVec.end(), CompGreater()); // for (size_t i = 0; i < stItemVec.size(); i++) printf("type: %d, id: %d
", stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID); return 0; }