亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c++11新增的便利算法實例分析

 更新時間:2014年09月08日 10:21:51   投稿:shichen2014  
這篇文章主要介紹了c++11新增的便利算法,主要有用于判斷、查找、數(shù)組、序列等的操作算法,非常具有實用價值,需要的朋友可以參考下

C++是一門應用非常廣泛的程序設計語言,而c++11則新增加了一些便利的算法,這些新增的算法使我們的代碼寫起來更簡潔方便,本文列舉一些常用的新增算法,算是做個總結分析,更多的新增算法讀者可以參考:http://en.cppreference.com/w/cpp/algorithm。

算法庫新增了三個用于判斷的算法all_of、any_of和none_of,定義如下:

template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );

template< class InputIt, class UnaryPredicate >
bool any_of( InputIt first, InputIt last, UnaryPredicate p );

template< class InputIt, class UnaryPredicate >
bool none_of( InputIt first, InputIt last, UnaryPredicate p );

① all_of:檢查區(qū)間[first, last)中是否所有的元素都滿足一元判斷式p,所有的元素都滿足條件返回true,否則返回false。
② any_of:檢查區(qū)間[first, last)中是否至少有一個元素都滿足一元判斷式p,只要有一個元素滿足條件就返回true,否則返回true。
③ none_of:檢查區(qū)間[first, last)中是否所有的元素都不滿足一元判斷式p,所有的元素都不滿足條件返回true,否則返回false。

下面是這幾個算法的示例:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    vector<int> v = { 1, 3, 5, 7, 9 };
   auto isEven = [](int i){return i % 2 != 0;
    bool isallOdd = std::all_of(v.begin(), v.end(), isEven);
    if (isallOdd)
       cout << "all is odd" << endl;

    bool isNoneEven = std::none_of(v.begin(), v.end(), isEven);
    if (isNoneEven)
       cout << "none is even" << endl;

    vector<int> v1 = { 1, 3, 5, 7, 8, 9 };
    bool anyof = std::any_of(v1.begin(), v1.end(), isEven);
    if (anyof)
       cout << "at least one is even" << endl;
}

輸出:

all is odd
none is odd
at least one is even

算法庫的查找算法新增了一個find_if_not,它的含義和find_if是相反的,即查找不符合某個條件的元素,find_if也可以實現(xiàn)find_if_not的功能,只需要將判斷式改為否定的判斷式即可,現(xiàn)在新增了find_if_not之后,就不需要再寫否定的判斷式了,可讀性也變得更好。下面是它的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector<int> v = { 1, 3, 5, 7, 9,4 };
  auto isEven = [](int i){return i % 2 == 0;};
  auto firstEven = std::find_if(v.begin(), v.end(), isEven);
  if (firstEven!=v.end())
       cout << "the first even is " <<* firstEven << endl;

    //用find_if來查找奇數(shù)則需要重新寫一個否定含義的判斷式
  auto isNotEven = [](int i){return i % 2 != 0;};
  auto firstOdd = std::find_if(v.begin(), v.end(),isNotEven);

    if (firstOdd!=v.end())
       cout << "the first odd is " <<* firstOdd << endl;

    //用find_if_not來查找奇數(shù)則無需新定義判斷式

    auto odd = std::find_if_not(v.begin(), v.end(), isEven);
    if (odd!=v.end())
       cout << "the first odd is " <<* odd << endl;
}

將輸出:

the first even is 4
the first odd is 1
the first odd is 1

可以看到使用find_if_not不需要再定義新的否定含義的判斷式了,更簡便了。

算法庫還增加了一個copy_if算法,它相比原來的copy算法多了一個判斷式,用起來更方便了,下面是它的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v = { 1, 3, 5, 7, 9, 4 };
    std::vector<int> v1(v.size());
    //根據(jù)條件拷貝
    auto it = std::copy_if(v.begin(), v.end(), v1.begin(), [](int i){return i%2!=0;});
    //縮減vector到合適大小
    v1.resize(std::distance(v1.begin(),it));
    for(int i : v1)
    {
       cout<<i<<" ";
    }

    cout<<endl;
}  

算法庫新增了iota用來方便的生成有序序列,比如我們需要一個定長數(shù)組,這個數(shù)組中的元素都是在某一個數(shù)值的基礎之上遞增的,那么用iota可以很方便的生成這個數(shù)組了。下面是它的基本用法:

#include <numeric>
#include <array>
#include <vector>
#include <iostream>
using namespace std;
 
