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

C++11的functional模塊介紹和使用案例

 更新時(shí)間:2024年02月19日 10:55:44   作者:老狼IT工作室  
functional模塊是C++ 11提供了一組函數(shù)對象和算法,用于增強(qiáng)C++的函數(shù)式編程能力,本文主要介紹了C++11的functional模塊介紹和使用案例,具有一定的參考價(jià)值,感興趣的可以了解一下

functional模塊介紹

functional模塊是C++ 11提供了一組函數(shù)對象和算法,用于增強(qiáng)C++的函數(shù)式編程能力。該模塊中的函數(shù)對象和算法可以大大簡化代碼,并提供了一些有用的工具,例如函數(shù)適配器和函數(shù)對象的組合。

functional模塊中的函數(shù)對象包括:

  • plus:加法函數(shù)對象,用于將兩個(gè)參數(shù)相加。
  • minus:減法函數(shù)對象,用于將第一個(gè)參數(shù)減去第二個(gè)參數(shù)。
  • multiplies:乘法函數(shù)對象,用于將兩個(gè)參數(shù)相乘。
  • divides:除法函數(shù)對象,用于將第一個(gè)參數(shù)除以第二個(gè)參數(shù)。
  • modulus:取模函數(shù)對象,用于返回第一個(gè)參數(shù)除以第二個(gè)參數(shù)的余數(shù)。
  • negate:取反函數(shù)對象,將參數(shù)取反。

functional模塊中的算法包括:

  • bind:將一個(gè)函數(shù)對象和一些參數(shù)綁定在一起,生成一個(gè)新的函數(shù)對象。
  • mem_fn:將一個(gè)成員函數(shù)包裝成函數(shù)對象。
  • not1:對一個(gè)函數(shù)對象進(jìn)行邏輯非運(yùn)算。
  • ref:將一個(gè)對象包裝成一個(gè)引用包裝器。
  • cref:將一個(gè)對象包裝成一個(gè)const引用包裝器。

使用案例

案例一

#include <iostream>
#include <functional>

int main() {
    std::plus<int> plusFunc;
    
    int result = plusFunc(1, 2);  // 將1和2相加,返回3
    
    std::cout << result << std::endl;
    
    return 0;
}

在上面的例子中,我們創(chuàng)建了一個(gè)std::plus<int>的實(shí)例plusFunc,該實(shí)例可以用于將兩個(gè)整數(shù)相加。然后,我們調(diào)用plusFunc函數(shù)對象,傳入?yún)?shù)1和2,并將返回的結(jié)果賦值給result變量。最后,我們將結(jié)果輸出到控制臺(tái),得到的輸出結(jié)果是3。

使用std::plus函數(shù)對象的好處是,它可以與其他函數(shù)對象和算法結(jié)合使用,例如使用std::transform算法對一個(gè)容器中的元素進(jìn)行相加操作:

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums {1, 2, 3, 4, 5};
    std::vector<int> result(nums.size());
    
    std::transform(nums.begin(), nums.end(), nums.begin(), std::negate<int>());
    std::transform(nums.begin(), nums.end(), result.begin(), std::bind(std::plus<int>(), std::placeholders::_1, 2));
    
    for (int num : result) {
        std::cout << num << " ";
    }
    
    return 0;
}

在上面的例子中,我們首先使用std::transform算法和std::negate函數(shù)對象對nums容器中的元素進(jìn)行取反操作。然后,使用std::transform算法和std::bind函數(shù)將nums中的每個(gè)元素與2相加,并將結(jié)果存儲(chǔ)在result容器中。最后,我們遍歷result容器并輸出每個(gè)元素的值。輸出結(jié)果為-1 0 1 2 3,表示nums中的每個(gè)元素都加上了2。

這個(gè)例子展示了std::plus函數(shù)對象的靈活性,它可以與其他函數(shù)對象和算法結(jié)合使用,簡化了代碼,并提供了更好的可讀性和可維護(hù)性。

案例二

