本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 求解std::vector
原文链接:https://www.stubbornhuang.com/1889/
发布于:2022年01月06日 11:08:44
修改于:2022年01月06日 11:09:09

1 C++获取vector中topk数值以及topk数值对应的索引
1.1 思路
topk问题是日常应用经常会遇到的问题,在python中对于多分类问题,经常需要输出top5的标签,在C++中,topk问题也是经常面对的问题。
比如给定一个std::vector<float>
的容器,需要找出该容器中最大的top5的数值以及这5个数值在该容器中的索引,对于这个问题可以使用std::sort函数进行解决,我将求解TopK数值对应索引的函数进行了如下的封装:
std::vector<int> TopKIndex(const std::vector<float>& vec,int topk)
{
std::vector<int> topKIndex;
topKIndex.clear();
std::vector<size_t> vec_index(vec.size());
std::iota(vec_index.begin(), vec_index.end(), 0);
std::sort(vec_index.begin(), vec_index.end(), [&vec](size_t index_1, size_t index_2) { return vec[index_1] > vec[index_2]; });
int k_num = std::min<int>(vec.size(), topk);
for (int i = 0; i < k_num; ++i)
{
topKIndex.emplace_back(vec_index[i]);
}
return topKIndex;
}
在上述代码中,先声明一个与原始数值容器等长的std::vector<size_t>
容器,用于存储topk数值对应的索引,然后通过std::iota
将该索引容器初始化为{0,1,2,......}
的形式,然后使用std::sort
方法通过比较原始数值容器的元素的大小去更新索引容器中索引的顺序。
1.2 示例
以下代码是求解std::vector<float>
容器中top5数值以及对应索引的示例。
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <iterator>
std::vector<int> TopKIndex(const std::vector<float>& vec,int topk)
{
std::vector<int> topKIndex;
topKIndex.clear();
std::vector<size_t> vec_index(vec.size());
std::iota(vec_index.begin(), vec_index.end(), 0);
std::sort(vec_index.begin(), vec_index.end(), [&vec](size_t index_1, size_t index_2) { return vec[index_1] > vec[index_2]; });
int k_num = std::min<int>(vec.size(), topk);
for (int i = 0; i < k_num; ++i)
{
topKIndex.emplace_back(vec_index[i]);
}
return topKIndex;
}
int main()
{
std::vector<float> testVec = { 1.0,0.9,2.0,89.6,3.0,4.0,4.1,4.3,115.3,4.9,5.9,10.2,11.6,9.5,25.3,99.3 };
std::vector<int> indexResult = TopKIndex(testVec,5);
std::cout << "原数组Top5数值为:" << std::endl;
for (int i = 0; i < indexResult.size(); ++i)
{
std::cout << testVec[indexResult[i]] << std::endl;
}
std::cout << "原数组Top5数值对应的索引为:" << std::endl;
for (int i = 0; i < indexResult.size(); ++i)
{
std::cout << indexResult[i] << std::endl;
}
int a = 1;
return 0;
}
运行结果:

当前分类随机文章推荐
- C++ – 字节数组byte[]或者unsigned char[]与long double的相互转换 阅读427次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与double的相互转换 阅读590次,点赞0次
- C++ - 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点 阅读665次,点赞0次
- C++STL容器 - std::map删除指定元素 阅读580次,点赞0次
- C++ - 随机洗牌算法,std::random_shuffle和std::shuffle 阅读329次,点赞0次
- C++ - 线程安全的std::cout 阅读1271次,点赞0次
- C++ - 得到字符串中某个字符串出现的个数 阅读2719次,点赞1次
- C++ – 字节数组byte[]或者unsigned char[]与float的相互转换 阅读548次,点赞0次
- C++ - 函数返回多个返回值的方法总结 阅读810次,点赞0次
- C++ - int转string方法总结 阅读4617次,点赞0次
全站随机文章推荐
- 深度学习 - Transformer详细注释 阅读64次,点赞0次
- WordPress - wp_login_url函数详解 阅读710次,点赞0次
- 资源分享 - 深度学习之PyTorch实战计算机视觉 (唐进民著) 高清PDF下载 阅读1334次,点赞0次
- 资源分享 - Computational Geometry on Surfaces - Performing Computational Geometry on the Cylinder, the Sphere, the Torus, and the Cone 英文高清PDF下载 阅读1322次,点赞0次
- 资源分享 - Natural Language Processing in Action 英文高清PDF下载 阅读64次,点赞0次
- Duilib - RichEdit和List等控件增加垂直滚动条vscrollbar和水平滚动条hscrollbar 阅读834次,点赞2次
- Python - 配置Yolov5出现ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'错误 阅读559次,点赞0次
- Python - 普通函数/lambda匿名函数/类成员函数作为回调函数的用法 阅读1520次,点赞0次
- 深度学习 - 卷积神经网络CNN简介 阅读58次,点赞0次
- 资源分享 - Vulkan Cookbook - Work through recipes to unlock the full potential of the next generation graphics API-Vulkan 英文高清PDF下载 阅读1414次,点赞0次
评论
150