int main()
{
vector<int> v(4) ;
//循環(huán)遍歷賦值來初始化數(shù)組
//for(int i=1; i<=4; i++)
//{
//  v.push_back(i);
//}

//直接通過iota初始化數(shù)組,更簡潔
  std::iota(v.begin(), v.end(), 1);
  for(auto n: v) {
    cout << n << ' ';
  }
  cout << endl;
  
  std::array<int, 4> array;
  std::iota(array.begin(), array.end(), 1);
  for(auto n: array) {
    cout << n << ' ';
  }
  std::cout << endl;
}

將輸出:

1 2 3 4
1 2 3 4

可以看到使用iota比遍歷賦值來初始化數(shù)組更簡潔,需要注意的是iota初始化的序列需要指定大小,如果上面的代碼中:vector<int> v(4) ;沒有指定初始化大小為4的話,則輸出為空。

算法庫還新增了一個同時獲取最大值和最小值的算法minmax_element,這樣我們如果想獲取最大值和最小值的時候就不用分別調用max_element和max_element算法了,用起來會更方便,minmax_element會將最小值和最大值的迭代器放到一個pair中返回,下面是它的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
  // your code goes here
  vector<int> v = { 1, 2, 5, 7, 9, 4 };
  auto result = minmax_element(v.begin(), v.end());
  
  cout<<*result.first<<" "<<*result.second<<endl;

  return 0;
}

將輸出:

1 9

算法庫新增了is_ sorted和is_ sorted_until算法,is_sort用來判斷某個序列是否是排好序的,is_sort_until則用來返回序列中前面已經排好序的部分序列。下面是它們的基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
  vector<int> v = { 1, 2, 5, 7, 9, 4 };
  auto pos = is_sorted_until(v.begin(), v.end());
  
  for(auto it=v.begin(); it!=pos; ++it)
  {
    cout<<*it<< " ";
  }
  cout<<endl;
  
  bool is_sort = is_sorted(v.begin(), v.end());
  cout<< is_sort<<endl;
  return 0;
}

將輸出:

1 2 5 7 9
0

總結:這些新增的算法讓我們用起來更加簡便,也增強了代碼的可讀性。

希望本文所述算法對大家更好的掌握C++11能有所幫助。

相關文章

  • c++??復制消除問題解決示例詳析

    c++??復制消除問題解決示例詳析

    這篇文章主要為大家介紹了c++??復制消除問題解決示例詳析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • C++實現(xiàn)LeetCode(83.移除有序鏈表中的重復項)

    C++實現(xiàn)LeetCode(83.移除有序鏈表中的重復項)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(83.移除有序鏈表中的重復項),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-07-07
  • C++面向行輸入之get()與getline()實例詳解

    C++面向行輸入之get()與getline()實例詳解

    在c++里當我們輸入一個字符串時習慣用cin,但是cin只能讀取一段不含空格的字符串,如果我們需要讀取一段包含空格的字符串時,就需要用到getline()或get(),下面這篇文章主要給大家介紹了關于C++面向行輸入之get()與getline()的相關資料,需要的朋友可以參考下
    2021-10-10
  • 初識C++?Vector模板與實例化原理

    初識C++?Vector模板與實例化原理

    這篇文章主要為大家介紹了初識C++?Vector模板與實例化原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • C/C++ Qt TreeWidget 嵌套節(jié)點操作使用

    C/C++ Qt TreeWidget 嵌套節(jié)點操作使用

    本文主要介紹了TreeWidget的如何使用,實現(xiàn)對樹形框多節(jié)點的各種操作,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C語言中的結構體內嵌函數(shù)用法

    C語言中的結構體內嵌函數(shù)用法

    這篇文章主要介紹了C語言中的結構體內嵌函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C++異常重拋出實例分析

    C++異常重拋出實例分析

    在本文里小編給大家分享的是關于C++異常重拋出實例分析,有興趣點朋友們可以跟著學習下。
    2020-05-05
  • 詳解如何在C/C++中測量一個函數(shù)或功能的運行時間

    詳解如何在C/C++中測量一個函數(shù)或功能的運行時間

    本文算是一個比較完整的關于在 C/C++ 中測量一個函數(shù)或者功能的總結,最后會演示三種方法的對比,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • C語言驅動開發(fā)內核特征碼掃描PE代碼段

    C語言驅動開發(fā)內核特征碼掃描PE代碼段

    這篇文章主要為大家介紹了C語言驅動開發(fā)內核特征碼掃描PE代碼段,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • 深入理解C++的多態(tài)性

    深入理解C++的多態(tài)性

    本篇文章是對C++的多態(tài)性進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論