#include <iostream>
#include <functional>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    std::function<int(int, int)> func1 = add;
    std::function<int(int, int)> func2 = std::bind(subtract, std::placeholders::_2, std::placeholders::_1);
    
    int result1 = func1(1, 2);  // 調(diào)用add函數(shù),返回3
    int result2 = func2(2, 1);  // 調(diào)用subtract函數(shù),返回1
    
    std::cout << result1 << std::endl;
    std::cout << result2 << std::endl;
    
    return 0;
}

在上面的例子中,我們定義了兩個(gè)函數(shù)add和subtract,并使用std::function將它們包裝成函數(shù)對象func1和func2。然后,我們可以像調(diào)用普通函數(shù)一樣使用這些函數(shù)對象,通過調(diào)用func1和func2來執(zhí)行對應(yīng)的函數(shù)。輸出結(jié)果分別為3和1。

在這個(gè)例子中,我們使用了bind函數(shù)將subtract函數(shù)的兩個(gè)參數(shù)綁定在一起,然后通過調(diào)整參數(shù)的順序來實(shí)現(xiàn)減法運(yùn)算。這是functional模塊中的一個(gè)常見用法,可以用來生成新的函數(shù)對象。

案例三

#include <iostream>
#include <functional>

class MyClass {
public:
    void printMessage(const std::string& message) {
        std::cout << message << std::endl;
    }
};

int main() {
    MyClass obj;
    auto printFunc = std::mem_fn(&MyClass::printMessage);
    
    printFunc(obj, "Hello, world!");  // 調(diào)用obj對象的printMessage函數(shù),并傳入?yún)?shù)"Hello, world!"
    
    return 0;
}

在上面的例子中,我們定義了一個(gè)名為MyClass的類,該類具有一個(gè)成員函數(shù)printMessage,用于輸出一條消息。然后,我們創(chuàng)建了一個(gè)MyClass類型的對象obj,并使用std::mem_fn將printMessage函數(shù)轉(zhuǎn)化為一個(gè)函數(shù)對象printFunc。最后,我們調(diào)用printFunc函數(shù)對象,傳入obj對象和字符串參數(shù)"Hello, world!",從而調(diào)用了obj對象的printMessage函數(shù)并輸出了消息。

使用std::mem_fn函數(shù)對象的好處是,它允許我們以通用的方式處理成員函數(shù)指針和成員函數(shù)對象,可以與其他函數(shù)對象和算法結(jié)合使用。例如,我們可以通過std::transform算法將一個(gè)容器中的每個(gè)元素調(diào)用成員函數(shù)并獲取結(jié)果:

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

class MyClass {
public:
    int multiply(int x, int y) {
        return x * y;
    }
};

int main() {
    std::vector<int> nums {1, 2, 3, 4, 5};
    std::vector<int> result(nums.size());
    
    MyClass obj;
    auto multiplyFunc = std::mem_fn(&MyClass::multiply);
    
    std::transform(nums.begin(), nums.end(), result.begin(), std::bind(multiplyFunc, &obj, std::placeholders::_1, 2));
    
    for (int num : result) {
        std::cout << num << " ";
    }
    
    return 0;
}

在上面的例子中,我們定義了一個(gè)名為MyClass的類,該類具有一個(gè)成員函數(shù)multiply,用于計(jì)算兩個(gè)整數(shù)的乘積。然后,我們創(chuàng)建了一個(gè)MyClass類型的對象obj,并使用std::mem_fn將multiply函數(shù)轉(zhuǎn)化為一個(gè)函數(shù)對象multiplyFunc。最后,我們使用std::transform算法和std::bind函數(shù)將nums容器中的每個(gè)元素和數(shù)字2傳入multiplyFunc函數(shù)對象中進(jìn)行計(jì)算,并將結(jié)果存儲(chǔ)在result容器中。最后,我們遍歷result容器并輸出每個(gè)元素的值。輸出結(jié)果為2 4 6 8 10,表示nums中的每個(gè)元素都乘以了2。

這個(gè)例子展示了std::mem_fn函數(shù)對象的靈活性,它允許我們處理成員函數(shù)指針和成員函數(shù)對象,并與其他函數(shù)對象和算法結(jié)合使用,簡化了代碼,并提供了更好的可讀性和可維護(hù)性。

案例四

#include <iostream>
#include <functional>

void updateValue(int& value) {
    value += 10;
}

