1. 程式人生 > >STL之基礎演算法(二)

STL之基礎演算法(二)

/ generate and generate_n
//將仿函式gen的處理結果填充在[first, last)區間內所有元素上,所謂填寫
//就是用迭代器所指元素的assignment操作
//注意:對於使用者自定義型別要提供operator =() 

template <class _ForwardIter, class _Generator>
void generate(_ForwardIter __first, _ForwardIter __last, _Generator __gen) {
  __STL_REQUIRES(_ForwardIter, _ForwardIterator);
  __STL_GENERATOR_CHECK(_Generator, 
          typename iterator_traits<_ForwardIter>::value_type);
  for ( ; __first != __last; ++__first)//遍歷整個序列
    *__first = __gen();
}
//將仿函式gen的處理結果填充在first開始的n個元素上,所謂填寫
//就是用迭代器所指元素的assignment操作
template <class _OutputIter, class _Size, class _Generator>
_OutputIter generate_n(_OutputIter __first, _Size __n, _Generator __gen) {
  __STL_REQUIRES(_OutputIter, _OutputIterator);
  for ( ; __n > 0; --__n, ++__first)//只限於n個元素
    *__first = __gen();
  return __first;
}
/*
下面是計算set集合的相關演算法,分別是並集set_union,差集set_difference,交集set_intersection
和對稱差集set_symmetric_difference,這是個函式都提供了兩個版本的函式原型
第一個版本是採用預設的排序比較方式 operator<
第二個版本是使用者通過comp自行指定排序方式
注意:這四個演算法接受的輸入區間都是有序的,輸出也是有序的
*/

// Set algorithms: includes, set_union, set_intersection, set_difference,
// set_symmetric_difference.  All of these algorithms have the precondition
// that their input ranges are sorted and the postcondition that their output
// ranges are sorted.

// 判斷[first1, last1)是否包含[first2, last2),  
// 注意: 兩個區間要保證有序,預設排序方式是operator<,若要自行定義排序方式,則呼叫第二版本;
template <class _InputIter1, class _InputIter2>
bool includes(_InputIter1 __first1, _InputIter1 __last1,
              _InputIter2 __first2, _InputIter2 __last2) {
  __STL_REQUIRES(_InputIter1, _InputIterator);
  __STL_REQUIRES(_InputIter2, _InputIterator);
  __STL_REQUIRES_SAME_TYPE(
       typename iterator_traits<_InputIter1>::value_type,
       typename iterator_traits<_InputIter2>::value_type);
  __STL_REQUIRES(typename iterator_traits<_InputIter1>::value_type,
                 _LessThanComparable);
  while (__first1 != __last1 && __first2 != __last2)//遍歷兩個區間
    if (*__first2 < *__first1)//first2小於first1表示不包含
      return false;//返回FALSE
    else if(*__first1 < *__first2)//若first1小於first2 
      ++__first1;//尋找第一個區間下一個位置
    else
      ++__first1, ++__first2;//若first2等於first1,遍歷兩區間的下一位置

  return __first2 == __last2;//若第二個區間先到達尾端,則返回TRUE
}

