在子类的向量上使用std :: sort

时间:2019-07-20 03:10:18

标签: c++ stdvector

我有A类,B类和C类。 B类是A类的孩子。 C类本质上是A类,而B类则归为一类(不分为父级和子级)。

我的目标是拥有B类的std :: vector,并能够使用std :: sort对向量进行反向排序。我在所有3个类中都添加了比较运算符的重载。

运行时:

std::vector<C> c;
std::sort(c.rbegin(), c.rend());

向量正确排序。

但是,在运行时:

std::vector<B> b;
std::sort(b.rbegin(), b.rend());

比较类运算符在B类中重载了运算符“ <”(按预期方式),但是未进行实际排序。

我已经检查了类A和B,以查看是否错过了构造函数和复制构造函数的任何内容,据我所知,这完全没问题。因此,由于比较运算符重载在所有3个类中是否完全相同,因此不确定要解决的可能问题是什么。

此外,我想向B类或C类添加一个静态函数,以与std :: sort进行比较,而不是像我目前正在使用的那样使用<<。想检查是否可行?还是我需要添加一个免费功能进行比较?

EDIT(提供一个最小的可复制示例): A.cpp:

#include "A.h"

A::A(int test_addition)
{
  this->test_vector_.push_back(test_addition);
}

A::~A()
{

}

//overloads
A& A::operator = (const A& other)
{
  //std::cout << "0 - have come here to copy npo \n";

  if (this!=&other)
  {
    this->test_vector_ = other.test_vector_;
  }

  return *this;
}

A::A(const A& other)
{
  //std::cout << "COPYCONSTRUCTOR - have come here to copy npo \n";

  {
    this->test_vector_ = other.test_vector_;
  }

}

bool A::operator > (const A& other)
{
  return this->test_vector_.size() > other.test_vector_.size();
}

bool A::operator < (const A& other)
{
  return this->test_vector_.size() < other.test_vector_.size();
}

void A::add_new_addition(int test_addition)
{
  this->test_vector_.push_back(test_addition);
}

B.cpp:

#include "B.h"

B::B(int test_id_, int test_addition) :
                                          A(test_addition)
{
  this->test_id_ = test_id_;
}

B::~B()
{

}

//overloads
B& B::operator = (const B& other)
{

  if (this!=&other)
  {
    this->test_id_ = other.test_id_;
  }

  return *this;
}

B::B(const B& other) :
                      A(other)
{

  {
    this->test_id_ = other.test_id_;
  }

}

bool B::operator > (const B& other)
{
  return this->test_vector_.size() > other.test_vector_.size();
}

bool B::operator < (const B& other)
{
  return this->test_vector_.size() < other.test_vector_.size();
}

void B::debug_output()
{
  std::cout << "id: " << test_id_ << " " << test_vector_.size() << " \n";
}

main.cpp:

#include <vector>
#include <algorithm>

#include "A.h"
#include "B.h"

void create_test_vector(std::vector<B>& main_vector)
{
  int test_addition = 1;
  int id = 0;

  for (int i=0; i<10; i++)
  {
    B tmp_obj(i, test_addition);

    for (int q=0; q<i; q++)
    {
      tmp_obj.add_new_addition(test_addition);
    }

    main_vector.push_back(tmp_obj);
  }


  //std::sort(main_vector.begin(), main_vector.end());

  std::sort(main_vector.rbegin(), main_vector.rend());
}

void print_test_vector(std::vector<B>& main_vector)
{
  for (int i=0; i<main_vector.size(); i++)
  {
    main_vector[i].debug_output();
  }
}

int main()
{
  std::vector<B> main_vector;

  create_test_vector(main_vector);
  print_test_vector(main_vector);

  return 0;
}

在通过这个简单示例进行测试时,我意识到,如果我还将A类的复制构造函数语句添加到B类,则代码也可以工作。本质上,在我的错误代码中,仅复制了B类变量,而不复制了A类变量。

编辑的B类:

//overloads
B& B::operator = (const B& other)
{

  if (this!=&other)
  {
    this->test_id_ = other.test_id_;
    this->test_vector_ = other.test_vector_;
  }

  return *this;
}

B::B(const B& other) :
                      A(other)
{

  {
    this->test_id_ = other.test_id_;
    this->test_vector_ = other.test_vector_;
  }

}

0 个答案:

没有答案