访问地图中的分区向量

Accessing a partitioned vector within a map

我有一个具有以下结构的输入文件

#Latitude   Longitude   Depth [m]   Bathy depth [m] CaCO3 [%] ...
-78 -177    0   693 1
-78 -173    0   573 2
.
.

我创建了一个地图,它有一个基于 string 海洋盆地 的名称)的键和一个包含矢量的值数据。现在我需要按 bathyDepth 对向量进行排序。准确地说,我想对向量进行分区,以便我可以在深度介于 0500m500m1500m 之间的所有数据行之间进行分区, 1000m2000m...

我已经将数据存储到 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;} );
    }
}

注意: aPoint 类型。如果我尝试将 itp 存储到向量等结构中,我会收到以下错误:

error: no matching function for call to ‘std::vector<Point>::push_back(__gnu_cxx::__normal_iterator<Point*, std::vector<Point> >&)’

我只是不确定如何存储 itp。最终目标是计算特定深度 window(例如 1500m2500m)内数据点与所有其他数据点之间的距离。对此新手的任何帮助将不胜感激。

std::partition returns 分区元素组之间的分隔点处的迭代器,它是第二组的第一个元素。如果你想把它存储在另一个vector,向量类型应该是

std::vector<std::vector<Point>::iterator>

您使用它的方式,在后续调用分区时,您不想对整个向量进行分区,而只是对其中具有较大元素的部分进行分区(因为向量中的早期元素现在是较低的元素,您不需要在以后的分区调用中包含它们,因为它们就在它们应该在的位置,并且 partition 描述中没有任何内容表明它们不会被移动)。因此,i 循环的后续迭代中的第一个元素应该是前一个循环中返回的 itp 迭代器。

首先,让我们做一个简单的案例来说明您的问题:

struct Point { int bathyDepth; }; // this is all, what you need to show    

int main()
{
    // some points
    Point a{ 1 }, b{ 100 }, c{ 1000 }, d{ 2000 }, e{ 3000 }, f{ 4000 }, g{ 4501 }, h{ 400 }, i{ 1600 }, j{ 2200 }, k{ 700 };
    // one map element
    std::map<std::string, std::vector<Point> > seamap
    { {"Nat", std::vector<Point>{a, b, c, d, e, f, g, h, i, j, k}} };

    //Partition basins by depth
    for (auto it2= seamap.begin(); it2!= seamap.end(); ++it2)
    {
        int i = 500; // some range
        auto itp = std::partition(it2->second.begin(), it2->second.end(), [&i](const auto &a) {return a.bathyDepth < i; });    
    }

    return 0;
}

I'm just unsure of how to store itp.

要存储,你只需要知道它的类型。 等于 decltype(it2->second)::iterator,因为 std::partition returns 容器的迭代器类型。

因为您的地图 key_typestd::vector<Point>,它等于 std::vector<Point>::iterator

您可以通过编程方式对其进行测试:

if (std::is_same<decltype(it2->second)::iterator, decltype(itp)>::value)
   std::cout << "Same type";

这意味着您可以将 itp 存储在

using itpType = std::vector<Point>::iterator;
std::vector<itpType> itpVec;
// or any other containers, with itpType

The end-goal is to calculate the distance between a data point and all the other data points within a particular depth window (e.g. 1500 to 2500m).

如果是这样,您只需根据 bathyDepth 对地图的值 (std::vector<Point>) 进行排序,然后遍历它以找到所需的范围。当你使用 std::partition 时,在这个循环中

for (int i = 500; i<=4500; i+=500 )

最终效果/结果和一次排序一样,只是一步一步来。另外,请注意,要使用 std::partition 获得正确的结果,您需要排序 std::vector<Point>.

例如,参见example code here,它将打印您提到的范围。

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

struct Point
{
    int bathyDepth;
    // provide a operator< for std::sort()
    bool operator<(const Point &rhs)const { return this->bathyDepth < rhs.bathyDepth; }
};
// overloaded  << operator for printing #bathyDepth
std::ostream& operator<<(std::ostream &out, const Point &point) { return out << point.bathyDepth; }

//function for printing/ acceing the range
void printRange(const std::vector<Point>& vec, const int rangeStart, const int rangeEnd)
{
    for (const Point& element : vec)
    {
        if (rangeStart <= element.bathyDepth && element.bathyDepth < rangeEnd) std::cout << element << " ";
        else if (element.bathyDepth > rangeEnd) break; // no need for further checking
    }
    std::cout << "\n";
}

int main()
{
    Point a{ 1 }, b{ 100 }, c{ 1000 }, d{ 2000 }, e{ 3000 }, f{ 4000 }, g{ 4501 }, h{ 400 }, i{ 1600 }, j{ 2200 }, k{ 700 };
    std::map<std::string, std::vector<Point> > seamap
    { {"Nat", std::vector<Point>{a, b, c, d, e, f, g, h, i, j, k}} };

    for (auto it2 = seamap.begin(); it2 != seamap.end(); ++it2)
    {
        // sort it
        std::sort(it2->second.begin(), it2->second.end());
        //Partition basins by depth
        for (int i = 0; i < 4500; i += 500)  printRange(it2->second, i, i + 500);
    }
    return 0;
}

输出:

1 100 400 
700 
1000 
1600 
2000 2200 
                      // no elements in this range
3000 
                      // no elements in this range
4000