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

C++标准库算法实战指南

基于C++标准库算法

以下是基于C++标准库算法(Algorithm)的实用示例,涵盖常见应用场景和关键函数用法:


排序与搜索

sort: 对容器进行升序排序

vector<int> v = {4, 2, 5, 3, 1};
sort(v.begin(), v.end()); // 结果: {1, 2, 3, 4, 5}

binary_search: 在已排序范围内查找元素

bool found = binary_search(v.begin(), v.end(), 3); // 返回true

lower_bound: 查找第一个不小于给定值的元素

auto it = lower_bound(v.begin(), v.end(), 3); // 指向第一个3


数值运算

accumulate: 计算容器内元素总和

int sum = accumulate(v.begin(), v.end(), 0); // 15

partial_sum: 计算部分和

vector<int> result(5);
partial_sum(v.begin(), v.end(), result.begin()); // {1,3,6,10,15}

inner_product: 计算两个序列的点积

vector<int> a = {1, 2}, b = {3, 4};
int dot = inner_product(a.begin(), a.end(), b.begin(), 0); // 11


容器操作

copy: 复制元素到新容器

vector<int> target(5);
copy(v.begin(), v.end(), target.begin());

fill: 用指定值填充容器

fill(target.begin(), target.end(), 0); // {0,0,0,0,0}

generate: 通过函数生成值

int n = 0;
generate(target.begin(), target.end(), [&n]{ return n++; }); // {0,1,2,3,4}


查找与判断

find_if: 查找满足条件的元素

auto it = find_if(v.begin(), v.end(), [](int x){ return x > 3; }); // 指向4

count: 统计特定值出现次数

int cnt = count(v.begin(), v.end(), 3); // 1

all_of/any_of: 判断元素是否全部/部分满足条件

bool all_pos = all_of(v.begin(), v.end(), [](int x){ return x > 0; }); // true


集合操作

set_union: 计算两个有序集合的并集

vector<int> a = {1,2}, b = {2,3}, out(4);
set_union(a.begin(),a.end(),b.begin(),b.end(),out.begin()); // {1,2,3,0}

set_intersection: 计算有序集合的交集

set_intersection(a.begin(),a.end(),b.begin(),b.end(),out.begin()); // {2,0,0,0}

merge: 合并两个有序序列

merge(a.begin(),a.end(),b.begin(),b.end(),out.begin()); // {1,2,2,3}


元素处理

transform: 对每个元素应用函数

vector<int> squares(5);
transform(v.begin(), v.end(), squares.begin(), [](int x){ return x*x; }); // {1,4,9,16,25}

replace: 替换特定值

replace(v.begin(), v.end(), 3, 10); // {1,2,10,4,5}

remove_if: 移除满足条件的元素

auto new_end = remove_if(v.begin(), v.end(), [](int x){ return x%2==0; }); // 移除偶数


排列组合

next_permutation: 生成下一个排列

string s = "abc";
do {cout << s << endl;
} while(next_permutation(s.begin(), s.end()));

prev_permutation: 生成上一个排列

sort(s.rbegin(), s.rend());
do {cout << s << endl;
} while(prev_permutation(s.begin(), s.end()));


堆操作

make_heap: 将容器转为堆结构

vector<int> heap = {3,1,4,2};
make_heap(heap.begin(), heap.end()); // {4,2,3,1}

push_heap: 向堆中添加元素

heap.push_back(5);
push_heap(heap.begin(), heap.end()); // {5,4,3,1,2}

pop_heap: 从堆中移除最大元素

pop_heap(heap.begin(), heap.end());
int max_val = heap.back(); // 5


其他实用算法

shuffle: 随机打乱序列

random_device rd;
mt19937 g(rd());
shuffle(v.begin(), v.end(), g);

unique: 移除相邻重复元素

vector<int> dup = {1,2,2,3};
auto last = unique(dup.begin(), dup.end()); // {1,2,3,2}

rotate: 旋转容器元素

rotate(v.begin(), v.begin()+2, v.end()); // 左旋2位

C++机器翻译基础示例

以下是一些基础的C++代码片段,展示如何实现简单的机器翻译功能:

示例1:使用字典进行单词翻译

#include <iostream>
#include <unordered_map>
using namespace std;unordered_map<string, string> dictionary = {{"hello", "你好"},{"world", "世界"}
};string translateWord(const string& word) {if(dictionary.find(word) != dictionary.end()) {return dictionary[word];}return word;
}

示例2:简单句子分词和翻译

#include <sstream>
#include <vector>vector<string> splitSentence(const string& sentence) {vector<string> words;istringstream iss(sentence);string word;while(iss >> word) {words.push_back(word);}return words
http://www.lryc.cn/news/598444.html

相关文章:

  • Java基础day16-Vector类-Stack类-Collection子接口Set接口
  • 基础NLP | 02 深度学习基本原理
  • EasyExcel 模板导出数据 + 自定义策略(合并单元格)
  • 亚马逊云科技 EC2 部署 Dify,集成 Amazon Bedrock 构建生成式 AI 应用
  • 货车手机远程启动的扩展功能有哪些
  • QML 模型
  • java如何声明函数
  • Vulnhub Matrix-Breakout-2-Morpheus靶机攻略
  • jd h5st参数纯算
  • 现代C++的一般编程规范
  • Linux内核中动态内存分配函数解析
  • MYSQL中NOT IN和NOT EXISTS
  • 【Guava】1.1.我的报告
  • 宝塔通过docker部署JupyterHub指南【常见错误处理】
  • 从java到vue3:第二天
  • Vue3 面试题及详细答案120道(91-105 )
  • 个人笔记GUI
  • 【Python】Python多线程爬虫实战:从基础原理到分布式架构实现
  • Linux 基本命令整理
  • #来昇腾学AI 【十天成长计划】大模型LLM Prompt初级班
  • 详解力扣高频 SQL 50 题-1757.可回收且低脂的产品【入门】
  • 保障工业核心命脉:深度解读工业交换机QoS的“智能流量治理”之道
  • docker设置字体及时间,映射到宿主机上
  • rustfs/rustfs基于 Rust 的高性能分布式存储系统
  • 数字系统自动设计:从C++到门级网表
  • EXCEL——INDEX和MATCH傻傻分不清?
  • 基于QT(C++)实现(图形界面)选课管理系统
  • 网易大模型算法面经总结第一篇
  • 【News】同为科技亮相首届气象经济博览会
  • Qt 元对象系统(Meta-Object System)解析