使用不同的标准对数据进行分类
Sorting data using different criteria
我在以下程序C 方面遇到麻烦,以对这两个函数进行排序:
-
sort_by_base_depth(SnowData _array[], int size)
-
sort_by_date(SnowData _array[], int size)
这些在我的class_tester.cpp
文件中(在注释块中)。
我也想在功能中打印数据:
void print_array_elements(SnowData _array[], int size)
并在功能中获得并打印雪的平均值:
double get_average_base_depth(SnowData[], int)
SnowData.h
(完成)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
SnowData specification file
*/
class SnowData
{
private:
string snow_date;
double base_depth;
public:
SnowData();
SnowData(string _date, double _inches);
void print();
string getSnow_date();
double getBase_depth();
void setBase_depth(double);
void setSnowDate(string);
};
SnowData.cpp
(完成)
#include "SnowData.h"
#include <iomanip>
/*
Class default constructor
Sets default values for class private variables
*/
SnowData::SnowData()
{
snow_date = "";
base_depth = 0;
}
/*
OverLoaded constructor
Parameters used to populate class private variables via set functions
*/
SnowData::SnowData(string _date, double _inches)
{
setSnowDate(_date);
base_depth = 0;
setBase_depth(_inches);
}
/*
print functions
prints out class private variables
*/
void SnowData::print()
{
cout << setw(15) << left << snow_date
<< setw(5) << fixed << showpoint << setprecision(2) << right
<< base_depth << endl;
}
/*
accessor function for snow_date
*/
string SnowData::getSnow_date()
{
return snow_date;
}
/*
accessor function for base_depth
*/
double SnowData::getBase_depth()
{
return base_depth;
}
/*
mutator functions for base_depth.
ensures that base_depth is not set to a negative value
*/
void SnowData::setBase_depth(double _inches)
{
if (_inches >= 0)
base_depth = _inches;
}
/*
mutator function for snow_date
*/
void SnowData::setSnowDate(string _date)
{
snow_date = _date;
}
Class_Tester.cpp
(未完成)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "SnowData.h"
void print_array_elements(SnowData[], int);
//void sort_by_base_depth(SnowData[], int);
//void sort_by_date(SnowData[], int);
double get_average_base_depth(SnowData[], int);
int main()
{
string dates[7] = { "Jan 15", "Jan 16" ,"Jan 17" ,"Jan 18" ,"Jan 19" ,"Jan 20","Jan 21" };
double base_depth[7] = { 34.5, 23.6, 25.5, 31.5, 40.6, 30.9, 38.4 };
SnowData jan_snow[7];
int i = 0;
for (auto &one_snow_day : jan_snow)
{
one_snow_day.setSnowDate(dates[i]);
one_snow_day.setBase_depth(base_depth[i]);
i++;
}
cout << setprecision(2) << fixed << showpoint;
cout << " --- array after set functions invoked to populate array --n";
print_array_elements(jan_snow, 7);
cout << "Average base depth for the period "
<< jan_snow[0].getSnow_date() << " - "
<< jan_snow[6].getSnow_date() << " : "
<< get_average_base_depth(jan_snow, 7) << endl;
//sort_by_base_depth(jan_snow, 7);
cout << " --- array after sort by base_depth --n";
print_array_elements(jan_snow, 7);
//sort_by_date(jan_snow, 7);
cout << " --- array after sort by date --n";
print_array_elements(jan_snow, 7);
return 0;
}
double get_average_base_depth(SnowData _array[], int size)
{
double total_depth = 0; //Initialize Accumulator
for (int i = 0; i < 7; i++)
{
total_depth += i++;
}
return total_depth / 7;
/*
write code to iterate the array and add up base depth
from each individual array element
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
}
void print_array_elements(SnowData _array[], int size)
{
/*
Write down the for Loop to print out elements from array
RANGE-BASED FOR LOOP CANNOT BE USED!
*/
for (int index = 0; index < size; index++)
cout << _array << index+1 << " " ;
cout << endl;
}
void sort_by_base_depth(SnowData _array[], int size)
{
/*
Write down sort code to sort by base depth of each element in the array.
Use the getBase_depth() function of each array element
*/
}
void sort_by_date(SnowData _array[], int size)
{
/*
Write down sort code to sort by date of each element in the
array. Use the getSnow_date() function of each array element
*/
}
您可以使用std::sort
进行排序:
std::sort(begin(myArray), end(myArray));
这将使用默认的<
操作员比较元素。它将分为上升顺序。
但是,您的数组包含SnowData
对象,并且SnowData
没有<
操作员。您可以通过覆盖operator<
来创建一个,但是我们可以使用lambda函数。
假设您正在使用C 14:
// sort by base depth
std::sort(begin(myArray), end(myArray),
[](auto a, auto b){
return a.getBase_depth() < b.getBase_depth();
});
在C 11中,您不能将auto
传递给lambda,所以它是wordier的:
std::sort(begin(myArray), end(myArray),
[](const SnowData& a, const SnowData& b){
return a.getBase_depth() < b.getBase_depth();
});
可以在C 11之前将std::sort
与自定义谓词(自定义排序功能)一起使用,但它更加乏味。
对于按日期进行排序,这很难,因为您将日期存储为字符串。看看您是否可以使用std::chrono
?
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 如何读取文件并将该文件分类为不同类型的数据,这些数据都存储在结构中的特定数组中
- 计算用于CAFFE分类的HDF5数据集的平均值
- STL:在大量数据中进行分类和搜索
- C#从文件加载数据并分类为数组
- 如何根据一组规则/条件检查一组数据以进行分类
- 如何在类 c++ 中对数据结构进行分类
- 使用不同的标准对数据进行分类
- 如何设计分类器接口,不同的分类器和数据存储
- 如何区分类成员函数的声明和数据成员的声明
- SVM与openCV相结合,对数据进行分类
- 如何在c++上将分类数据转换为二进制变量