欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++非變易算法-stl算法

 更新時(shí)間:2014年03月13日 16:30:01   作者:  
本文主要介紹了C++ STL算法庫(kù)中的非變易算法,是一些原則上不會(huì)變更操作數(shù)據(jù)的算法,包括:逐個(gè)查找算法、元素搜索算法、元素統(tǒng)計(jì)算法、序列匹配算法、子序列搜索算法、這些函數(shù)均包含于<algorithm>頭文件,本文給出的所有代碼在VS2010中編譯運(yùn)行通過

C++ STL標(biāo)準(zhǔn)模板庫(kù)在數(shù)據(jù)結(jié)構(gòu)和算法的實(shí)踐領(lǐng)域發(fā)揮著重要作用,極大的提高了開發(fā)效率。STL的三大組成部分為容器、迭代器、算法,本文主要講解STL算法中的非變易算法。本文從實(shí)踐的角度簡(jiǎn)單介紹了一下相關(guān)函數(shù)的使用。

C++ STL的非變易算法(Non-mutating algorithms)是一組不破壞函數(shù)數(shù)據(jù)的模板函數(shù),用來(lái)對(duì)序列數(shù)據(jù)進(jìn)行逐個(gè)處理、元素查找、子序列搜索、統(tǒng)計(jì)和匹配,基本上可用于各種容器。下面的敘述中迭代器區(qū)間默認(rèn)為[first, last),迭代器具有“++”迭代和“*”訪問操作。

逐個(gè)處理算法


for_each函數(shù)
該函數(shù)對(duì)迭代器區(qū)間的每個(gè)元素,執(zhí)行單參數(shù)函數(shù)對(duì)象定義的操作。

下面的實(shí)例程序,將打印容器vector中的每個(gè)元素。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int x) {
    cout << x << " ";
}
int main(void) {
    vector<int> v;
    for(int i = 0; i < 10; i++) {
        v.push_back(i * 2);
    }
    for_each(v.begin(), v.end(), print);
    return 0;
}

結(jié)果輸出為:

復(fù)制代碼 代碼如下:

0 2 4 6 8 10 12 14 16 18

元素查找算法

find函數(shù)
該函數(shù)用于查找等于某值的元素。如果迭代器i所指的元素滿足*i == value,則返回迭代器i。未找到滿足條件的元素,返回last。只要找到第一個(gè)滿足條件的元素就返回迭代器位置,不再繼續(xù)查找。

下面的示例程序查找容器vector中,第一個(gè)值為6的元素,打印元素位置及其前一元素。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    for(int i = 0; i < 10; i++) {
        v.push_back(i * 2);
    }
    vector<int>::iterator iv = find(v.begin(), v.end(), 6);
    if(iv == v.end()) {
        cout << "Find nothing." << endl;
    } else {
        cout << "The postion of " << *iv << " is " << iv - v.begin() << endl;
        cout << "The previous element of it is " << *(--iv) << endl;
    }
    return 0;
}

結(jié)果輸出為:

復(fù)制代碼 代碼如下:

The postion of 6 is 3
The previous element of it is 4

find_if函數(shù)

該函數(shù)是find的一個(gè)謂詞判斷版本,查找滿足謂詞判斷函數(shù)的元素。

下面的實(shí)例程序?qū)ふ胰萜鱲ector中第一個(gè)能被3整除的元素。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int divBy3(int x) {
    return x % 3 ? 0 : 1;
}
int main(void) {
    vector<int> v;
    for(int i = 1; i < 10; i++) {
        v.push_back(i * 2);
    }
    vector<int>::iterator iv = find_if(v.begin(), v.end(), divBy3);
    if(iv == v.end()) {
        cout << "None could be divided by 3 with no remaineder." << endl;
    } else {
        cout << *iv << " could be divided by 3 with no remainder." << endl;
    }
    return 0;
}

結(jié)果輸出為:

復(fù)制代碼 代碼如下:

6 could be divided by 3 with no remainder.

adjacent_find函數(shù)

該函數(shù)用于查找相等或滿足條件的鄰近元素對(duì)。它有兩個(gè)使用原型,一個(gè)用于查找相等的兩個(gè)連續(xù)元素,另一個(gè)使用二元謂詞判斷,查找滿足條件的鄰近元素對(duì)。

