c++ primer第五版(中文)習題答案 第十章第三節第三小節-lambda捕獲和返回

本博客知識記錄自己學習中的筆記或者記錄,如果有錯誤歡迎大家糾正。

本節繼續學習c++11的新特性lambda表達式

10.20標準庫定義了一個名爲count_if的算法,類似find_if,此函數接收一對迭代器,表示輸入範圍,還接受一個謂詞,會對輸入範圍中的每個元素執行。count_if返回一個計數值,表示謂詞有多少次爲真,使用count_if重寫我們重寫統計有多少單詞超過6的部分。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

//將words按字典排序,刪除重複單詞
void elimDups(std::vector<std::string> &words)
{
    //排序
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
}

//make_plural(wc, "word ", "s ")當輸入中文本中word數大於一是在word後加s,爲words爲word的複數!
std::string make_plural(size_t ctr, const std::string &word, const std::string &ending)
{
    return (ctr == 1) ? word : word + ending;
}

//原版
void biggies(std::vector<std::string> &words,
    std::vector<std::string>::size_type sz)
{
    elimDups(words);//將words按字典排序,刪除重複單詞

    //按長度排序,長度相同的單詞維持字典排序
    //  stable_sort(words.begin(), words.end(),
    //      [](const std::string &a, const std::string &b)
    //  {return a.size() >= b.size(); });

    stable_partition(words.begin(), words.end(),
        [sz](const std::string &a)
    {return a.size() <= sz;  });



    //獲取一個迭代器,指向第一個滿足size()>=sz的元素
    auto count = count_if(words.begin(), words.end(),
        [sz](const std::string &a)
    {return a.size() > sz; });



    //計算滿足size>=sz的元素的數目
    //auto count = words.end() - wc;
    std::cout << count << " " << make_plural(count, "word", "s")
        << " of length " << sz << " or longer " << std::endl;



    std::cout << std::endl;

}

int main()
{

    std::vector<std::string>vecString;
    std::string s = "";

    //注意在win下使用ctrl+z結束輸入 
    while (std::cin >> s)
    {
        vecString.push_back(s);
    }

    biggies(vecString, 6);


    system("pause");

    return 0;
}

輸出結果爲:
這裏寫圖片描述

10.21編寫一個lambda,捕獲一個局部int變量,並遞減變量值,直到它爲0.一旦變量爲0,在調用lambda應該不在遞減變量。lambda應該返回一個bool值,指出捕獲的變量是否爲0;

#include <iostream>
#include <string>
int main()
{
    int count;
    std::string boolStr = "";
    std::cout << "input a number:" << std::endl;
    std::cin >> count ;

    //定義一個函數指針 接收lambda表達式
    auto f = [&count]()->bool
    {
        bool b = false;
        if (count==0)
            b = true;
        while (count>0)
            count--;
        return b;
    };


    if (f())
        boolStr = "true";
    else
        boolStr = "false";

    std::cout << "the number  is Zero :is " << boolStr << std::endl;

    system("pause");
    return 0;
}

輸入不爲0時 的結果爲
這裏寫圖片描述
輸入0時的結果爲
這裏寫圖片描述

lambda捕獲列表
[] 空捕獲列表。lambda不能使用說在函數中的變量,一個lambda只有捕獲變量後才能使用它們。

[names] names是一個逗號分隔的名字列表[name1,name2]這些名字都是lambda表達式所在函數的局部變量。默認情況下,捕獲列表的變量都被拷貝,名字前面使用了&,則採用引用捕獲方式

[&] 隱式捕獲列表,採用引用捕獲方式,lambda體中所使用的所在函數的實體都採用引用方式使用

[=] 隱式捕獲列表,採用值捕獲方式,lambda體中所使用的所在函數的實體都採用值方式使用

[& ,names] names是一個逗號分隔的名字列表[&,name1,name2]names這些變量採用值捕獲方式,而其他採用引用方式

[= ,names] names是一個逗號分隔的名字列表[=,&name1,&name2]names這些變量採用引用捕獲方式,而其他採用值方式
其中names不能包含this,並且都要在前面加上&符號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章