int main() {
    int num = 5;
    
    std::cout << "Before update: " << num << std::endl;
    
    std::function<void()> updateFunc = std::bind(updateValue, std::ref(num));
    updateFunc();
    
    std::cout << "After update: " << num << std::endl;
    
    return 0;
}

在上面的例子中,我們定義了一個(gè)名為updateValue的函數(shù),該函數(shù)接受一個(gè)引用參數(shù),并將其值增加10。然后,我們創(chuàng)建了一個(gè)名為num的整數(shù)變量,初始值為5。接下來,我們使用std::ref函數(shù)模板將num對象轉(zhuǎn)化為一個(gè)引用包裝器,并通過std::bind函數(shù)將updateValue函數(shù)與引用包裝器綁定到一起,創(chuàng)建一個(gè)函數(shù)對象updateFunc。最后,我們調(diào)用updateFunc函數(shù)對象,從而實(shí)際調(diào)用了updateValue函數(shù),并傳遞了num對象的引用作為參數(shù)。輸出結(jié)果為:

Before update: 5
After update: 15

可以看到,通過使用std::ref函數(shù)模板,我們可以在函數(shù)調(diào)用中傳遞對象的引用,從而實(shí)現(xiàn)對對象的修改。

案例五

#include <iostream>
#include <functional>

void printValue(const int& value) {
    std::cout << "Value: " << value << std::endl;
}

int main() {
    int num = 10;

    std::function<void()> printFunc = std::bind(printValue, std::cref(num));
    printFunc();

    return 0;
}

在上面的例子中,我們定義了一個(gè)名為printValue的函數(shù),該函數(shù)接受一個(gè)常量引用參數(shù),并打印該值。然后,我們創(chuàng)建了一個(gè)名為num的整數(shù)變量,初始值為10。接下來,我們使用std::cref函數(shù)模板將num對象轉(zhuǎn)化為一個(gè)常量引用包裝器,并通過std::bind函數(shù)將printValue函數(shù)與常量引用包裝器綁定到一起,創(chuàng)建一個(gè)函數(shù)對象printFunc。最后,我們調(diào)用printFunc函數(shù)對象,從而實(shí)際調(diào)用了printValue函數(shù),并傳遞了num對象的常量引用作為參數(shù)。輸出結(jié)果為:

Value: 10

可以看到,通過使用std::cref函數(shù)模板,我們可以在函數(shù)調(diào)用中傳遞對象的常量引用,從而實(shí)現(xiàn)對對象的只讀訪問。

案例六

#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

bool isOdd(int num) {
    return num % 2 != 0;
}

int main() {
    std::vector<int> nums {1, 2, 3, 4, 5};
    
    std::vector<int> evenNums;
    std::copy_if(nums.begin(), nums.end(), std::back_inserter(evenNums), std::not1(std::ptr_fun(isOdd)));
    
    for (int num : evenNums) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

在上面的例子中,我們定義了一個(gè)名為isOdd的謂詞函數(shù),該函數(shù)接受一個(gè)整數(shù)參數(shù),并判斷該整數(shù)是否為奇數(shù)。然后,我們創(chuàng)建了一個(gè)整數(shù)類型的vector容器nums,并使用std::copy_if算法和std::not1函數(shù)模板將nums容器中的所有偶數(shù)元素復(fù)制到另一個(gè)vector容器evenNums中。在調(diào)用std::copy_if算法時(shí),我們使用std::not1函數(shù)模板將isOdd謂詞函數(shù)進(jìn)行否定操作,從而實(shí)現(xiàn)對偶數(shù)元素的篩選。最后,我們遍歷evenNums容器并輸出其中的所有元素。輸出結(jié)果為:

2 4

這個(gè)例子展示了std::not1函數(shù)模板的用途,可以對謂詞函數(shù)進(jìn)行否定操作,從而實(shí)現(xiàn)相反的邏輯判斷。在實(shí)際開發(fā)中,std::not1函數(shù)模板可以用于各種需要對謂詞函數(shù)進(jìn)行邏輯反操作的場景,例如在STL算法中對元素進(jìn)行篩選、排序或處理等操作。

到此這篇關(guān)于C++11的functional模塊介紹和使用案例的文章就介紹到這了,更多相關(guān)C++11 functional模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論