【发布时间】:2019-02-07 19:55:09
【问题描述】:
我有一个具有以下结构的输入文件
#Latitude Longitude Depth [m] Bathy depth [m] CaCO3 [%] ...
-78 -177 0 693 1
-78 -173 0 573 2
.
.
我创建了一个地图,它有一个基于 string(海洋盆地的名称)的键和一个包含数据向量的值。现在我需要按bathyDepth 对向量进行排序。准确地说,我想对向量进行分区,以便我可以在所有数据行之间进行分区,其深度在0 和500m、500m 和1500m、1000m 和2000m 之间。 ..
我已将数据存储到 map 结构中,但我不确定如何存储和访问分区,以便随后可以cout 特定深度的数据点。
我的尝试:
//Define each basin spatially
//North Atlantic
double NAtlat1 = 0, NAtlong1 = -70, NAtlat2 = 75, NAtlong2 = -15;
//South Atlantic and the rest...
double SPLIT = 0;
struct Point
{
//structure Sample code/label--Lat--Long--SedimentDepth[m]--BathymetricDepth[m]--CaCO3[%]--CO3freefraction (SiO2 carb free)[%]--biogenic silica (bSiO2)[%]--Quartz[%]--CO3 ion[umol/l]--CO3critical[umol/l]--Delta CO3 ion[umol/kg]--Ref/source
string dummy;
double latitude, longitude, rockDepth, bathyDepth, CaCO3, fCaCO3, bSilica, Quartz, CO3i, CO3c, DCO3;
string dummy2;
//Use Overload>> operator
friend istream& operator>>(istream& inputFile, Point& p);
};
//MAIN FUNCTION
std::map<std::string, std::vector<Point> > seamap;
seamap.insert( std::pair<std::string, std::vector<Point> > ("Nat", vector<Point>{}) );
seamap.insert( std::pair<std::string, std::vector<Point> > ("Sat", vector<Point>{}) );
//Repeat insert() for all other basins
Point p;
while (inputFile >> p && !inputFile.eof() )
{
//Check if Southern Ocean
if (p.latitude > Slat2)
{
//Check if Atlantic, Pacific, Indian...
if (p.longitude >= NAtlong1 && p.longitude < SAtlong2 && p.latitude > SPLIT)
{
seamap["Nat"].push_back(p);
} // Repeat for different basins
}
else
{
seamap["South"].push_back(p);
}
}
//Partition basins by depth
for ( std::map<std::string, std::vector<Point> >::iterator it2 = seamap.begin(); it2 != seamap.end(); it2++ )
{
for (int i = 500; i<=4500; i+=500 )
{
auto itp = std::partition( it2->second.begin(), it2->second.end(), [&i](const auto &a) {return a.bathyDepth < i;} );
}
}
注意: a 的类型为 Point。如果我尝试将itp 存储到向量等结构中,则会收到以下错误:
error: no matching function for call to ‘std::vector<Point>::push_back(__gnu_cxx::__normal_iterator<Point*, std::vector<Point> >&)’
我只是不确定如何存储itp。最终目标是计算一个数据点与特定深度窗口内的所有其他数据点之间的距离(例如1500m 到2500m)。对此新手的任何帮助将不胜感激。
【问题讨论】:
-
我很确定您可以进一步缩小代码范围,向我们展示您的问题的minimal reproducible example。
-
抱歉,“简单排序”是什么意思?
标签: c++ c++11 stdvector stdmap partition