C++ 对类的向量进行排序

C++ 对类的向量进行排序,c++,class,sorting,vector,C++,Class,Sorting,Vector,我有一门叫做“超声波模板”的课。这些UltrasondTemplate对象包含一个int参数,该参数显示它们的定义时间(类似于时间戳)。我有一个叫做“超声波目标”的类,它包含一个超声波模板的向量。 我使用push_back(超声波模板)将超声波模板添加到向量中 现在我想按照时间戳的顺序对向量进行排序,而不是按照我将它们添加到向量的顺序 我在谷歌上找到了很多答案,这些答案都向我展示了相同的解决方案,但显然我仍然在做错事。以下是我认为找到解决方案所必需的代码片段: 超声波模板 class Ultra

我有一门叫做“超声波模板”的课。这些UltrasondTemplate对象包含一个int参数,该参数显示它们的定义时间(类似于时间戳)。我有一个叫做“超声波目标”的类,它包含一个超声波模板的向量。 我使用push_back(超声波模板)将超声波模板添加到向量中

现在我想按照时间戳的顺序对向量进行排序,而不是按照我将它们添加到向量的顺序

我在谷歌上找到了很多答案,这些答案都向我展示了相同的解决方案,但显然我仍然在做错事。以下是我认为找到解决方案所必需的代码片段:

超声波模板

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

//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_;
};
//排序算法
结构MyTemplateSort{
布尔运算符()(超声波模板t1,超声波模板t2){
int it1=t1.getVolumePos();
int it2=t2.getVolumePos();
如果(it1
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;
  }
 }
void FMainWindow::match_slot()
{
int i;
//这是我要找的目标的名字
QTreeWidgetItem*item=TargetInfo小部件\->treeWidget->currentItem();
int index=targetInfo小部件->树状图->indexOfTopLevelItem(项目);
QString itemToAppendName=项目->文本(0);
对于(i=0;i
例如:我在卷(0)中定义了Template1,在卷(70)中定义了Template2,在卷(40)中定义了Template3。现在的顺序是(Template1,Template2,Template3),但我希望它是(Template1,Template3,Template2)。但这段代码没有这样做

如果缺少信息,请告诉我,我会提供更多代码

非常感谢。

您的
getTemplates()
方法按值返回,在这里造成混乱:

std::sort(USTargetVector.at(i).getTemplates().begin(),
          USTargetVector.at(i).getTemplates().end(), tmpltSrt);     
您正在对不兼容的迭代器范围进行排序。您可以通过返回引用来修复该特定问题:

vector<UltrasoundTemplate>& getTemplates() { return USTemplateVector_; }
您还可以修改比较函子以避免不必要的副本(以及为了一般可读性和常量正确性):


请注意,提供对类的私有数据的引用通常不是好的做法。如果可能,您应该找到一种方法从
UltrasondTarget
接口中删除该数据。例如,您可以公开一对迭代器,和/或为类提供排序方法。

juanchopanza回答正确,问题是ay您正在从UltrasonTarget返回向量。为了触及另一个主题,可能最好对实现的设计进行一点更改。由于UltrasonTarget是超声波的容器,因此将排序作为此类的一种方法来实现是有意义的,这样您就可以直接访问USTemplateVector_uu,并可以保存un必要的副本。类似于:

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;
 }
}
class超声波目标
{
公众:
超声靶(/*…*/);
向量getTemplates(){return USTemplateVector_u;}
无效排序();
私人:
向量USTemplateVector;
};
void UltrasonTarget::sort()
{
std::sort(USTemplateVector_uu.begin()、USTemplateVector_uu.end()、tmpltSrt);
}
void FMainWindow::match_slot()
{
int i;
//这是我要找的目标的名字
QTreeWidgetItem*item=TargetInfo小部件\->treeWidget->currentItem();
int index=targetInfo小部件->树状图->indexOfTopLevelItem(项目);
QString itemToAppendName=项目->文本(0);
对于(i=0;i
你可能忘记分配
卷了吧?我想你在一个答案中涉及的主题太多了!如果他打算更改UltrasonTarget的内容,他不应该使用getTemplates的const&版本,因为const引用的内容不能更改。是的,我看到了。对答案+1,顺便说一句。写得好向上。@IanMedeiros他们不会使用
const
重载,但在需要的情况下最好有一个重载。由于模板向量封装在目标类中,因此将排序方法添加到目标类中而不是在类外执行会更有意义……这样可以避免他通过-值对引用返回的问题。@IanMedeiros当然。他们可能会在这个过程中学到一些东西。但我建议同时添加这两个重载。
struct MyTemplateSort {
  bool operator() const ( const UltrasoundTemplate& t1, const UltrasoundTemplate& t2)
  {
    return t1.getVolumePos() < t2.getVolumePos();
  }
};
class UltrasoundTemplate
{
public:
 ...
 int getVolumePos() const { return volume_; }
 ...
};
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;
 }
}