最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 標(biāo)準(zhǔn)庫(kù)中的 <algorithm> 頭文件算法操作總結(jié)

 更新時(shí)間:2025年04月15日 09:40:23   作者:胡亂兒起個(gè)名  
C++ 標(biāo)準(zhǔn)庫(kù)中的 <algorithm> 頭文件提供了大量有用的算法,主要用于操作容器(如 vector, list, array 等),這些算法通常通過迭代器來操作容器元素,本文給大家介紹C++ 標(biāo)準(zhǔn)庫(kù)中的 <algorithm> 頭文件算法總結(jié),感興趣的朋友一起看看吧

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

  C++ 標(biāo)準(zhǔn)庫(kù)中的 <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};
// 對(duì)每個(gè)元素執(zhí)行操作
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};
// 計(jì)算等于3的元素個(gè)數(shù)
int count_3 = std::count(v.begin(), v.end(), 3);
// 計(jì)算滿足條件的元素個(gè)數(shù)
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);
// 查找第一個(gè)偶數(shù)
auto it_even = std::find_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });
// 查找第一個(gè)非偶數(shù)
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);
// 復(fù)制元素
std::copy(src.begin(), src.end(), dst.begin());
// 條件復(fù)制
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個(gè)元素為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());
// 對(duì)每個(gè)元素應(yīng)用函數(shù)
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);
// 替換所有偶數(shù)為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};
// 替換所有3為10
std::replace(v.begin(), v.end(), 3, 10);
// 替換所有偶數(shù)為0
std::replace_if(v.begin(), v.end(), 
                [](int i){ return i % 2 == 0; }, 0);

3. 排序和相關(guān)操作

std::sort, std::stable_sort

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

std::partial_sort

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

std::nth_element

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

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());
// 查找第一個(gè)破壞排序的元素
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};
// 查找第一個(gè)不小于3的元素
auto low = std::lower_bound(v.begin(), v.end(), 3);
// 查找第一個(gè)大于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());
// 合并兩個(gè)已排序的序列
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));
// 對(duì)稱差集(在v1或v2中但不同時(shí)在兩者中)
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};
// 構(gòu)建最大堆
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());
// 同時(shí)查找最小和最大元素
auto minmax = std::minmax_element(v.begin(), v.end());

std::clamp (C++17)

int value = 15;
// 將值限制在10-20范圍內(nèi)
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};
// 生成下一個(gè)排列
do {
    // 處理當(dāng)前排列
} while (std::next_permutation(v.begin(), v.end()));
// 生成前一個(gè)排列
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};
// 點(diǎn)積
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++編程效率,避免了手動(dòng)編寫循環(huán)的繁瑣工作,同時(shí)通常比手寫循環(huán)更高效。

到此這篇關(guān)于C++ 標(biāo)準(zhǔn)庫(kù)中的 <algorithm> 頭文件算法總結(jié)的文章就介紹到這了,更多相關(guān)C++ <algorithm> 頭文件算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)堆排序圖文示例

    C語言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)堆排序圖文示例

    這篇文章主要為大家介紹了C語言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)堆排序的圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • C語言超詳細(xì)講解猜數(shù)字游戲的實(shí)現(xiàn)

    C語言超詳細(xì)講解猜數(shù)字游戲的實(shí)現(xiàn)

    現(xiàn)在很多游戲都有抽獎(jiǎng)抽卡的功能,其實(shí)這個(gè)就類似于猜數(shù)字,生成一個(gè)隨機(jī)數(shù),然后你去猜,猜對(duì)了就得獎(jiǎng)。猜到一定次數(shù)就會(huì)保底。要實(shí)現(xiàn)猜數(shù)字的小游戲,首先是要讓程序生成隨機(jī)數(shù),這就要用到rand、srand和time這三個(gè)函數(shù),其次要了解時(shí)間戳
    2022-07-07
  • C++中雙冒號(hào)::的作用淺析

    C++中雙冒號(hào)::的作用淺析

    在C++中經(jīng)常使用雙冒號(hào)::,很多朋友不知道是什么意思,這篇文章主要介紹了C++中雙冒號(hào)::的作用,需要的朋友可以參考下
    2018-06-06
  • 關(guān)于C++中0是十進(jìn)制還是八進(jìn)制的問題

    關(guān)于C++中0是十進(jìn)制還是八進(jìn)制的問題

    本篇文章中,小編將為大家介紹關(guān)于C++中0是十進(jìn)制還是八進(jìn)制的問題,有需要的朋友可以參考一下
    2013-04-04
  • 基于Qt實(shí)現(xiàn)離線瓦片地圖下載器

    基于Qt實(shí)現(xiàn)離線瓦片地圖下載器

    這篇文章主要介紹了如何通過Qt實(shí)現(xiàn)離線瓦片地圖下載器,文中的示例代碼對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-01-01
  • C++ stack與queue使用方法詳細(xì)講解

    C++ stack與queue使用方法詳細(xì)講解

    stack是一種容器適配器,專門用在具有后進(jìn)先出操作的上下文環(huán)境中,其刪除只能從容器的一端進(jìn)行 元素的插入與提取操作;隊(duì)列是一種容器適配器,專門用于在FIFO上下文(先進(jìn)先出)中操作,其中從容器一端插入元素,另一端提取元素
    2023-01-01
  • c++實(shí)現(xiàn)哈希桶的步驟

    c++實(shí)現(xiàn)哈希桶的步驟

    本文主要介紹了c++實(shí)現(xiàn)哈希桶的步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • C++?protobuf中對(duì)不同消息內(nèi)容進(jìn)行賦值的方式總結(jié)(set_、set_allocated_、mutable_、add_)

    C++?protobuf中對(duì)不同消息內(nèi)容進(jìn)行賦值的方式總結(jié)(set_、set_allocated_、mutable_、

    這篇文章主要給大家介紹了關(guān)于C++?protobuf中對(duì)不同消息內(nèi)容進(jìn)行賦值的方式總結(jié),主要使用的是set_、set_allocated_、mutable_、add_,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • C++類和對(duì)象到底是什么

    C++類和對(duì)象到底是什么

    C++ 是一門面向?qū)ο蟮木幊陶Z言,理解 C++,首先要理解類(Class)和對(duì)象(Object)這兩個(gè)概念。下面和小編一起來學(xué)習(xí)吧
    2021-09-09
  • C++采用ring3讀取MBR實(shí)例

    C++采用ring3讀取MBR實(shí)例

    這篇文章主要介紹了C++采用ring3讀取MBR實(shí)例,可實(shí)現(xiàn)對(duì)硬盤的主引導(dǎo)記錄的讀取,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10

最新評(píng)論

长春市| 鲁山县| 湛江市| 凤山县| 怀仁县| 道孚县| 陇西县| 甘谷县| 梧州市| 黎城县| 信宜市| 岫岩| 石泉县| 建德市| 石屏县| 通化市| 北流市| 德庆县| 安宁市| 桐庐县| 蓬莱市| 昭平县| 和田县| 台北县| 缙云县| 黄浦区| 达孜县| 辽宁省| 高尔夫| 读书| 普陀区| 南阳市| 晋中市| 凤翔县| 吉首市| 宁德市| 黄石市| 衡水市| 安多县| 旌德县| 文山县|