//版本二:使用者通過comp自行指定排序方式
template <class _InputIter1, class _InputIter2, class _Compare>
bool includes(_InputIter1 __first1, _InputIter1 __last1,
              _InputIter2 __first2, _InputIter2 __last2, _Compare __comp) {
  __STL_REQUIRES(_InputIter1, _InputIterator);
  __STL_REQUIRES(_InputIter2, _InputIterator);
  __STL_REQUIRES_SAME_TYPE(
       typename iterator_traits<_InputIter1>::value_type,
       typename iterator_traits<_InputIter2>::value_type);
  __STL_BINARY_FUNCTION_CHECK(_Compare, bool,
       typename iterator_traits<_InputIter1>::value_type,
       typename iterator_traits<_InputIter2>::value_type);
  while (__first1 != __last1 && __first2 != __last2)
    if (__comp(*__first2, *__first1))
      return false;
    else if(__comp(*__first1, *__first2)) 
      ++__first1;
    else
      ++__first1, ++__first2;

  return __first2 == __last2;
}
// partition, stable_partition, and their auxiliary functions
//若迭代器的型別為forward_iterator_tag,則呼叫此函式
template <class _ForwardIter, class _Predicate>
_ForwardIter __partition(_ForwardIter __first,
		         _ForwardIter __last,
			 _Predicate   __pred,
			 forward_iterator_tag) {
  if (__first == __last) return __first;//若為空,直接退出

  while (__pred(*__first))//若pred出first的值為true
    if (++__first == __last) return __first;//先移動迭代器first,在判斷是否到達尾端last

  _ForwardIter __next = __first;//繼續判斷

  while (++__next != __last)//若下一個位置依然不是尾端
    if (__pred(*__next)) {//繼續pred出next的值,若為true
      swap(*__first, *__next);//交換值
      ++__first;//繼續下一位置
    }

  return __first;
}
//若迭代器的型別為bidirectional_iterator_tag,則呼叫此函式
template <class _BidirectionalIter, class _Predicate>
_BidirectionalIter __partition(_BidirectionalIter __first,
                               _BidirectionalIter __last,
			       _Predicate __pred,
			       bidirectional_iterator_tag) {
  while (true) {
    while (true)
      if (__first == __last)//若為空
        return __first;//直接退出
      else if (__pred(*__first))//first的值符合不移動條件,則不移動該值
        ++__first;//只移動迭代器
      else//若頭指標符合移動
        break;//跳出迴圈
    --__last;//尾指標回溯
    while (true)
      if (__first == __last)//頭指標等於尾指標
        return __first;//操作結束
      else if (!__pred(*__last))//尾指標的元素符合不移動操作
        --__last;//至移動迭代器,並不移動具體元素
      else//尾指標的元素符合移動操作
        break;//跳出迴圈
    iter_swap(__first, __last);//頭尾指標交換元素
    ++__first;//準備下一次迴圈
  }
}
//將區間[first,last)的元素進行排序,被pred判斷為true的放在區間的前段,判定為false的放在區間後段
//該算算可能會使元素的元素位置放生改變.
/*
演算法功能:Rearranges the elements from the range [first,last), in such a way that all the elements
for which pred returns true precede all those for which it returns false. 
The iterator returned points to the first element of the second group.

演算法原型:
	template <class BidirectionalIterator, class UnaryPredicate>
	BidirectionalIterator partition (BidirectionalIterator first,
                                   BidirectionalIterator last, UnaryPredicate pred);
*/
template <class _ForwardIter, class _Predicate>
inline _ForwardIter partition(_ForwardIter __first,
   			      _ForwardIter __last,
			      _Predicate   __pred) {
  __STL_REQUIRES(_ForwardIter, _Mutable_ForwardIterator);
  __STL_UNARY_FUNCTION_CHECK(_Predicate, bool, 
        typename iterator_traits<_ForwardIter>::value_type);
  //首先萃取出迭代器first的型別,根據迭代器的型別呼叫不同的函式
  return __partition(__first, __last, __pred, __ITERATOR_CATEGORY(__first));
}



相關推薦

STL基礎演算法()

/ generate and generate_n //將仿函式gen的處理結果填充在[first, last)區間內所有元素上,所謂填寫 //就是用迭代器所指元素的assignment操作 //注意:對於使用者自定義型別要提供operator =() template

Qt入門基礎篇 ( ) :Qt項目建立、編譯、運行和發布過程解析

qt 5 對話 讓我 進度 qmake ctr deploy 設定 設置 轉載請註明出處:CN_Simo。 題解:   本篇內容主講Qt應用從創建到發布的整個過程,旨在幫助讀者能夠快速走進Qt的世界。   本來計劃是講解Qt源碼靜態編譯,如此的話讀者可能並不能清楚地知

matlab 數學建摸基礎

matlab函式知識點 linspace函式-等差的行向量 transppse函式-行向量與列向量轉換 ones(2,4)-產生(2,4)的元數為1的矩陣 Matlab中隨機數生成器主要有: betarnd 貝塔分佈的隨機數生成器 binornd 二項分佈的隨機數生

STL排序演算法

1.merge() 以下是排序和通用演算法:提供元素排序策略 merge: 合併兩個有序序列,存放到另一個序列。 例如: vecIntA,vecIntB,vecIntC是用vector宣告的容器,vecIntA已包含1,3,5,7,9元素,vecIntB已包含2,4,6,8元素

玩轉termux基礎配置

1. qqbot實現qq機器人 qqbot 是一個用 python 實現的、基於騰訊 SmartQQ 協議的 QQ 機器人框架,可執行在 Linux 、 Windows 和 Mac OSX 平臺下。 你可以通過擴充套件 qqbot 來實現: (1)監控、收集 QQ 訊息 (2)自動訊

【FastDev4Android框架開發】Volley完全解析基礎使用(十六)

