2013-09-30 40 views
3

我有一個名爲「UltrasoundTemplate」的類。這些UltrasoundTemplate對象包含一個int參數,該參數顯示何時被定義(類似於時間戳)。我有一個名爲「UltrasoundTarget」的類,其中包含UltrasoundTemplate的矢量。 我用push_back(ultrasoundTemplate)向矢量添加UltrasoundTemplates。排序一個類的向量

現在我想按照時間戳的順序排列矢量,而不是將它們添加到矢量中的順序。

我在谷歌中找到了很多答案,這些答案都向我展示了相同的解決方案,但顯然我仍然在做錯的事情。下面是代碼片段,我認爲是有必要尋找一個解決方案:

ultrasoundTemplate.h

class UltrasoundTemplate 
{ 
public: 
UltrasoundTemplate(/*...*/); 
int getVolumePos() { return volume_; } 
private: 
int volume_; 
}; 

ultrasoundTarget.h

//the sort algorithm 
struct MyTemplateSort { 
bool operator() (UltrasoundTemplate t1, UltrasoundTemplate t2){ 
    int it1 = t1.getVolumePos(); 
    int it2 = t2.getVolumePos(); 

    if (it1 < it2) 
     return true; 
    return false; 
} 
}; 

class UltrasoundTarget 
{ 
public: 
UltrasoundTarget(/*...*/); 
vector<UltrasoundTemplate> getTemplates() { return USTemplateVector_; } 
private: 
vector<UltrasoundTemplate> USTemplateVector_; 
}; 

FMainWindow.cpp

void FMainWindow::match_slot() 
{ 
int i; 
//here I get the name of the target I'm looking for 
QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem(); 
int index = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item); 
QString itemToAppendName = item->text(0); 
for(i = 0; i < USTargetVector.size(); i++){ 
    if(USTargetVector.at(i).getName() == itemToAppendName) { 
    //here I try to sort 
    MyTemplateSort tmpltSrt; 
    std::sort(USTargetVector.at(i).getTemplates().begin(), 
       USTargetVector.at(i).getTemplates().end(), tmpltSrt);  
    break; 
    } 
} 

作爲例如:我在Volume(0)中定義Template1,在Volume(70)中定義Template2,在Volume中定義Template3(40 )。現在的順序是(Template1,Template2,Template3),但我希望它是(Template1,Template3,Template2)。但是這段代碼並沒有這樣做。

如果缺少信息,請告訴我,我會提供更多的代碼。

非常感謝。

+0

您忘了指定'volume_',也許? – Katniss

回答

5

getTemplates()按值方法返回,弄得一團糟這裏:

std::sort(USTargetVector.at(i).getTemplates().begin(), 
      USTargetVector.at(i).getTemplates().end(), tmpltSrt);  

要排序不兼容的迭代器範圍。

vector<UltrasoundTemplate>& getTemplates() { return USTemplateVector_; } 

這是常見的做法是一個const超載添加到這樣的方法:你可以通過返回的參考解決特定的問題

const vector<UltrasoundTemplate>& getTemplates() const { return USTemplateVector_; } 

您還可以修改你的比較仿函數,以避免不必要的副本(和一般的可讀性和const正確性):

struct MyTemplateSort { 
    bool operator() const (const UltrasoundTemplate& t1, const UltrasoundTemplate& t2) 
    { 
    return t1.getVolumePos() < t2.getVolumePos(); 
    } 
}; 

這就要求你做一個getVolumePos()const方法,它應該是無論如何:

class UltrasoundTemplate 
{ 
public: 
... 
int getVolumePos() const { return volume_; } 
... 
}; 

請注意,是不是一般的好做法提供了一個類的私有數據的引用。如果可能的話,你應該找到一種方法從UltraSoundTarget接口中刪除它。例如,你可以公開一對迭代器,和/或給這個類一個排序方法。

+0

我覺得你在一個答案中觸及了太多的話題!如果他打算更改UltrasoundTarget的內容,則不應使用const&版本的getTemplates,因爲const引用的內容不能更改。 –

+0

是的,我明白了。對答案+1,順便說一句。很好的寫作。 – WhozCraig

+0

@IanMedeiros他們不會使用'const'重載,但是對於需要的情況,最好有一個。 – juanchopanza

0

juanchopanza答案是正確的,問題是你從UltrasoundTarget返回矢量的方式。只需觸及另一個主題,也許可以稍微改變一下實現的設計。由於UltrasoundTarget是Ultrasound的容器,因此將此類實現爲此類的一種方法是有意義的,這樣您就可以直接訪問USTemplateVector_並且可以節省不必要的副本。例如:

class UltrasoundTarget 
{ 
public: 
UltrasoundTarget(/*...*/); 
vector<UltrasoundTemplate> getTemplates() { return USTemplateVector_; } 

void sort(); 

private: 
vector<UltrasoundTemplate> USTemplateVector_; 
}; 

void UltrasoundTarget::sort() 
{ 
std::sort(USTemplateVector_.begin(), USTemplateVector_.end(), tmpltSrt); 
} 

void FMainWindow::match_slot() 
{ 
int i; 
//here I get the name of the target I'm looking for 
QTreeWidgetItem *item = targetInfoWidget_->treeWidget->currentItem(); 
int index = targetInfoWidget_->treeWidget->indexOfTopLevelItem(item); 
QString itemToAppendName = item->text(0); 
for(i = 0; i < USTargetVector.size(); i++){ 
if(USTargetVector.at(i).getName() == itemToAppendName) 
{ 
    //here I try to sort 
    MyTemplateSort tmpltSrt; 
    USTargetVector.at(i).sort(); 
    break; 
} 
}