如何使用C 创建分类表
how to create a classification table using c++
我只是想知道如何使用数组创建分类表。因此,我启动了下面的代码,我一直坚持如何替换设定范围内的数字并使其与其他内容相等。我进一步详细说明了我的意思。通过在我的代码中发表评论。我希望有人能够为此提供指导。
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
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 从其他两个文件中创建一个分类整数的文件
- 如何使用C 创建分类表
- 对QObject进行子分类并创建QList