轉載請標明出處:(一).前言:             【好訊息】個人網站已經上線執行,後面部落格以及技術乾貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org對於網路框架這

Python3學習筆記基礎教程

fibo.py __author__ = 'Administrator' def fib(n): a,b=0,1 while b<n: print(b,end=' ') a,b=b,a+b print() de

noip資料結構與演算法 基礎演算法 4 維差值維護

noip資料結構與演算法 之 基礎小演算法 4 二維差值維護 二維差值維護問題實際上是對一維差值維護問題的擴充套件,相信來看二維差值維護的各位都已經對一維差值維護問題有足夠的認識了。下面先看一下二維差值維護的問題。  問題描述: 已知一個n*n的矩陣a,有m次操作,每次

程式設計菜鳥到大佬路:演算法基礎

第二天學習精要 列舉 生理週期 例題2:生理週期 題目描述 人有體力、情商、智商的高峰日子,它們分別每隔23天、 28天和33天出現一次。 對於每個人,我們想知道何時三個高峰落在同一天。 給

STLset具體解釋(

基本操作 二叉樹 mono itl 自己 pair leading 左右子樹 ews 首先來看看set集合容器: set集合容器實現了紅黑樹的平衡二叉樹數據結構。在插入元素時它會自己主動調整二叉樹的排列,把該元素放到適當的位置,而且 保證左右子樹平衡。平衡二

算法基礎知識樹、叉樹,

pan rect nbsp 結構 src class wiki 子節點 資料 一、樹 把它叫做“樹”是因為它看起來像一棵倒掛的樹,也就是說它是根朝上,而葉朝下的。在計算機科學中,樹(英語:tree)是一種抽象數據類型(ADT)或是實作這種

算法基礎知識樹、叉樹

image inline cstyle width var mar span mes med 一、樹 把它叫做“樹”是因為它看起來像一棵倒掛的樹,也就是說它是根朝上,而葉朝下的。在計算機科學中,樹(英語:tree)是一種抽象數據類型(AD

基礎集合()總結

線程不安全 emp abstract 和集 write next 不可變 叠代器 關系 1. Map集合和collection結合的區別 1》Collection一次存一個元素;Map一次存一對元素; 2》Collection是單列集合;Map是雙列集合; 3》Map中的存

python基礎學習 數據結構list及相關基本操作

意思 指定位置 blog div 基礎 我們 clas 位置 列表 list是py內置的一種數據類型,list就是列表的意思,list就是一種有序的數據集合,可以隨時增加和刪除list的元素。 生活中,比如我們要列出全班同學的名字,就可以用list來表示 >>

Python基礎數據類型和運算(1)——數字

spa 發生 double類型 圖片 body poi 存在 discard 交互模式 數據類型初識 數字 2 是一個整數的例子。 長整數 不過是大一些的整數。 3.23和52.3E-4是浮點數的例子。E標記表示10的冪。在這裏,52.3E-4表示52.3 * 1

Python基礎數據類型和運算(2)——字符串

創建 options 生成 quotes 字符串 表達 ngs 字符串格式化 lib 字符串基礎 Python 也提供了可以通過幾種不同方式表示的字符串。它們可以用單引號 (‘...‘) 或雙引號 ("...") 標識 。\ 可以用來轉義引號: >>>

Python 基礎知識(

break post elif true 滿足 賦值 隨機數 計數 spa 一、分支運算   在Python 2.x中判斷不等於還可以用<> if語句進階:elif if 條件1:   ...... elif 條件2:   ...... else:

Python旅 (基礎 1-25)

body 函數 closed 技術分享 items pop img -s 之間 字典:   字典是python中唯一的映射類型,采用鍵值對(key-value)的形式存儲數據。python對key進行哈希函數運算,根據計算的結果決定value的存儲地址,所以字典是無序存儲

java學習筆記基礎語法(

讓其 實例 高效率 使用 個數 存儲 記錄 棧內存 數組 1.數組: 概念:同一種類型數據的集合,其實,數組就是一個容器 優點:可以方便的對其進行操作,編號從0開始,方便操作這些元素。 2,數組的格式 元素類型[]數組名=new 元素類型[數組元素個

Go基礎--操作Mysql()

有時 ans ror score 事件 tab 自動 還需要 以及 在上一篇文章中主要整理了Golang連接mysql以及一些基本的操作,並進行了大概介紹,這篇文章對增刪查改進行詳細的整理 讀取數據 在上一篇文章中整理查詢數據的時候,使用了Query的方法查詢,其實d