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