简体   繁体   English

C ++中结构数组元素的最小值和索引

[英]min value and index from elements of array of structure in C++

The problem: 问题:

#include <iostream>
#include <algorithm>
using namespace std;

struct abc
{
   int cost;
   int any;
};

int main() {
abc *var1 = new abc[5];
   var1[0].cost = 4;
   var1[1].cost = 42;
   var1[2].cost = 5;
   var1[3].cost = 0;
   var1[4].cost = 12;

  //       cout<< "value = " << *std::min_element(var1.cost,var1.cost+5) << endl;
  //       cout << "Position = " << (std::min_element(var1.cost,var1.cost+5)-var1.cost) << endl;
    return 0;
}

How to find minimum value and position of var1[].cost? 如何找到var1 []。cost的最小值和位置? is it possible to find this using std::min_element? 可以使用std :: min_element找到它吗?

std::min_element - cppreference.com std :: min_element-cppreference.com

You can use a comparision function object to have std::min_element look at the member cost . 您可以使用比较函数对象让std::min_element查看成员cost

#include <iostream>
#include <algorithm>
using namespace std;

struct abc
{
   int cost;
   int any;
};

struct cmp_abc {
  bool operator()(const abc& a, const abc& b) const {
    return a.cost < b.cost;
  }
};

int main() {
   abc *var1 = new abc[5];
   var1[0].cost = 4;
   var1[1].cost = 42;
   var1[2].cost = 5;
   var1[3].cost = 0;
   var1[4].cost = 12;

   abc *res = std::min_element(var1, var1 + 5, cmp_abc());

   cout << "value = " << res->cost << endl;
   cout << "Position = " << (res - var1) << endl;

   delete[] var1;
   return 0;
}

I can think of at least four ways to do this with std::min_element 我可以想到至少四种方法来使用std::min_element

1) Add a "less than" member function to the struct/class: 1)向结构/类添加“小于”成员函数:

struct abc
{
    int cost;
    int any;

    bool operator<(const abc &other) const { // member function
        return cost < other.cost;
    }
};

int main() {
    // ...
    // The algorithm will find and use the class operator< by default
    abc *ptr = std::min_element(var1, var1 + 5);
}

2) Define a free function: 2)定义一个自由函数:

bool abc_less(const abc &lhs, const abc &rhs) // free function
{
    return lhs.cost < rhs.cost;
}

int main() {
    // ...
    // Pass a function pointer to the algorithm
    abc *ptr = std::min_element(var1, var1 + 5, abc_less);
}

3) Define a function object type: 3)定义一个功能对象类型:

struct abc_less // function object
{
    bool operator()(const abc &lhs, const abc &rhs) const {
        return lhs.cost < rhs.cost;
    }
};

int main() {
    // ...
    // Construct and pass a function object to the algorithm    
    abc *ptr = std::min_element(var1, var1 + 5, abc_less());
}

4) Create a lambda function: 4)创建一个lambda函数:

int main() {
    // ...
    // Create a lambda at the point of call in this example
    abc *ptr = std::min_element(var1, var1 + 5, [](const abc &lhs, const abc &rhs) { return lhs.cost < rhs.cost; });
}

Finally, use the returned iterator (pointer in this case) to print the value or offset: 最后,使用返回的迭代器(在这种情况下为指针)来打印值或偏移量:

std::cout << "Value = " << ptr->cost << '\n';
std::cout << "Position = " << (ptr - var1) << '\n'; // or std::distance(var1, ptr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM