如何使用C 创建分类表

how to create a classification table using c++

本文关键字:分类 创建 何使用      更新时间:2023-10-16

我只是想知道如何使用数组创建分类表。因此,我启动了下面的代码,我一直坚持如何替换设定范围内的数字并使其与其他内容相等。我进一步详细说明了我的意思。通过在我的代码中发表评论。我希望有人能够为此提供指导。

int main(int argc, _TCHAR* argv[])
{
    vector<vector<double>> arrays = {
        { 0.2746458, 0.484255, 0.15154546, 0.0325468},
        { 0.141573001, 0.129732453, 0.3524564, 0.000458475} 
    };
size_t count = 0;
double sum = 0;
float element = 0; 
for (const vector<double> &array : arrays) {
    for (float element : array) {
        if (0.0 <= element && element <= 0.24) {
            /* This part of the code should replace any number within the given range 
            of 0.0 to 0.24 and make that number equal to 1 So essentially I want 
            the above array to end up looking like this:
            { 0.2746458, 0.484255, 1, 1},
            { 1, 1, 0.3524564, 1}
            */
        }
        if (0.24 <= element && element <= 0.5) {
            /*  This part of the code is meant to do something similar to the above one
            were it finds any number within a range of 0.24 to 0.5 and make each of those 
            numbers equal to 2 so the array ends up looking like this
            { 2, 2, 1, 1},
            { 1, 1, 2, 1}
            */
        }
    }
}
cout << "Classification Table " << endl;
//print the arrays after it has gone through the above code

return 0;

}

您可以使用代理类。例如:

class DynamicValue
{
  private:
    int c;
  public:
    DynamicValue() : c(0) {}
    DynamicValue(double x) : c() { this->c = this->classification(x); }
    DynamicValue(const DynamicValue& dv) c(dv.c) {}
    ~DynamicValue() {}
    DynamicValue& operator = (double x) {
      this->c = this->classification(x);
      return *this;
    }
    DynamicValue& operator = (const DynamicValue& dv) {
      this->c = dv.c;
      return *this;
    }
    operator int () { return this->c; }
    int classification(double x) {
      if(x < 0.0) return 0;
      else if(x < 0.24) return 1;
      else if(x < 0.5) return 2;
      else return 3;
    }
};

然后您可以这样使用:

std::vector<DynamicValue> values(5);
values[0] = 0.3;
values[1] = -0.01;
values[2] = 0.1;
values[3] = 0.63;
values[4] = 0.21;
for(std::size_t i = 0; i < values.size(); ++i)
  std::cout << (int)values[i] << std::endl; // will print 20131