C++ 求向量的向量中的最大位置

C++ 求向量的向量中的最大位置,c++,vector,C++,Vector,我有一个向量向量向量 std::vector<std::vector<std::vector<double>>> mountain_table std::vector mountain\u表 我想找到这个向量的坐标I,j,k,它是最高的。我知道我应该使用max\u元素,但我不知道如何在3d向量中使用它 for (size_t i = 0; i <mountain_table.size(); ++i) { for (size_t j = 0; j

我有一个向量向量向量

std::vector<std::vector<std::vector<double>>> mountain_table
std::vector mountain\u表
我想找到这个向量的坐标I,j,k,它是最高的。我知道我应该使用
max\u元素
,但我不知道如何在3d向量中使用它

for (size_t i = 0; i <mountain_table.size(); ++i)
{
  for (size_t j = 0; j < mountain_table[i].size() ++j)
    {
     // find max element index k here and check if it is maximum.
     // If yes save i, j, k and update max val
    }
}
如何获取这些坐标?

您应该使用“for”循环,因为您没有3d矢量

std::size_t rv[3] = {0};
std::size_t i = 0;
double max_value = mountain_table[0][0][0];
for (const auto& x : mountain_table) {
  std::size_t j = 0;
  for (const auto& y : x) {
    auto it = std::max_element(y.begin(), y.end());
    if (*it > max_value) {
      rv[0] = i; rv[1] = j; rv[2] = it - y.begin();
      max_value = *it;
    }
    ++j;
  }
  ++i;
}
for (size_t i = 0; i <mountain_table.size(); ++i)
{
  for (size_t j = 0; j < mountain_table[i].size() ++j)
    {
     // find max element index k here and check if it is maximum.
     // If yes save i, j, k and update max val
    }
}

for(size_t i=0;i下面是我将如何做的,通过在矩阵上循环,检查最高值,并记录其索引

size_t highestI = 0;
size_t highestJ = 0;
size_t highestK = 0;

double highestValue = -std::numeric_limits<double>::infinity(); // Default value (Include <limits>)

for (size_t i = 0; i < mountain_table.size(); ++i)
{
    for (size_t j = 0; j < mountain_table[i].size(); ++j)
    {
        for (size_t k = 0; k < mountain_table[i][j].size(); ++k)
        {
            if (mountain_table[i][j][k] > highestValue)
            {
                highestValue = mountain_table[i][j][k]; // Highest 
                // value needed to figure out highest indexes
                // Stores the current highest indexes
                highestI = i; 
                highestJ = j;
                highestK = k;
            }
        }
    }
}
size\u t highestI=0;
最大尺寸j=0;
最大尺寸k=0;
double highestValue=-std::numeric_limits::infinity();//默认值(包括)
对于(大小i=0;i最高值)
{
highestValue=山_表[i][j][k];//最高
//计算最高索引所需的值
//存储当前最高的索引
highestI=i;
最高j=j;
最高k=k;
}
}
}
}
这可能不是最有效的算法,但它以一种可以理解的方式完成了任务。

由于函数非常简短且易于实现,我建议您自己编写类似的代码,以适合您的具体场景

// For types like this I would suggest using a type alias
using Vector3d = std::vector<std::vector<std::vector<double>>>;

std::array<size_t, 3> max_element(const Vector3d& vector) {
    std::std::array<size_t, 3> indexes;
    double biggest = vector[0][0][0];
    for (unsigned i = 0; i < vector.size(); ++i)
        for (unsigned j = 0; j < vector[i].size(); ++j)
            for (unsigned k = 0; k < vector[i][j].size(); ++k)
                if (value > biggest) {
                    biggest =  value;
                    indexes = { i, j, k };
                }
    return indexes;
}
//对于这样的类型,我建议使用类型别名
使用Vector3d=std::vector;
std::数组最大元素(常量向量3D和向量){
std::std::数组索引;
双最大=向量[0][0][0];
for(无符号i=0;i最大值){
最大=价值;
索引={i,j,k};
}
收益指标;
}

我可以给您的另一个建议是编写自定义类
Vector3d
,使用方便的函数,如
operator()(intx,inty,intz)
等,并在内部以大小
宽度*高度*深度
的简单
矢量
保存数据。我建议将数据线性化,以便能够使用标准算法。其想法是提供两个函数,从3D坐标中获取索引,反之亦然:

