当前位置: 首页 > 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.dtcms.com/a/296898.html

相关文章:

  • Linux 进程间通信:共享内存详解
  • 2025年人形机器人动捕技术研讨会于7月31日在京召开
  • 如何使用 pdfMake 中文字体
  • Next.js 中配置不同页面布局方案
  • 无锡市亨达电机盛装亮相 2025上海生物发酵展引关注
  • 深入理解大语言模型生成参数:temperature、top\_k、top\_p 等全解析
  • 首发即开源!DAWorkBench数据可视化分析软件正式发布!(附源码下载网址)
  • ubuntu安装teams解决方法
  • JavaScript中this的5大核心规则详解
  • vue 项目中 components 和 views 包下的组件功能区别对比,示例演示
  • Eureka-服务注册,服务发现
  • CSDN技术专栏开篇:高效开发环境搭建指南
  • Android Activity与Fragment生命周期变化
  • 深度学习(鱼书)day01--感知机
  • springboot实战篇2
  • 磁悬浮转子不平衡质量控制:比例谐振控制器深度解析
  • iOS网络之异步加载
  • Win10系统自带输入法打字,莫名切全角英文字母变大问题
  • Linux驱动18 --- LCD 屏
  • Ubuntu同一网段下配置多个雷达
  • 大模型开发框架LangChain之集成MCP工具
  • MC0461排队
  • 【时时三省】(C语言基础)怎样定义和使用指向函数的指针变量
  • 深入解析Java微服务架构请求流程:Nginx到Nacos的完整旅程
  • 数据库期中复习
  • JSONObject相关知识点
  • 嵌入式通信知识串讲:从同步 / 异步传输到 UART 协议 STM32F103 硬件解析
  • 大模型提示词漏洞攻防测试:技术分析与实践指南
  • 客户关系管理(CRM)百科:定义、价值及发展趋势
  • JMeter 性能测试实战笔记