怎样网站制作设计,网站制作公司商丘市,施工企业安全团建小游戏,网络平台贷款逾期不还的后果目录 一、算法概述二、代码实现1、计算最值2、计算最值及其索引 三、结果展示 本文由CSDN点云侠原创#xff0c;原文链接。如果你不是在点云侠的博客中看到该文章#xff0c;那么此处便是不要脸的爬虫与GPT。
一、算法概述 类似下图所示#xff0c;计算第一列中1或2对应的最… 目录 一、算法概述二、代码实现1、计算最值2、计算最值及其索引 三、结果展示 本文由CSDN点云侠原创原文链接。如果你不是在点云侠的博客中看到该文章那么此处便是不要脸的爬虫与GPT。
一、算法概述 类似下图所示计算第一列中1或2对应的最大值。
二、代码实现
1、计算最值
#include map
#include vector
#include iostreamint main()
{// 示例数据std::vectorstd::pairint, int data { {1, 100}, {1, 101}, {1, 102}, {2, 100}, {2, 101}, {2, 102} };// 使用std::map来存储每个键的最大值std::mapint, int maxValues;// 迭代数据for (const auto pair : data) {// 如果这个键还没有在map中或者当前值大于map中存储的值更新它if (maxValues.find(pair.first) maxValues.end() || pair.second maxValues[pair.first]) {maxValues[pair.first] pair.second;}}// 输出结果for (const auto maxPair : maxValues) {std::cout Column1 value maxPair.first has a maximum Column2 value of maxPair.second std::endl;}return 0;
}2、计算最值及其索引
#include map
#include vector
#include iostreamint main()
{// 示例数据每个pair是{第一列的值, 第二列的值}std::vectorstd::pairint, int data { {1, 100}, {1, 101}, {1, 102}, {2, 100}, {2, 101}, {2, 102} };// 使用std::map来存储每个键的最大值的索引std::mapint, int maxIndices;// 使用std::map来存储每个键的当前最大值std::mapint, int maxValues;// 迭代数据i 是行数索引for (int i 0; i data.size(); i) {const auto pair data[i];// 检查是否需要更新最大值和行数索引if (maxValues.find(pair.first) maxValues.end() || pair.second maxValues[pair.first]) {maxValues[pair.first] pair.second;maxIndices[pair.first] i; // 更新行数索引}}// 输出结果for (const auto maxIndex : maxIndices) {std::cout Column1 value maxIndex.first has a maximum Column2 value in row maxIndex.second std::endl;}return 0;
}三、结果展示
Column1 value 1 has a maximum Column2 value of 102
Column1 value 2 has a maximum Column2 value of 102Column1 value 1 has a maximum Column2 value in row 2
Column1 value 2 has a maximum Column2 value in row 5