Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何简单地用C++;? 我希望能够处理像C++中的表之类的东西。我指的是“excel电子表格”或“R数据框架”中的表格。然而,我的解决方案不必那么强大。我不需要在运行时添加列,但我将添加行以创建平均值。我从各个数据点构建这个表,这个表将在程序的其他地方读取,用于数据分析。我觉得像SQLite这样的解决方案太过分了。如何简单地表示这种数据 为了更容易地讨论选项,请考虑下面的海洋温度表,我们将称之为“代码>数据< /代码>: | DataType | DateTime | Location | Temperature | | ----------- | ---------------- | --------- | ----------- | | Observation | 2020-07-03_1325 | buoy 3882 | 18.1 | | Observation | 2020-07-03_1512 | buoy 3882 | 16.6 | | Observation | 2020-07-03_1701 | buoy 3882 | 15.8 | | DailyAvg | 2020-07-03_0000 | buoy 3882 | 16.8 |_C++_Data Structures_Datatable - Fatal编程技术网

如何简单地用C++;? 我希望能够处理像C++中的表之类的东西。我指的是“excel电子表格”或“R数据框架”中的表格。然而,我的解决方案不必那么强大。我不需要在运行时添加列,但我将添加行以创建平均值。我从各个数据点构建这个表,这个表将在程序的其他地方读取,用于数据分析。我觉得像SQLite这样的解决方案太过分了。如何简单地表示这种数据 为了更容易地讨论选项,请考虑下面的海洋温度表,我们将称之为“代码>数据< /代码>: | DataType | DateTime | Location | Temperature | | ----------- | ---------------- | --------- | ----------- | | Observation | 2020-07-03_1325 | buoy 3882 | 18.1 | | Observation | 2020-07-03_1512 | buoy 3882 | 16.6 | | Observation | 2020-07-03_1701 | buoy 3882 | 15.8 | | DailyAvg | 2020-07-03_0000 | buoy 3882 | 16.8 |

如何简单地用C++;? 我希望能够处理像C++中的表之类的东西。我指的是“excel电子表格”或“R数据框架”中的表格。然而,我的解决方案不必那么强大。我不需要在运行时添加列,但我将添加行以创建平均值。我从各个数据点构建这个表,这个表将在程序的其他地方读取,用于数据分析。我觉得像SQLite这样的解决方案太过分了。如何简单地表示这种数据 为了更容易地讨论选项,请考虑下面的海洋温度表,我们将称之为“代码>数据< /代码>: | DataType | DateTime | Location | Temperature | | ----------- | ---------------- | --------- | ----------- | | Observation | 2020-07-03_1325 | buoy 3882 | 18.1 | | Observation | 2020-07-03_1512 | buoy 3882 | 16.6 | | Observation | 2020-07-03_1701 | buoy 3882 | 15.8 | | DailyAvg | 2020-07-03_0000 | buoy 3882 | 16.8 |,c++,data-structures,datatable,C++,Data Structures,Datatable,重要的是,我能够访问基于任何属性的数据,以便我能够(某种程度上)快速收集特定位置的所有点、具有相同日期的所有点等 我曾考虑过制作一个2d数组(类似于std::vector),但这需要用户记住保存所需数据的列的位置。(例如,对于第二个数据点的温度,用户必须使用数据[1][3])。我还考虑过制作一个类似以下内容的结构: struct DataPoint { ObservationType observationType; DateTime dateTime; std::str

重要的是,我能够访问基于任何属性的数据,以便我能够(某种程度上)快速收集特定位置的所有点、具有相同日期的所有点等

我曾考虑过制作一个2d数组(类似于
std::vector
),但这需要用户记住保存所需数据的列的位置。(例如,对于第二个数据点的温度,用户必须使用
数据[1][3]
)。我还考虑过制作一个类似以下内容的结构:

struct DataPoint {
    ObservationType observationType;
    DateTime dateTime;
    std::string location;
    double temperature;
}
然后简单地使用
std::vector
跟踪整个表

因此,我们提出了我的问题:这两种方法中的任何一种都有意义吗?还有其他更合理的方法吗


其次,在我看来,数据点是不可变的。阻止用户在创建后修改数据有意义吗?如果是这样,最好的方法是什么?

std::vector的选项看起来不错。您不需要阻止数据修改,只需在创建后使用
const

如果需要按位置、温度等快速访问行,可以基于
map
或更复杂的数据结构创建特定索引

例如,您可以创建
std::multimap
,其中键是温度,值是表中的行数(向量中的索引)。然后,您可以使用std::lower_bound和std::upper_bound检索给定温度范围内的行的所有索引

我的解决方案不必那么强大

您可以将相同的数据保存在多个数据结构中,这肯定不是最有效的方法,但是如果您有足够的空间并且访问数据更为关键,那么这是值得考虑的

重要的是,我能够访问基于任何属性的数据,以便我能够(某种程度上)快速收集特定位置的所有点、具有相同日期的所有点等

基于您可以使用的
struct DataPoint
,例如,如果location是
multimap
键,您可以使用它访问特定位置的所有数据点

#include <iostream>
#include <vector>
#include <map> 

enum ObservationType{
    eReport,
    eHumanObservation,
    eCamera
};

typedef struct DataPoint {
    ObservationType observationType;
    std::string location;
    double temperature;
} DataPoint;


class Data {
    public:
        Data()
        {
        
            DataPoint d1 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 3882",
                .temperature = 25.3
            };

            DataPoint d2 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 3882",
                .temperature = 32.5
            };
    
            DataPoint d3 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 1659",
                .temperature = 25.3
            };
        
            dataPoints.push_back(d1);
            dataPoints.push_back(d2);
            dataPoints.push_back(d3);

            //populating all data points in location multimap
            locMap.insert(std::pair<std::string,const DataPoint>(d1.location, d1));
            locMap.insert(std::pair<std::string,const DataPoint>(d2.location, d2));    
            locMap.insert(std::pair<std::string,const DataPoint>(d3.location, d3));    
    }
    
        const std::multimap<std::string, const DataPoint>& GetLocationMuMap() const {
            return locMap;
        }
    
    private:
        std::vector<DataPoint> dataPoints;
        std::multimap<std::string, const DataPoint> locMap;
};

