STL 第四周學習筆記

Algorithm

clipboard.png

迭代器分類

clipboard.png

迭代器類型對算法的影響

clipboard.png

clipboard.png

clipboard.png

仿函數

  • STL各部分關係

clipboard.png

容器經過內存分配器分配空間;容器和算法由於迭代器而分離,算法經過迭代器訪問容器;仿函數協助算法指定不一樣的策略;適配器套接仿函數。仿函數將某種"行爲"做爲算法的參數傳給算法。算法

仿函數適配器(Function Adapter)

  • binder1st

給定一個vector,其元素爲:[0,0,0,1,0],想要匹配到第一個非零元素。咱們有find_if()的算法和not_equal_to的函數對象,根據這兩個東西就能夠實現咱們的目的。然而,not_equal_to()(const T& a,const T&b),並不能接受一個參數;find_if()又是向最後一個函數對象直接給予一個引用做爲參數,因此沒法編譯經過。爲此使用binder1st來把其中的一個參數固定下來。函數

vector<int>::iterator it = find_if(v.begin(),v.end(),bind1st(not_equal_to<int>(),0));
  • binder2nd

clipboard.png

  • 爲了這種機制的考慮,在全部的仿函數中都要有first_argument_type等這幾個typedef。一般經過派生自binary_function<class Arg1,class Arg2,class Result>能夠直接獲得。
  • 例如 binder1st<not_equal_to<int>>(f,0)就表明生成一個函數對象func(x)=f(0,x);
  • 各類函數適配器都是以實例化後的仿函數的類型做爲模板參數的類模板,這是函數適配器的重要標誌。

迭代器適配器

  • reverse_iterator

clipboard.png

一些注意點

  1. 注意若是使用了諸如push(new Point(x,y))這種語法,必需要在刪除容器以前遍歷容器進行內存釋放,不然就發生了內存泄露。
  2. 儘可能用算法代替手寫的循環,一方面是從效率的角度考慮,另外一方面使用算法的這種方式更好地體現了面向對象的思想。
  3. 容器的size和capacity

Capacity是容器"佔據"的空間,size是容器"使用"的空間。因此容器有可能佔據了很大的空間然而並無使用那一部分空間,那麼這種狀況下應該經過swap方法對容器進行縮小。spa

  1. 爲何建議在容器中放指針而不是對象?

1.拷貝的開銷比較大。
2.若是存在繼承,就會發生slicing。即因爲保存的僅僅是基類對象,其留下的大小也就是基類對象的大小。若是存在繼承關係時,若將派生類對象放入容器,則只會保留基類部分,派生類部分會丟失。這種方式體現出了C++中的多態性。指針

一些泛型算法

  • for_each
template<class _InIt,class _Fnl> inline

_Fnl for_each(_InIt _First, _InIt _Last, _Fnl _Func)

for_each就是把兩個迭代器之間的對象實例扔給_Func做爲惟一參數。注意,_Func不必定非得是函數對象(仿函數),直接傳函數名或函數指針也是能夠的。code

  • find
template<class _InIt,class _Ty> inline

_Ty find(_InIt _First, _InIt _Last,const _Ty& _Val)

若是在左閉右開區間內找到了值爲_Val的對象則返回該迭代器,不然返回器尾部迭代器。對象

  • find_if
template<class _InIt,class _Pr> inline

_Ty find(_InIt _First, _InIt _Last,const _Pr _Pred)

_Pr爲一個函數名或者函數對象(仿函數)。若是在左閉右開區間內找到了的能使得這一對象返回真的,則返回該迭代器,不然返回器尾部迭代器。注意_Pr爲單參,注意聯合適配器的使用。繼承

  • adjacent_findip

    template <class ForwardIterator>
    
    ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);

返回第一個遇到的兩個連續相等的對象中,第一個對象的迭代器,如對於[0,1,2,3,3,4],則返回指向第一個3的迭代器。內存

template<class _InIt,class _Pr> inline

_Ty find(_InIt _First, _InIt _Last,const _Pr _Pred)

Adjacent的操做其實是針對本身和後繼進行的比較操做。除了默認的等於比較操做以外,也可使用自定義的函數或仿函數。_Pred 具備兩個參數,使用時會把連續的兩個值分別做爲第一參數和第二參數求真值。ci

  • find_first_of
template <class ForwardIterator1, class ForwardIterator2>

ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2);

 

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>

ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2,

BinaryPredicate pred);

它是在兩組區間內搜索是否有it1==it2,若是有就返回第一組區間內對象的迭代器。也可使用自定義的雙參函數進行比較。

  • count/count_if
template <class InputIterator, class T>

typename iterator_traits<InputIterator>::difference_type

count (InputIterator first, InputIterator last, const T& val);

返回左閉右開區間中值等於val的元素的個數。

類比find,count_if就是本身指定所想使用的函數。一樣地,其爲單參,要注意兩對適配器的使用。

  • mismatch
template <class InputIterator1, class InputIterator2, class BinaryPredicate>

pair<InputIterator1, InputIterator2>

mismatch (InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate pred);

當不指定最後一個函數時,其做用是逐元素比對兩個序列,返回指向第一個容器中的第一個不一致的迭代器。若是指定函數,則是須要返回值等於False。

  • equal
template <class InputIterator1, class InputIterator2, class BinaryPredicate>

bool equal (InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate pred);

當不指定最後一個函數時,其做用是檢查兩個容器內容是否徹底相等。若指定最後一個函數,則是逐元素檢查是否知足兩兩返回值均爲True。

  • search
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>

ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,

ForwardIterator2 first2, ForwardIterator2 last2,

BinaryPredicate pred);

當不指定最後一個函數時,其做用是搜索前一個序列中是否存在和第二個序列相同的子序列;若是自定義判別函數,則是逐元素比對,搜索是否存在可以和第二個序列匹配的序列。返回值爲第一個序列中匹配的位置,若是找不到則返回末位迭代器。

相關文章
相關標籤/搜索