模板
类Matrix3D//minimal
{
公众:
使用值_type=T;
使用迭代器=std::vector::迭代器;
私人:
std::vector_数据;
大小,大小,大小;
大小索引(大小x,大小y,大小z)常数
{
返回x*\u sizex*\u sizey+y*\u sizey+z;
}
std::tuple coords_from_index(size_t index)const
{
const size\u t x=索引/(\u sizex*\u sizey);
索引=索引%x;
const size\u t y=索引/大小;
const size\u t z=索引%\u sizey;
返回make_元组(x,y,z);
}
公众:
Matrix3D(大小、大小、大小):\u-sizex(大小),…{}
运算符()(大小x,大小y,大小z)//添加常量版本
{
返回_数据[来自_坐标(x,y,z)的索引_];
}
std::元组坐标(迭代器it)
{
大小索引=标准::距离(开始(_数据),it);
从索引返回坐标(索引);
}
迭代器begin(){返回begin(_data);}
迭代器end(){return end(_data);}
}
用法:

Matrix3D<double> m(3, 3, 3);
auto it = std::max_element(m.begin(), m.end()); // or min, or whatever from http://en.cppreference.com/w/cpp/header/algorithm
auto coords = m.coords(it);
std::cout << "x=" << coords.get<0>() << ... << "\n";
Matrix3D-m(3,3,3);
auto it=std::max_元素(m.begin(),m.end());//或min,或来自http://en.cppreference.com/w/cpp/header/algorithm
自动坐标=m.coords(it);

std::cout我不认为您可以使用
std::max\u element
来处理此类数据。您可以使用
std::accumulate()

使用dvect=std::vector;
使用ddvect=std::vector;
使用dddvect=std::vector;
dddvect mx={{{1,2,3},{-1,3},{8,-2,3},
{ {}, { -1, 25, 3 }, { 7, 3, 3 } },
{ { -1, -2, -3 }, {}, { 33 } } };
结构最大值{
尺寸i=0;
尺寸j=0;
尺寸k=0;
double value=-std::numeric_limits::infinity();
最大值()=默认值;
最大值(大小i,大小j,大小k,双v):i(i),j(j),k(k),值(v){}

最大值运算符或使用c++11。对于最大坐标,我指的是Mountain_table[I][j][k],其值(双精度)为最大值。您不能使用
max_元素
,因为标准算法适用于范围,而您的数据不是以这种方式排列的。我建议您将数据线性化。@Mar我建议您编辑您的问题,您的“max coordinates”语句非常令人困惑。我认为答案强调了您应该在多大程度上更改您的数据组织…当两个值相同时,您希望返回什么?我认为您不需要在这里回答这个问题;但您应该知道答案。键入“don’t”->“do”"?最明显的是
最高值
应该是
双精度
@NickA我的错误;修复it@Salvasize_t是无符号的,有3个比较?@ArnavBorborah您将-1赋值给无符号变量?@KillzoneKid也做了相应的更改。这不支持锯齿矩阵though@Default的确!A
std::map
将用于支持解析矩阵。但由于OP不需要它;)真的吗?我很感兴趣,但不幸的是我无法将其可视化。您介意给出一个map如何解决锯齿数组的提示吗?@默认值只需将
std::vector
替换为
std::map
\u data[index]
仍然有效,但会有不同的含义,即按需创建值。它可以是std::MAP。您如何知道您可以访问
向量[0][0][0]
?@Slava我不知道,这个示例不是我自己使用的确切代码,而是
using dvect = std::vector<double>;
using ddvect = std::vector<dvect>;
using dddvect = std::vector<ddvect>;
dddvect mx = { { { 1, 2, 3 }, { -1, 3 }, { 8,-2, 3 } },
               { {}, { -1, 25, 3 }, { 7, 3, 3 } },
               { { -1, -2, -3 }, {}, { 33 } } };

struct max_value {
    size_t i = 0;
    size_t j = 0;
    size_t k = 0;

    double value = -std::numeric_limits<double>::infinity();

    max_value() = default;
    max_value( size_t i, size_t j, size_t k, double v ) : i( i ), j( j ), k( k ), value( v ) {}

    max_value operator<<( const max_value &v ) const
    {
        return value > v.value ? *this : v;
    }

};

auto max = std::accumulate( mx.begin(), mx.end(), max_value{}, [&mx]( const max_value &val, const ddvect &ddv ) {
    auto i = std::distance( &*mx.cbegin(), &ddv );
    return std::accumulate( ddv.begin(), ddv.end(), val, [i,&ddv]( const max_value &val, const dvect &dv ) {
        auto j = std::distance( &*ddv.cbegin(), &dv );
        return std::accumulate( dv.begin(), dv.end(), val, [i,j,&dv]( const max_value &val, const double &d ) {
            auto k = std::distance( &*dv.cbegin(), &d );
            return val << max_value( i, j, k, d );
        } );
    } );
} );