int main()
{
    Data data = Data();

    const DataPoint d4 = {
        .observationType = ObservationType::eCamera,
        .location = "buoy 1659",
        .temperature = 28.34
    };
    //Error : next line will fail compilation because the multimap instance is const.
    //data.GetLocationMuMap().insert(std::pair<std::string,const DataPoint>(d4.location, d4));
    
    auto range = data.GetLocationMuMap().equal_range("buoy 3882");
    std::cout << "Temperatures measured at buoy 3882" << std::endl;
    for (auto it = range.first; it != range.second; ++it)
    {
        //Error : next line will fail - The value stored in the map is of type const DataPoint
        //(*it).second.observationType = eCamera;
        std::cout << "  [" << (*it).first << ", " << (*it).second.temperature << "]" << std::endl;
    }
    
    return 0;
}
#包括
#包括
#包括
枚举观测类型{
电子报告,
不人道的观察,
埃卡梅拉
};
类型定义结构数据点{
观测类型观测类型;
std::字符串位置;
双温;
}数据点;
类数据{
公众:
数据()
{
数据点d1={
.observationType=observationType::eReport,
.location=“浮标3882”,
.温度=25.3
};
数据点d2={
.observationType=observationType::eReport,
.location=“浮标3882”,
.温度=32.5
};
数据点d3={
.observationType=observationType::eReport,
.location=“浮标1659”,
.温度=25.3
};
数据点。推回(d1);
数据点。推回(d2);
数据点。推回(d3);
//填充位置多重映射中的所有数据点
locMap.insert(std::pair(d1.location,d1));
locMap.insert(std::pair(d2.location,d2));
locMap.insert(std::pair(d3.location,d3));
}
常量std::multimap&GetLocationMuMap()常量{
返回locMap;
}
私人:
向量数据点;
std::multimap locMap;
};
int main()
{
数据=数据();
常数数据点d4={
.observationType=observationType::eCamera,
.location=“浮标1659”,
.温度=28.34
};
//错误:下一行编译将失败,因为multimap实例是const。
//data.GetLocationMuMap().insert(std::pair(d4.location,d4));
自动范围=data.GetLocationMuMap().equal_范围(“浮标3882”);

std::cout这是一种不同的方法-使用
库和lambdas

请注意,这不是一个节省内存的解决方案,但创建多查询函数(lambdas)非常简单和容易,对于简单的查询/筛选,您也可以使用条件for循环

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

enum ObservationType{
    eReport,
    eHumanObservation,
    eCamera
};

typedef struct DataPoint {
    ObservationType observationType;
    std::string location;
    double temperature;
} DataPoint;


class Data {
    public:
        Data()
        {
        
            DataPoint d1 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 3882",
                .temperature = 25.3
            };

            DataPoint d2 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 3882",
                .temperature = 32.5
            };
    
            DataPoint d3 = {
                .observationType = ObservationType::eReport,
                .location = "buoy 1659",
                .temperature = 25.3
            };
        
            dataPoints.push_back(d1);
            dataPoints.push_back(d2);
            dataPoints.push_back(d3);

    }
    
    std::vector<DataPoint> GetDataPoints(const std::string& location, double lowBoundTemp)
    {
        std::vector<DataPoint> filteredDp;
        auto predicate = [&](const DataPoint& dp)
                            {if(dp.temperature >= lowBoundTemp && dp.location.compare(location))
                                return true;};
        std::copy_if (dataPoints.begin(), dataPoints.end(), std::back_inserter(filteredDp), predicate);
        return filteredDp;
    }
    
    private:
        std::vector<DataPoint> dataPoints;
};

int main()
{
    Data data = Data();
    std::string searchedLocation = "buoy 1659";
    double searchedLowBoundTemp = 25.4;
    auto vec = data.GetDataPoints(searchedLocation, searchedLowBoundTemp);
    std::cout << "Data Points from Location : " << searchedLocation << " above temperature : " << searchedLowBoundTemp << std::endl; 
    for (const auto& item : vec){
        std::cout << "location " << item.location << ", temperature " << item.temperature << std::endl;
    }
    
    return 0;
}
#包括
#包括
#包括
枚举观测类型{
电子报告,
不人道的观察,
埃卡梅拉
};
类型定义结构数据点{
观测类型观测类型;
std::字符串位置;
双温;
}数据点;
类数据{
公众:
数据()
{
数据点d1={
.observationType=observationType::eReport,
.location=“浮标3882”,
.温度=25.3
};
数据点d2={
.observationType=observationType::eReport,
.location=“浮标3882”,
.温度=32.5
};
数据点d3={
.observationType=observationType::eReport,
.location=“浮标1659”,
.温度=25.3
};
数据点。推回(d1);
数据点。推回(d2);
数据点。推回(d3);
}
标准::