gpt4 book ai didi

c++ - 如何找到元组 vector 中的最大值?

转载 作者:行者123 更新时间:2023-11-28 05:33:19 25 4
gpt4 key购买 nike

考虑:

vector<tuple<int, int, char>> array;
for(int i=0; i<m-1; i++)
{
long long int p;
cin >> p;
get<0>(x) = p; // Updated value
get<1>(x) = get<0>(x); // Real value
get<2>(x) = 'v';
array.push_back(x);
}
sort(array.begin(), array.end());
reverse(array.begin(), array.end());
// How can I get the maximum of the second field
// to to element with largest first field
// and then delete the tuple?

假设我有一个数据类型为元组的数组,如 int, int, char

例如,(5,5,h);(5,2,h);(5,7,v);( 3,1,h);(3,7,h);(1,1 ,v).

它已经根据第一个值降序排列。现在我想找到包含最大的第一个值和最大的第二个值的部分?所以我只需要搜索前三部分。有没有办法在这里使用 max() 或任何其他 STL 函数?

最佳答案

std::tuple::operator <执行字典序比较 ( Cplusplus.com )。这意味着,在 sort 之后和 reverse array的第0项包含 tuple在所有具有最大第一个元素的元组中具有最大第二个元素的所有元组中具有最大的第三个元素,这就是您所需要的。

所以,只需返回 array[0] .

关于c++ - 如何找到元组 vector 中的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38918978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com