当前位置: 首页 > news >正文

C++ 刷题 使用到的一些有用的容器和函数

 优先队列

c++优先队列priority_queue(自定义比较函数)_c++优先队列自定义比较-CSDN博客

373. 查找和最小的 K 对数字 - 力扣(LeetCode)

官方题解:

class Solution {
public:vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {auto cmp = [&nums1,&nums2](const pair<int,int>& a,const pair<int,int>& b) {return nums1[a.first] + nums2[a.second] > nums1[b.first] + nums2[b.second];};int m = nums1.size();int n = nums2.size();vector<vector<int>> ans;priority_queue<pair<int,int>,vector<pair<int,int>>,decltype(cmp)> pq(cmp);for(int i = 0;i < min(k,m);i++) {pq.emplace(i,0);}while(k-- > 0 && !pq.empty()) {auto [x,y] = pq.top();pq.pop();ans.emplace_back(initializer_list<int>{nums1[x],nums2[y]});if(y + 1 < n) {pq.emplace(x,y + 1);}}return ans;}
};

c++优先队列(priority_queue)用法详解_c++ 优先队列-CSDN博客

count函数

[C++] 基础教程 - std::count函数介绍和使用场景_std::cout-CSDN博客

std::count 是C++标准库的一个算法,用来计算给定值在指定范围内出现的次数

template <class InputIt, class T>
size_t count(InputIt first, InputIt last, const T& value);

使用场景:

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> nums = {1, 2, 3, 4, 5, 2, 3, 2};int target = 2;size_t count = std::count(nums.begin(), nums.end(), target);std::cout << "The number " << target << " appears " << count << " times in the array." << std::endl;return 0;
}
#include <iostream>
#include <string>
#include <algorithm>int main() {std::string str = "hello world";char target = 'l';size_t count = std::count(str.begin(), str.end(), target);std::cout << "The character '" << target << "' appears " << count << " times in the string." << std::endl;return 0;
}

http://www.lryc.cn/news/446921.html

相关文章:

  • 【Kubernetes】常见面试题汇总(三十四)
  • C++标准库双向链表 list 中的insert函数实现。
  • 华为机考练习(golang)
  • 51单片机快速入门之按键应用拓展
  • 数据库 - MySQL的事务
  • 【Python机器学习】NLP信息提取——提取人物/事物关系
  • vector类
  • python常见的魔术方法
  • 自动化测试常用函数:弹窗、等待、导航、上传与参数设置
  • 【必看】2024国赛选题分布情况分析及数模国赛答辩指南~答辩不走弯路
  • 微服务注册中⼼1
  • 我设置了路由器自动切换ip,这会让我的账号登录地址经常改变吗
  • Nginx 限流实战教程和技巧
  • AlphaFold3 | 详解 AlphaFold3 的模型结构及其在不同类型的预测实验中的表现
  • 公交IC卡收单管理系统 多处 SQL注入致RCE漏洞复现
  • 淘客系统开发之卷轴模式系统源码功能分析
  • MoCo中的字典
  • Xcode16 iOS18 编译问题适配
  • 加密解密的艺术:探索Java中的DES算法
  • jQuery——层次选择器
  • MySQL常见面试总结
  • 记录一次学习--委派攻击学习
  • 前端列表数据太多导致页面卡顿就这么处理
  • 机器学习_神经网络_深度学习
  • MT6765/MT6762(R/D/M)/MT6761(MT8766)安卓核心板参数比较_MTK联发科4G智能模块
  • TikTok五分钟开户快速步骤流程!
  • BFS 解决拓扑排序 , 课程表 , 课程表 II , 火星词典
  • web安全攻防渗透测试实战指南_web安全攻防渗透测试实战指南,零基础入门到精通,收藏这一篇就够了
  • 大模型如何赋能智慧城市新发展?
  • 随记——机器学习