下面的實(shí)例程序用于尋找容器中相等的元素和奇偶性相同的元素。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int parity_equal(int x, int y) {
    return (x - y) % 2 == 0 ? 1 : 0;
}
int main(void) {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(5);
    v.push_back(5);
    v.push_back(7);
    vector<int>::iterator iv = adjacent_find(v.begin(), v.end());
    if(iv != v.end()) {
        cout << "There are two equal elements." << endl;
        cout << "It is " << *iv << endl;
    }
    iv = adjacent_find(v.begin(), v.end(), parity_equal);
    if(iv != v.end()) {
        cout << "There are two parity euqal elements." << endl;
        cout << "They are " << *iv << " and ";
        iv++;
        cout << *iv << endl;
    }
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

There are two equal elements.
It is 5
There are two parity euqal elements.
They are 3 and 5

find_first_of函數(shù)

該函數(shù)用于查找某個(gè)范圍之內(nèi)的元素。它有兩個(gè)使用原型,一個(gè)是相等,另一個(gè)是二元謂詞判斷。元素找到則返回迭代器,否則返回末位置。

下面的實(shí)例程序用于計(jì)算容器v2中元素在容器v中重合出現(xiàn)的首位置。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);
    v.push_back(5);
    v.push_back(5);
    v.push_back(7);
    vector<int> v2;
    v2.push_back(3);
    v2.push_back(3);
    v2.push_back(5);
    vector<int>::iterator iv = find_first_of(v.begin(), v.end(), v2.begin(), v2.end());
    cout << "The position of the first equal element is " << iv - v.begin() << endl;
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The position of the first equal element is 3

元素統(tǒng)計(jì)算法

count函數(shù)
該函數(shù)用于計(jì)算容器中某個(gè)給定值的出現(xiàn)次數(shù)。它有兩個(gè)使用原型,區(qū)別在于計(jì)數(shù)是直接返回還是引用返回。

下面的實(shí)例程序計(jì)算了容器中5的出現(xiàn)次數(shù),結(jié)果直接返回。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    for(int i = 0; i < 17; i++) {
        v.push_back(i % 6);
    }
    int num = count(v.begin(), v.end(), 5);
    cout << "The number of 5 is " << num << endl;
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The number of 5 is 2

count_if函數(shù)

該函數(shù)使用謂詞判斷函數(shù),統(tǒng)計(jì)迭代器區(qū)間上滿足條件的元素個(gè)數(shù)。它有兩個(gè)使用原型,區(qū)別在與計(jì)數(shù)是直接返回還是引用返回。

