2013-09-30 3 views
3

"UltrasoundTemplate"이라는 클래스가 있습니다. 이 UltrasoundTemplate 객체에는 int 매개 변수가 들어 있으며,이 매개 변수는 정의 된 위치 (타임 스탬프와 같은 것)를 표시합니다. 그리고 UltrasoundTemplate의 벡터를 포함하는 "UltrasoundTarget"이라는 클래스가 있습니다. push_back (ultrasoundTemplate)을 사용하여 UltrasoundTemplates를 벡터에 추가합니다.클래스의 벡터 정렬

이제 벡터에 추가 한 순서 대신 시간 스탬프 순서로 벡터를 정렬하고 싶습니다.

나는 구글에서 많은 해결책을 발견했는데, 모두 똑같은 해결책을 보여 주지만 분명히 나 역시 여전히 잘못된 것을하고있다. AS를

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; 
    } 
} 

: 여기에 내가 생각하는 코드 조각은 해결책을 찾기 위해 필요하다 예 : 볼륨 (0)에 Template1, 볼륨 (70)에 Template2, 볼륨 (40)에 Template3을 정의합니다.). 주문은 지금 (Template1, Template2, Template3)이지만 (Template1, Template3, Template2)되도록하고 싶습니다. 그러나이 코드는 그렇게하지 않습니다.

정보가 누락되면 알려 주시면 더 많은 코드를 제공해 드리겠습니다.

고맙습니다. 여기에 엉망으로 만드는 값을 기준으로

+0

'volume_'을 (를) 지정하는 것을 잊어 버렸습니까? – Katniss

답변

5

귀하의 getTemplates() 메소드가 리턴 :

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

당신은 호환되지 않는 반복자 범위를 정렬한다. 그것은 일반적인 관행은 이러한 방법에 const 과부하를 추가하는 것입니다

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

: 당신은 참조를 반환하여 특정 문제를 해결할 수 있습니다

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()를 만드는 것이 필요합니다 어쨌든해야있어서,

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

주입니다 일반적으로 클래스의 개인 데이터에 대한 참조를 제공하는 것은 좋은 방법이 아닙니다. 가능한 경우 UltraSoundTarget 인터페이스에서이를 제거하는 방법을 찾아야합니다. 예를 들어 한 쌍의 반복자를 노출하거나 클래스에 정렬 메소드를 제공 할 수 있습니다.

+0

하나의 답변으로 너무 많은 주제를 다뤘다 고 생각합니다. UltrasoundTarget의 내용을 변경하려는 경우 const 참조의 내용을 변경할 수 없기 때문에 getTemplates의 const 및 버전을 사용하면 안됩니다. –

+0

그래, 알 겠어. 대답, btw +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; 
} 
}