排序矢量的类

时间:2012-03-22 12:55:07

标签: c++ sorting stdvector

在我公平地分享“这是链接到线程的重复”回复之前,我想说我已经实现了我在StackOverflow和CPlusPlus网站上阅读的内容。问题是我的矢量不会排序。我必须在实现中遗漏一些东西。也许不正确使用参考文献等。

这里有:我有两个类--DisargetParam和一个名为DETargetParamVector的包装类,它包含一个DETargetParam类型的STL向量。我的目标是使用DETargetParam的适应性成员变量以升序对std :: vector进行排序。我试图通过在DETargetParam类中重载less运算符并返回一个布尔结果来做到这一点。

然而,这并没有排序。一切都完美地编译和执行,但没有任何排序。我真的希望有人可以帮助我。

这是DETargetParam的源代码:

#ifndef _DE_TARGET_PARAM_H_DP_
#define _DE_TARGET_PARAM_H_DP_

template <typename T, unsigned int N>
class DETargetParam {
 private:
  /** The array of N parameters in type T */
  T param[N];
  long double fitness;
  long double probability;
  long double probabilityInterval;

 public:
  /**
   * @brief Default constructor.
   *
   * Nada!
   */
  DETargetParam() {
    /* NULL */
  }

  long double getFitness() {
    return fitness;
  }

  void setFitness(long double x) {
    fitness = x;
  }

  long double getProbability() {
    return probability;
  }

  void setProbability(long double x) {
    probability = x;
  }

  long double getProbabilityInterval() {
    return probabilityInterval;
  }

  void setProbabilityInterval(long double x) {
    probabilityInterval = x;
  }

  bool operator<(const DETargetParam& rhs) const {
    return (fitness < rhs.fitness);
  }

  T& operator[](unsigned int i) {
    return param[i];
  }
};

#endif // _DE_TARGET_PARAM_H_DP_

和DETargetParamVector包装类:

#ifndef _DE_TARGET_PARAM_VECTOR_H_DP_
#define _DE_TARGET_PARAM_VECTOR_H_DP_

#include <algorithm>
#include <cstdio>
#include <vector>

#include "DETargetParam.h"

template <typename T, unsigned int N, unsigned int NP>
class DETargetParamVector {
 private:
  /** This is a STL vector holding the parameters */
  std::vector< DETargetParam<T, N> > vec;

 public:
  /**
   * @brief Default constructor
   *
   * Move along... nothing to see here.
   */
  DETargetParamVector() {
    vec.reserve(NP);
  }

  void SortAndCalculate() {
    SortVector();
    PrintSorted();
  }

  void SortVector() {
    std::sort(vec.begin(), vec.end());
  }

  void PrintSorted() {
    for (unsigned int i = 0; i < NP; ++i) {
      fprintf(stdout, "%.12Lf, %.12Lf, %.12Lf\n", vec[i].getFitness(), vec[i].getProbability(), vec[i].getProbabilityInterval());
    }

    fprintf(stdout, "\n");

    fflush(stdout);
  }

  DETargetParam<T, N>& operator[](unsigned int i) {
    return vec[i];
  }
};

#endif // _DE_TARGET_PARAM_VECTOR_H_DP_

和主要功能细节:

#include <cmath>
#include <ctime>

#include "DETargetParamVector.h"

const unsigned int N = 10;
const unsigned int NP = 10;

int main() {
  srand(time(0));

  DETargetParamVector<long double, N, NP> targetVector;

  // For each member of the population.
  for (unsigned int i = 0; i < NP; ++i) {
    targetVector[i].setFitness(static_cast<long double>(rand()));
  }

  targetVector.SortAndCalculate();

  return 0;
}

提前致谢。

2 个答案:

答案 0 :(得分:4)

您的矢量确实已排序。问题是你的向量是空的,你所做的就是在向量之外写入和读取。

  /**
   * @brief Default constructor
   *
   * Move along... nothing to see here.
   */
  DETargetParamVector() {
    vec.reserve(NP);
  }

评论很讽刺,因为这是你的错误。 vec.reserve(NP)不会改变向量的大小,它只是为了节省将来的工作。您需要使用resize替换reserve,或者甚至更好地从一开始就初始化它:

  /**
   * @brief Default constructor
   *
   * Move along... nothing to see here.
   */
  DETargetParamVector() : vec(NP) {
  }

在侧节点上,这个:

int N = 10;
int NP = 10 * N;

int main() {
  DETargetParamVector<long double, N, NP> targetVector;

是非法的,因为你有非const int作为模板参数。您需要将int N替换为int const N(同样适用于NP)。

答案 1 :(得分:1)

我已经整理了一个简化版本的代码,至少可以生成和排序一些对象。我也(至少是IMO)修复了一些你的设计让我觉得有些问题的地方(尽管结果仍然不完美,至少是IMO)。

de_target_param.h:

#ifndef _DE_TARGET_PARAM_H_DP_
#define _DE_TARGET_PARAM_H_DP_

#include <iostream>

template <typename T, unsigned int N>
class DETargetParam {
private:
    /** The array of N parameters in type T */
    //  T param[N];
    long double fitness;
    //  long double probability;
    //  long double probabilityInterval;

public:
    DETargetParam(long double f) : fitness(f) { }
    /*
    long double getFitness() {
    return fitness;
    }

    void setFitness(long double x) {
    fitness = x;
    }

    long double getProbability() {
    return probability;
    }

    void setProbability(long double x) {
    probability = x;
    }

    long double getProbabilityInterval() {
    return probabilityInterval;
    }

    void setProbabilityInterval(long double x) {
    probabilityInterval = x;
    }
    */
    friend std::ostream &operator<<(std::ostream &os, DETargetParam const &d) { 
        return os << d.fitness;
    }

    bool operator<(const DETargetParam& rhs) const {
        return (fitness < rhs.fitness);
    }
};

#endif // _DE_TARGET_PARAM_H_DP_

de_target_param_vector.h:

#include "de_target_param.h"

#ifndef _DE_TARGET_PARAM_VECTOR_H_DP_
#define _DE_TARGET_PARAM_VECTOR_H_DP_

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

template <typename T, unsigned int N, unsigned int NP>
class DETargetParamVector {
    std::vector< DETargetParam<T, N> > vec;
public:
    void push(DETargetParam<T, N> const &d) {
        vec.push_back(d);
    }

    void SortAndCalculate() {
        SortVector();
        std::cout << *this;
    }

    void SortVector() {
        std::sort(vec.begin(), vec.end());
    }

    friend std::ostream &operator<<(std::ostream &os, DETargetParamVector const &d) {
        std::copy(d.vec.begin(), d.vec.end(), std::ostream_iterator<DETargetParam<T, N> >(os, "\n"));
        return os;
    }
};

#endif // _DE_TARGET_PARAM_VECTOR_H_DP_

而且,我写了一个小main来创建,排序和打印(按排序顺序)一些对象:

#include "de_target_param_vector.h"

int main() {

    DETargetParamVector<int, 1, 5> params;

    for (int i=0; i<5; i++) 
        params.push(rand());

    params.SortAndCalculate();
    return 0;
}

根据你如何设计你的DETargetParam类,我认为谷歌搜索“伪面向对象”或类似的东西应该会出现一些有用的相关阅读。很多你的代码似乎(至少对我来说)来体现这种类型。