c++ - 访问 map 中的分区 vector

标签 c++ c++11 stdvector stdmap partition

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

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

我创建了一个 map ,它有一个基于字符串(海洋盆地的名称)的键和一个包含数据 vector 的值。现在我需要按 bathyDepth 对 vector 进行排序。准确地说,我想对 vector 进行分区,以便我可以在深度在 0500m 之间的所有数据行之间进行分区,500m1500m, 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 存储到诸如 vector 的结构中,我会收到以下错误:

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

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

最佳答案

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

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 返回容器的迭代器类型。

因为,您的 map 的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).

如果是这样,您只需根据 std::vector<Point> 对 map 的值 ( bathyDepth ) 进行排序并遍历它以找到所需的范围。当您使用 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

关于c++ - 访问 map 中的分区 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138216/

相关文章:

c++ - 使用堆栈将成员变量重置为其原始值的一般方法?

c++ - 调用没有对象参数编译器错误的非静态成员函数

c++ - 数组元素到可变参数模板参数

c++ - 在 C++ 中的 push_back 之前将字符串转换为 float

c++ - std::find 是否隐式修复无效参数?

c++ - 如何将一系列数据从 char 数组复制到 vector 中?

c++ - 是否存在程序员可能希望避免 bool 表达式的短路求值的合理场景?

C++ 暴力破解程序非常慢

c++ - 没有新字段的派生类的非虚拟析构函数c++

c++ - 为什么将 T 从外部模板作为默认参数传递给 std::function 会导致编译错误?