簡體   English   中英

C ++如何使用std :: sort對c ++中的對象數組進行排序

[英]C++ How to use std::sort to sort a array of objects in c++

我有模板類和指向對象的指針數組以及我的對象的重載邏輯運算符。 我的泡泡排序正在起作用。 因此,它知道如何比較我想要用標准排序聲明替換它的對象

實現List.tem

#include <stdio.h>
#include <stdlib.h>
#include <vector>
///////////////
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

template <class Type>
List<Type>::List()
{
    itemPtr = NULL;
    used = 0;
    size = 0;
}


template <class Type>
List<Type>::List(const List<Type>& source)
{
    itemPtr = NULL;
    used = 0;
    size = source.size;
    itemPtr = new Type[size];
    for(int i = 0; i < source.used; i++)
        addItem(source.itemPtr[i]);
}

template <class Type>
List<Type>& List<Type>::operator = (const List<Type>& source)
{
    used = 0;
    if(this == &source)
        return(*this);  
    free();

    size = source.size;
    itemPtr = new Type[size];

    for(int i = 0; i < source.used; i++)
        addItem(source.itemPtr[i]);
    return(*this);
}    

template <class Type>
List<Type>::~List()
{
    free();
}

template <class Type>
void List<Type>::free() 
{
    if(itemPtr != NULL)
    {
        delete [] itemPtr;
        itemPtr = NULL;
    }
}

template <class Type>
void List<Type>::alloc(int sizeIcrease) 
{
    Type* tmpPtr = NULL;
    size += sizeIcrease;
    tmpPtr = new Type[size];
    copy(itemPtr, itemPtr+used, tmpPtr);
    free();
    itemPtr = tmpPtr;
}

template <class Type>
Type List<Type>::getItem(int index) const
{
    Type item;
    if(index >= 0 && index < used)
        item = itemPtr[index];
    return (item);
}    

template <class Type>
int List<Type>::findItem(Type itemIn)
{
    int i = 0;
    for(i = 0; i < used; i++)
        if(itemPtr[i] == itemIn)
            break;
    if (i == used)  i = -1;
    return (i);
}

template <class Type>
void List<Type>::addItem(Type itemIn)
{
    if(used == size) alloc(10);
    itemPtr[used++] = itemIn;
    bubbles();
}

template <class Type>
void List<Type>::removeItem(Type itemIn)
{
    int index = findItem(itemIn);
    removeItem(index);
}

template <class Type>
void List<Type>::removeItem(int index)
{
    if(index >= 0 && index < used)
        itemPtr[index] = itemPtr[--used];
    bubbles();
}

template <class Type>
void List<Type>::readFile(Field fileName)
{
    Type itemTmp;   
    ifstream inFile(fileName.c_str());
    if(!inFile)
        cout << "Error opening file\n";
    do
    {
        inFile >> itemTmp;
        if(!inFile.fail())
            addItem(itemTmp);

    } while (!inFile.fail());
    //bubbles();
    vector<Type*> myvector; //(itemPtr, itemPtr+used);

    vector<Type>::iterator it;
    sort (myvector.begin(), myvector.end(), sort_by_pointee<Type>());
    inFile.close();
}


template <class Type>
void List<Type>::writeFile(Field fileName)
{
    ofstream outFile;
    outFile.open(fileName.c_str());
    if(!outFile)
        cout << "Error opening file\n";

    for(int i = 0; i < used; i++)
    {
        outFile << itemPtr[i] << "\n";
    }

    outFile.close();
}
    template <class Type>
void List<Type>::print()
{
//////Coded
}

template <class Type>
void List<Type>::bubbles()
{
////// Coded
}

template <class Type>
ostream& operator<<(ostream& os, const List<Type>& ad)
{
    for(int i = 0; i < ab.used; i++)    
        os << ab.getItem(i) << endl;
    return os;
}
template <class Type>
ofstream& operator<<(ofstream& ofs, const List<Type>& ad)
{
    for(int i = 0; i < ab.used; i++)            
        ofs << ab.getItem(i) << ",";
    return ofs;
}

template<class Type>
struct sort_by_pointee 
{
    bool operator() (const Type* lhs, const Type* rhs) const
    {
        return (*lhs < *rhs);
    }
};

如果未提供std::sort()的第三個參數,則使用operator< like對對象進行排序:

if (a < b) {
   // ...
}

因此,對Foo類型的對象進行排序所需要的只是:

bool Foo::operator< (const Foo& rhs) const;

要么

bool operator< (const Foo& lhs, const Foo& rhs);

話雖這么說,如果你有一個指針數組,那么你需要提供一個自定義謂詞,除非你想按內存地址對對象進行排序(我非常懷疑這是你想要的)。 你可以這樣做:

template<class T>
struct sort_by_pointee {
    bool operator() (const T* lhs, const T* rhs) const
    {
        return (*lhs < *rhs);
    }
};

並使用它像:

std::vector<Foo*> foos;
// ...
std::sort(foos.begin(), foos.end(), sort_by_pointee<Foo>());

編輯 :您發布的樣本將正常工作並對數據進行排序,但該向量不會作為存儲在itemPtr數組中的數據的代理。 用我的注釋再次閱讀:

{ 
    vector<Type> myvector (itemPtr, itemPtr+8);
    // 'myvector' holds a copy of the first 8 elements in the 'itemPtr' array.

    sort (myvector.begin(), myvector.end());
    // contents of 'myvector' are sorted, but this is a copy of 'itemPtr''s
    // contents, so items in 'itemPtr' are still in their original order.
}

如果要在適當的位置對[itemPtr,itemPtr+8)的內容進行排序,您可以這樣做:

std::sort(itemPtr, itemPtr+8); // use custom predicate if required.

編輯 :好的,按照您發布的代碼,我會將readFile()方法從其原始定義修復為:

template <class Type>
void List<Type>::readFile(Field path)
{
    ifstream file(path.c_str());
    if(!file.is_open()) {
        cout << "Error opening file\n";
    }
    for (Type item; file >> item;) {
        addItem(item);
    }
    sort (itemPtr, itemPtr+used);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM