C++ 標準庫中的 <algorithm> 頭文件算法總結

C++ 常用 <algorithm> 算法概覽

??C++ 標準庫中的 <algorithm> 頭文件提供了大量有用的算法,主要用于操作容器(如 vector, list, array 等)。這些算法通常通過迭代器來操作容器元素。

1. 非修改序列操作

std::all_of, std::any_of, std::none_of

#include <algorithm>
#include <vector>std::vector<int> v = {1, 2, 3, 4, 5};// 檢查所有元素是否滿足條件
bool all_even = std::all_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 檢查是否有任一元素滿足條件
bool any_even = std::any_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 檢查是否沒有元素滿足條件
bool none_even = std::none_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

std::for_each

std::vector<int> v = {1, 2, 3, 4, 5};// 對每個元素執行操作
std::for_each(v.begin(), v.end(), [](int &n){ n *= 2; });

std::count, std::count_if

std::vector<int> v = {1, 2, 3, 4, 5};// 計算等于3的元素個數
int count_3 = std::count(v.begin(), v.end(), 3);// 計算滿足條件的元素個數
int count_even = std::count_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

std::find, std::find_if, std::find_if_not

std::vector<int> v = {1, 2, 3, 4, 5};// 查找值為3的元素
auto it = std::find(v.begin(), v.end(), 3);// 查找第一個偶數
auto it_even = std::find_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 查找第一個非偶數
auto it_not_even = std::find_if_not(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

2. 修改序列操作

std::copy, std::copy_if

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dst(5);// 復制元素
std::copy(src.begin(), src.end(), dst.begin());// 條件復制
std::vector<int> dst_even;
std::copy_if(src.begin(), src.end(), std::back_inserter(dst_even), [](int i){ return i % 2 == 0; });

std::fill, std::fill_n

std::vector<int> v(5);// 填充所有元素為42
std::fill(v.begin(), v.end(), 42);// 填充前3個元素為10
std::fill_n(v.begin(), 3, 10);

std::transform

std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());// 對每個元素應用函數
std::transform(v.begin(), v.end(), result.begin(), [](int i){ return i * 2; });

std::replace, std::replace_if

std::vector<int> v = {1, 2, 3, 4, 5};// 替換所有3為10
std::replace(v.begin(), v.end(), 3, 10);// 替換所有偶數為0
std::replace_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; }, 0);

std::remove, std::remove_if

std::vector<int> v = {1, 2, 3, 4, 5, 6};// "移除"所有3(實際上是移動到最后,返回新的邏輯end)
auto new_end = std::remove(v.begin(), v.end(), 3);
v.erase(new_end, v.end()); // 真正刪除// "移除"所有偶數
new_end = std::remove_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });
v.erase(new_end, v.end());

3. 排序和相關操作

std::sort, std::stable_sort

std::vector<int> v = {5, 3, 1, 4, 2};// 默認升序排序
std::sort(v.begin(), v.end());// 自定義排序
std::sort(v.begin(), v.end(), [](int a, int b){ return a > b; }); // 降序// 穩定排序(保持相等元素的相對順序)
std::stable_sort(v.begin(), v.end());

std::partial_sort

std::vector<int> v = {5, 6, 1, 3, 2, 4};// 部分排序(前3個最小的元素)
std::partial_sort(v.begin(), v.begin() + 3, v.end());

std::nth_element

std::vector<int> v = {5, 6, 1, 3, 2, 4};// 使第n個元素處于正確位置
std::nth_element(v.begin(), v.begin() + 2, v.end());
// v[2]現在是排序后的正確元素,前面的都<=它,后面的都>=它

std::is_sorted, std::is_sorted_until

std::vector<int> v = {1, 2, 3, 4, 5};// 檢查是否已排序
bool sorted = std::is_sorted(v.begin(), v.end());// 查找第一個破壞排序的元素
auto it = std::is_sorted_until(v.begin(), v.end());

4. 二分搜索(必須在已排序的序列上使用)

std::lower_bound, std::upper_bound, std::equal_range

std::vector<int> v = {1, 2, 2, 3, 4, 5};// 查找第一個不小于3的元素
auto low = std::lower_bound(v.begin(), v.end(), 3);// 查找第一個大于3的元素
auto up = std::upper_bound(v.begin(), v.end(), 3);// 查找等于3的范圍
auto range = std::equal_range(v.begin(), v.end(), 3);

std::binary_search

std::vector<int> v = {1, 2, 3, 4, 5};// 檢查元素是否存在
bool found = std::binary_search(v.begin(), v.end(), 3);

5. 集合操作(必須在已排序的序列上使用)

std::merge

std::vector<int> v1 = {1, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> dst(v1.size() + v2.size());// 合并兩個已排序的序列
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), dst.begin());

std::includes, std::set_difference, std::set_intersection, std::set_union, std::set_symmetric_difference

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {2, 4, 6};std::vector<int> result;// 檢查v1是否包含v2的所有元素
bool includes = std::includes(v1.begin(), v1.end(), v2.begin(), v2.end());// 差集(v1 - v2)
std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result));// 交集
result.clear();
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));// 并集
result.clear();
std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));// 對稱差集(在v1或v2中但不同時在兩者中)
result.clear();
std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));

6. 堆操作

std::make_heap, std::push_heap, std::pop_heap, std::sort_heap

std::vector<int> v = {3, 1, 4, 1, 5, 9};// 構建最大堆
std::make_heap(v.begin(), v.end());// 添加元素到堆
v.push_back(6);
std::push_heap(v.begin(), v.end());// 移除堆頂元素
std::pop_heap(v.begin(), v.end());
v.pop_back();// 堆排序
std::sort_heap(v.begin(), v.end());

7. 最小/最大值操作

std::min_element, std::max_element, std::minmax_element

std::vector<int> v = {3, 1, 4, 1, 5, 9};// 查找最小元素
auto min_it = std::min_element(v.begin(), v.end());// 查找最大元素
auto max_it = std::max_element(v.begin(), v.end());// 同時查找最小和最大元素
auto minmax = std::minmax_element(v.begin(), v.end());

std::clamp (C++17)

int value = 15;
// 將值限制在10-20范圍內
int clamped = std::clamp(value, 10, 20); // 返回15
clamped = std::clamp(5, 10, 20); // 返回10
clamped = std::clamp(25, 10, 20); // 返回20

8. 排列操作

std::next_permutation, std::prev_permutation

std::vector<int> v = {1, 2, 3};// 生成下一個排列
do {// 處理當前排列
} while (std::next_permutation(v.begin(), v.end()));// 生成前一個排列
std::prev_permutation(v.begin(), v.end());

9. 其他有用算法

std::accumulate (來自 <numeric>)

#include <numeric>
std::vector<int> v = {1, 2, 3, 4, 5};// 求和
int sum = std::accumulate(v.begin(), v.end(), 0);// 自定義操作(如乘積)
int product = std::accumulate(v.begin(), v.end(), 1, [](int a, int b){ return a * b; });

std::inner_product (來自 <numeric>)

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};// 點積
int dot = std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);

std::iota (來自 <numeric>)

std::vector<int> v(5);// 填充序列值
std::iota(v.begin(), v.end(), 10); // v = {10, 11, 12, 13, 14}

這些算法可以大大提高C++編程效率,避免了手動編寫循環的繁瑣工作,同時通常比手寫循環更高效。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/75763.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/75763.shtml
英文地址,請注明出處:http://en.pswp.cn/web/75763.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

程序化廣告行業(84/89):4A廣告代理公司與行業資質解讀

程序化廣告行業&#xff08;84/89&#xff09;&#xff1a;4A廣告代理公司與行業資質解讀 大家好&#xff01;在探索程序化廣告行業的道路上&#xff0c;每一次知識的分享都是我們共同進步的階梯。一直以來&#xff0c;我都希望能和大家攜手前行&#xff0c;深入了解這個充滿機…

deepin使用autokey添加微信快捷鍵一鍵顯隱ctrl+alt+w

打開deepin商店&#xff0c;搜索快捷鍵&#xff0c;找到autokey 快捷鍵管理&#xff0c;點擊安裝 點擊右鍵新建文件夾 點擊右鍵新建腳本 打開腳本并添加以下內容 import subprocess import time# ------------------ 配置項 ------------------ WM_CLASS "wechat…

文件內容課堂總結

Spark SQL是Spark用于結構化數據處理的模塊&#xff0c;前身是Shark。Shark基于Hive開發&#xff0c;雖提升了SQL-on-Hadoop效率&#xff0c;但對Hive依賴過多。2014年6月1日Shark項目停止開發&#xff0c;團隊將資源投入Spark SQL項目。Spark SQL具有諸多優點&#xff0c;如擺…

Zotero PDF Translate 翻譯插件使用OpenAI API配置教程

PDF Translate&#xff1a;提升 Zotero 內置 PDF 閱讀器的翻譯功能 “PDF Translate” 是一款為 Zotero 設計的插件&#xff0c;旨在方便用戶在 Zotero 內置的 PDF 閱讀器中進行劃詞或段落翻譯&#xff0c;輔助閱讀外文文獻。 一、 安裝插件 下載插件&#xff1a; 訪問 PDF T…

火山引擎旗下的產品

用戶問的是火山引擎旗下的產品&#xff0c;我需要詳細列出各個類別下的產品。首先&#xff0c;我得確認火山引擎有哪些主要業務領域&#xff0c;比如云計算、大數據、人工智能這些。然后&#xff0c;每個領域下具體有哪些產品呢&#xff1f;比如云計算方面可能有云服務器、容器…

C/C++程序中實現Python綁定多種技術路線

在C/C程序中實現Python綁定有多種技術路線&#xff0c;選擇合適的方法取決于項目需求、性能要求和開發效率。以下是常見的幾種方案&#xff0c;按易用性排序&#xff1a; 1. PyBind11&#xff08;推薦首選&#xff09; 特點&#xff1a;現代C庫&#xff0c;語法簡潔&#xff0…

【位運算】消失的兩個數字

文章目錄 面試題 17.19. 消失的兩個數字解題思路 面試題 17.19. 消失的兩個數字 面試題 17.19. 消失的兩個數字 ? 給定一個數組&#xff0c;包含從 1 到 N 所有的整數&#xff0c;但其中缺了兩個數字。你能在 O(N) 時間內只用 O(1) 的空間找到它們嗎&#xff1f; ? 以任意…

自然語言處理Hugging Face Transformers

Hugging Face Transformers 是一個基于 PyTorch 和 TensorFlow 的開源庫&#xff0c;專注于 最先進的自然語言處理&#xff08;NLP&#xff09;模型&#xff0c;如 BERT、GPT、RoBERTa、T5 等。它提供了 預訓練模型、微調工具和推理 API&#xff0c;廣泛應用于文本分類、機器翻…

vue開發基礎流程 (后20)

創建項目命令&#xff1b; 或者 vue create my - vue - router - project這個是創建帶路由的項目 22.組件組成 比如說一個頁面吧&#xff0c;他三個組件&#xff0c;template就是用來放所有的標簽&#xff0c;script用來放業務邏輯&#xff0c;style用來放樣式&#xff0c;c…

高性能內存kv數據庫Redis

引言 在當今數據驅動的時代&#xff0c;高效的數據存儲和檢索對于各類應用程序至關重要。Redis&#xff08;Remote Dictionary Server&#xff09;作為一款開源的內存鍵值數據庫&#xff0c;憑借其出色的性能、豐富的數據結構和靈活的特性&#xff0c;在眾多場景中得到了廣泛應…

自動化測試概念篇

文章目錄 目錄1. 自動化1.1 自動化概念1.1.1 回歸測試 1.2 自動化分類1.3 自動化測試金字塔 2. web自動化測試2.1 驅動2.1.1 安裝驅動管理2.1.2 selenium庫 3. Selenium3.1 一個簡單的web自動化示例3.2 selenium驅動瀏覽器的工作原理 目錄 自動化web自動化測試Selenium 1. 自…

《AI大模型應知應會100篇》第17篇:大模型的偏見與公平性問題

第17篇&#xff1a;大模型的偏見與公平性問題 摘要 在人工智能迅速發展的今天&#xff0c;大型語言模型&#xff08;LLM&#xff09;已經深入到我們的日常生活和工作中。然而&#xff0c;這些模型并非完美無缺&#xff0c;它們可能攜帶并放大數據中的偏見&#xff0c;導致不公…

【踩坑】GitHub Actions 運行的 Linux 環境中,文件名是大小寫敏感的

在使用 VuePress 搭建個人博客并部署到 GitHub Pages 的過程中&#xff0c;我遇到了一個頗為棘手的問題&#xff1a;本地打包一切正常&#xff0c;但在 GitHub Actions 自動執行打包流程時&#xff0c;卻提示找不到 README.md 文件&#xff0c;導致整個流程失敗。經過一番深入排…

C# 13新特性 - .NET 9

轉載&#xff1a; C# 13 中的新增功能 | Microsoft Learn C# 13 包括以下新增功能。 可以使用最新的 Visual Studio 2022 版本或 .NET 9 SDK 嘗試這些功能&#xff1a;Introduced in Visual Studio 2022 Version 17.12 and newer when using C# 13 C# 13 中的新增功能 | Micr…

numpy.ma.masked_where:屏蔽滿足條件的數組

1.函數功能 屏蔽滿足條件的數組內容&#xff0c;返回值為掩碼數組 2.語法結構 np.ma.masked_where(condition, a, copyTrue)3. 參數 參數含義condition屏蔽條件a要操作的數組copy布爾值&#xff0c;取值為True時&#xff0c;結果復制數組(原始數據不變)&#xff0c;否則返回…

【Redis】數據結構和內部編碼

先來復習一下之前學過的幾個基本的全局命令&#xff1a; keys&#xff1a;用來查看匹配規則的keyexists&#xff1a;用來判定執行key是否存在del&#xff1a;刪除指定的keyexpire&#xff1a;給key設置過期時間ttl&#xff1a;查詢key的過期時間type&#xff1a;查詢key對應的…

OBOO鷗柏如何以智能教育室內外觸摸屏一體機AI變革硬件

在AI技術蓬勃發展的當下&#xff0c;OBOO鷗柏室外觸摸屏一體機通過融入AI科技&#xff0c;為教育領域帶來了翻天覆地的變化。這款一體機不僅為高校和大學校園提供了革命性的數字化教學解決方案&#xff0c;更引領了引體向上成績提升一體機帶訓室外終端屏幕設備的新潮流。其創新…

從零搭建高并發體育直播網站:架構設計、核心技術與性能優化實戰

本文從技術視角拆解體育直播網站開發全流程&#xff0c;涵蓋高并發架構設計、低延遲視頻流傳輸、實時彈幕系統實現等核心模塊&#xff0c;并附可復用的代碼片段與優化方案。適合中高級開發者進階實戰參考。 一、需求分析與技術選型 1. 典型業務場景 核心需求&#xff1a;支持1…

【Python內置函數的深度解析與應用】id

目錄 前言&#xff1a;技術背景與價值當前技術痛點解決方案概述目標讀者說明 一、技術原理剖析核心概念圖解關鍵技術模塊技術選型對比 二、實戰演示環境配置要求核心代碼實現1. 基礎身份驗證2. 不可變對象優化3. 對象生命周期追蹤 運行結果驗證 三、性能對比測試方法論量化數據…

3.vtkProp 和vtkProp3D

文章目錄 vtkProp 和vtkProp3D使用vtkProp3D使用vtkPro vtkProp 和vtkProp3D vtkProp 和 vtkProp3D 都是VTK&#xff08;Visualization Toolkit&#xff09;庫中的類&#xff0c;它們用于在渲染場景中表示可視化元素。理解這兩個類的區別和用途對于有效地使用VTK進行三維數據可…