下面的實(shí)例程序統(tǒng)計(jì)了容器中大于10的數(shù)字的出現(xiàn)次數(shù),結(jié)果直接返回。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int greaterThan10(int x) {
    return x > 10 ? 1 : 0;
}
int main(void) {
    vector<int> v;
    for(int i = 0; i < 17; i++) {
        v.push_back(i);
    }
    int num = count_if(v.begin(), v.end(), greaterThan10);
    cout << "The number of the figure that greater than 10 is " << num << endl;
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The number of the figure that greater than 10 is 6

序列匹配算法

mismatch函數(shù)
該函數(shù)用于比較兩個(gè)序列,找出首個(gè)不匹配元素的位置。它有兩個(gè)使用原型,分別為不相等和不滿足二元謂詞條件。

該函數(shù)還涉及到pair的使用。

下面的實(shí)例程序比較兩個(gè)整型容器,并找出不匹配的數(shù)字。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v1, v2;
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(5);
    v2.push_back(3);
    v2.push_back(5);
    v2.push_back(7);
    pair<vector<int>::iterator, vector<int>::iterator> result = mismatch(v1.begin(), v1.end(), v2.begin());
    if(result.first == v1.end() && result.second == v2.end()) {
        cout << "v1 is same as v2." << endl;
    } else {
        cout << "The dismatching figure are "
             << *(result.first) << " and "
             << *(result.second) << endl;
    }
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The dismatching figure are 5 and 7

equal函數(shù)

該函數(shù)逐一比較兩個(gè)序列的元素是否相等,返回值為true/false,不返回迭代器值。它有兩個(gè)使用原型,分別為元素相等和二元謂詞判斷條件。

下面的實(shí)例程序用于比較兩容器中數(shù)字絕對(duì)值是否相等。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int absEqual(int x, int y) {
    //return (x == abs(y) || y == abs(x)) ? 1 : 0;
    return abs(x) == abs(y) ? 1 : 0;
}
int main(void) {
    vector<int> v1, v2;
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(5);
    v2.push_back(3);
    v2.push_back(5);
    v2.push_back(-5);
    if(equal(v1.begin(), v1.end(), v2.begin(), absEqual)) {
        cout << "The elements of v1 and v2 are equal in abosolute value." << endl;
    } else {
        cout << "The elements of v1 and v2 are not equal in abosolute value." << endl;
    }
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The elements of v1 and v2 are equal in abosolute value.

子序列搜索算法

search函數(shù)

該函數(shù)在一個(gè)序列中搜索與另一序列匹配的子序列。它有兩個(gè)使用原型,分別為完全匹配和二元謂詞判斷。匹配成功則返回子序列的首個(gè)元素的迭代器值。

search函數(shù)與find_first_of函數(shù)形似,但不相同。search找的是一塊相同的區(qū)域,要求這塊區(qū)域與后面列表的元素及其順序相同;find_first_of找的是一個(gè)元素,只要這個(gè)元素是后面一個(gè)列表的任意一個(gè)就行。

下面的實(shí)例程序說(shuō)明了search與find_first_of的不同。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
int main(void) {
    vector<int> v1, v2;
    v1.push_back(1);
    v1.push_back(4);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v2.push_back(2);
    v2.push_back(3);
    v2.push_back(4);
    vector<int>::iterator ivSearch, ivFind;
    ivSearch = search(v1.begin(), v1.end(), v2.begin(), v2.end());
    ivFind = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
    cout << "Position of search: " << ivSearch - v1.begin() << endl;
    cout << "Position of find_first_of: " << ivFind - v1.begin() << endl;
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

Position of search: 2
Position of find_first_of: 1

search_n函數(shù)

該函數(shù)用于搜索序列中是否有一系列元素值均為某個(gè)給定值的子序列。它有兩個(gè)使用原型,分別為值相等和滿足謂詞判斷條件。

下面的實(shí)例程序展示了尋找3個(gè)連續(xù)的數(shù)字8的過程。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    v.push_back(3);
    v.push_back(8);
    v.push_back(8);
    v.push_back(8);
    v.push_back(4);
    vector<int>::iterator iv = search_n(v.begin(), v.end(), 3, 8);
    if(iv == v.end()) {
        cout << "There are no three consecutive 8." << endl;
    } else {
        cout << "Three consecutive 8 is founded." << endl;
    }
    return 0;
}

結(jié)果輸出為:

復(fù)制代碼 代碼如下:

Three consecutive 8 is founded.

find_end函數(shù)
該函數(shù)用于在一個(gè)序列中搜索出最后一個(gè)與另一序列匹配的子序列。用search函數(shù)作用相似,方向相反。

下面的實(shí)例程序,展示了搜索容器v中最后一個(gè)與v1匹配的子序列的過程。

復(fù)制代碼 代碼如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v, v2;
    v.push_back(1);
    v.push_back(3);
    v.push_back(5);
    v.push_back(3);
    v.push_back(5);
    v.push_back(7);
    v2.push_back(3);
    v2.push_back(5);
    vector<int>::iterator iv = find_end(v.begin(), v.end(), v2.begin(), v2.end());
    if(iv != v.end()) {
        cout << "The position of last matching subsequence is " << iv - v.begin() << endl;
    }
    return 0;
}

輸出結(jié)果為:

復(fù)制代碼 代碼如下:

The position of last matching subsequence is 3

相關(guān)文章

  • C++引用的詳細(xì)解釋

    C++引用的詳細(xì)解釋

    以下是對(duì)C++中引用的使用進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C++實(shí)現(xiàn)LeetCode(130.包圍區(qū)域)

    C++實(shí)現(xiàn)LeetCode(130.包圍區(qū)域)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(130.包圍區(qū)域),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語(yǔ)言工程文件該如何寫(以三子棋游戲?yàn)槔?

    C語(yǔ)言工程文件該如何寫(以三子棋游戲?yàn)槔?

    工程上寫代碼應(yīng)分為多個(gè)文件,那么你知道C語(yǔ)言工程文件該如何寫嗎,本文就以以三子棋游戲?yàn)槔?介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • C++調(diào)試追蹤class成員變量的方法

    C++調(diào)試追蹤class成員變量的方法

    本文所講的是不通過修改一個(gè)class的成員,就能夠追蹤其成員。方法就是類似C語(yǔ)言中的函數(shù)指針
    2013-11-11
  • C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲(單人版)

    C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲(單人版)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲單人版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表

    C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單單向鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Eclipse中C++連接mysql數(shù)據(jù)庫(kù)

    Eclipse中C++連接mysql數(shù)據(jù)庫(kù)

    這篇文章主要為大家詳細(xì)介紹了Eclipse中C++連接mysql數(shù)據(jù)庫(kù) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • VC中CWinThread類以及和createthread API的區(qū)別分析

    VC中CWinThread類以及和createthread API的區(qū)別分析

    這篇文章主要介紹了VC中CWinThread類以及和createthread API的區(qū)別分析,較為詳細(xì)的講述了CWinThread類的原理,并以實(shí)例形式對(duì)AfxBeginThread函數(shù)的內(nèi)部實(shí)現(xiàn)進(jìn)行了解釋說(shuō)明,需要的朋友可以參考下
    2014-10-10
  • C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式

    C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式

    這篇文章主要介紹了C++中的頭文件與Extern(外部函數(shù)調(diào)用)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++ 中的Swap函數(shù)寫法匯總

    C++ 中的Swap函數(shù)寫法匯總

    這篇文章主要介紹了C++ 中的Swap函數(shù)寫法匯總,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論