第7章:容器函数、lambad表达式

1,容器函数的使用、lambad表达式的使用
2,源码

#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <vector>
#include <list>
#include <string>

/*
算法:算法不会执行容器操作,因此他们自身不能改变容器大小
1,读容器一些函数
    find:可以操作任何类型的容器和数组,如果找到值返回对应元素的迭代器
    accumulate:求和算法
    equal:查看两个序列是否有相等的值,相等返回true,不相等返回false
2,写容器一些函数
   fill:    可以对容器进行写操作
   fill_n:  对容器进行n个元素的写操作
3,back_inserter:插入迭代器
4,copy:拷贝函数,拷贝的目的序列至少要和输入序列一样多的元素
  replace:替换函数
5,重新排序算法
  sort: 排序
  unique:消除相同元素,仅能比较相邻元素
6,相排序算法,添加函数
  相算法添加的参数叫谓词
*/




using namespace std;
bool is_shorted(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}


int main(void)
{
    /*Find----------------------------------------------------------------------------------------------*/
    //如果找到返回给定值元素的迭代器
    vector<int> vector_num = {0,1,2,3,4,5,6,7,8,9,10};
    auto vector_result = find(vector_num.begin(), vector_num.end(), 8);
    cout << (vector_result == vector_num.end() ? "Data NO Find" : "Data Find") << endl;

    list<string> string_buf = {"xiaohua", "xiaotao", "xiaogou", "xiaojun"};
    string str= "xiaotao";
    auto list_rsult = find(string_buf.begin(), string_buf.end(), str);
    cout << (list_rsult == string_buf.end() ? "Data NO Find" : "Data Find") << endl;

    int arry[] = {0,1,2,3,4,5,6,7,8,9,10};
    int *arry_result = find(begin(arry), end(arry), 8);
    cout << *arry_result <<endl;



    /*accumulate--------------------------------------------------------------------------------------*/
    //前两个参数初始化求和范围,最后一个参数初始化sum值为0
    int sum = accumulate(vector_num.begin(), vector_num.end(), 0);
    cout << "sum = " << sum <<endl;

    //容器序列中的元素必须和第三个参数元素类型相同,或转换成第三个参数类型
    //如果第三个元素不进行类型转换会出现一个<const char *>编译错误; ""为const char *类型
    auto str_begin = string_buf.begin();
    while (str_begin != string_buf.end())
    {
        *str_begin += "; ";
        str_begin++;
    }
    string str_sum = accumulate(string_buf.begin(), string_buf.end(), string(""));
    cout << str_sum << endl;


    /*equal-----------------------------------------------------------------------------------------*/
    list<string> equal_str1 = {"tian", "di", "ren", "he"};
    list<string> equal_str2 = {"tian", "di", "ren", "he"};

    bool result_equal = equal(equal_str1.cbegin(), equal_str1.cend(), equal_str2.cbegin());
    cout << (result_equal ? "Equal" : "No Equal") << endl;


    /*fill-----------------------------------------------------------------------------------------*/
    //fill不进行容器操作,因此不会改变容器大小;fill_n算法不会检查写操作,所以在在指定操作长度时候一定要注意
    fill(vector_num.begin(), vector_num.end(), 0);
    cout << accumulate(vector_num.begin(), vector_num.end(), 0) << endl;
    fill(vector_num.begin(), vector_num.begin()+vector_num.size()/2, 10);
    cout << accumulate(vector_num.begin(), vector_num.end(), 0) << endl;
    //下面使用方式比较危险,操作n个元素
    fill_n(vector_num.begin(), 10, 10);
    cout << accumulate(vector_num.begin(), vector_num.end(), 0) << endl;



    /*back_inserter插入迭代器-----------------------------------------------------------------------*/
    vector<int> back_inserter_num;
    //创建一个插入迭代器,并添加10个元素
    fill_n(back_inserter(back_inserter_num), 10, 0);


    /*copy拷贝-------------------------------------------------------------------------------------*/
    int a1[] = {0,1,2,3,4,5,6,7,8,9,10};
    int a2[sizeof(a1)/sizeof(*a1)];

    //a2和a1有相同的元素
    copy(begin(a1), end(a1), a2);
    //用80替换8
    replace(begin(a1), end(a1), 8, 80);


    /*sort或unique排序---------------------------------------------------------------------------*/
    vector<string> string_words = {"My", "name", "is", "bandaostart", "My", "age", "is", "28"};

    //sort排序
    sort(string_words.begin(), string_words.end());
    auto string_words_beg = string_words.begin();
    while (string_words_beg != string_words.end())
    {
        cout << *string_words_beg++ << " ";
    }
    cout << endl;

    //unique去掉重复,因为算法函数不能执行容器操作,需要使用容器操作函数来执行真正的删除
    //返回重复元素迭代器地址,unique仅能比较相邻两个元素是否相同
    auto unique_beg = unique(string_words.begin(), string_words.end());
    string_words_beg = string_words.begin();
    while (string_words_beg != string_words.end())
    {
        cout << *string_words_beg++ << " ";
    }
    cout << endl;
    string_words.erase(unique_beg, string_words.end());
    string_words_beg = string_words.begin();
    while (string_words_beg != string_words.end())
    {
        cout << *string_words_beg++ << " ";
    }
    cout << endl;


    /*重载算法函数------------------------------------------------------------------------------*/
    //is_shorted为重载过的函数
    sort(string_words.begin(), string_words.end(), is_shorted);
    for(const auto &s : string_words)
    {
        cout << s << " ";
    }
    cout << endl;

    return 0;
}
#include <iostream>
#include <algorithm>
#include <iterator>
#include <numeric>
#include <vector>
#include <string>
#include <functional>

/*
1,调用lambda表达式
  [](const string &s1, const string &s2){ return s1.size()>s2.size();}         //默认返回类型,不能包含return意外的语句
  [](const string &s1, const string &s2) ->int { return s1.size()>s2.size();}  //指定返回类型,可以包含return以外的语句
2,lambda参数传递
3,参数绑定
   auto newcallable = bind(callable, arg_list);
*/




using namespace std;
using namespace std::placeholders;

bool check_sized(const string &s1, size_t sz)
{
    return s1.size() > sz;
}


void sort_num(int num1, int num2, int num3, int num4, int num5)
{
    cout << num1 << " ";
    cout << num2 << " ";
    cout << num3 << " ";
    cout << num4 << " ";
    cout << num5 << " ";
    cout << endl;
}






int main(void)
{
    vector<string> string_words = {"My", "name", "is", "bandaostart", "My", "age", "is", "28"};


    /*lambad表达式的使用-----------------------------------------------------------------------*/
    //unique仅能比较相邻元素
    sort(string_words.begin(), string_words.end());
    string_words.erase(unique(string_words.begin(), string_words.end()), string_words.end());
    sort(string_words.begin(), string_words.end(), [](const string &s1, const string &s2){return s1.size()>s2.size();});
    for (const auto &s : string_words)
    {
        cout << s << " ";
    }
    cout << endl;


    /*lambda参数传输-------------------------------------------------------------------------*/
    unsigned int sz = 3;
    auto wc = find_if(string_words.begin(), string_words.end(), [sz](const string &s1){return s1.size()< sz ;});
    cout << "num = " << wc-string_words.begin() << endl;
    auto string_words_beg = string_words.begin();

    while(string_words_beg != wc)
    {
        cout << *string_words_beg++ << " ";
    }
    cout << endl;


    /*lambda值的捕获------------------------------------------------------------------------*/
    //值在创建时拷贝,不是在使用时拷贝
    {
        size_t vt = 100;

        auto f = [vt] ()  {return vt ;};  //创建时将vt拷贝到名为f的可调用对象

        vt = 0;

        cout << f() << endl;
    }

    //引用捕获
    {
        size_t vt = 100;

        auto f = [&vt](){return vt ;};

        vt = 0;

        cout << f() <<endl;
    }


    /*可变lamdba-------------------------------------------------------------------------*/
    //对于一个值被拷贝量,lamdba不能改变其变量;如果想要改变加上mutable
    {
        size_t vt  = 100;

        //auto f = [vt] () {return ++vt; }; 不加mutable会提示编译错误
        auto f = [vt] () mutable {return ++vt; };

        cout << f() <<endl;
    }

    //如果是引用变量值则可以修改
    {
        size_t vt = 100;

        auto f = [&vt] () {return ++vt ;};

        cout << f() << endl;
    }


    /*指定lamdba返回类型----------------------------------------------------------------*/
    //如果lamdba包含任何return之外的语句,则编译器默认返回void
    {
        vector<int> num = {1, -2, 3, -4, 5, -6, 7, -8, 9};

        //负值取绝对值
        transform(num.begin(), num.end(), num.begin(), [](const &temp_num){return temp_num > 0 ? temp_num : -temp_num; });
        for (const auto &temp_num : num)
        {
            cout << temp_num << " ";
        }
        cout << endl;

        //指定返回类型,  使用 ->int, 或者包含非return语句编译器报错
        transform(num.begin(), num.end(), num.begin(),
                  [](const &temp_num) ->int
                  {  if (temp_num % 2 == 0) return temp_num; else return -temp_num  ;});
        for (const auto &temp_num : num)
        {
            cout << temp_num << " ";
        }
        cout << endl;

    }


    /*bind函数使用---------------------------------------------------------------------------------*/
    //check_sized函数第二个参数指定为6,在调用CheckSized时,传入第一个参数即可
    auto CheckSized = bind(check_sized, std::placeholders::_1, 6);
    string words = "hello";
    cout << CheckSized(words) << endl;

    auto SortNum = bind(sort_num, 1, 2, 3, std::placeholders::_2, std::placeholders::_1);
    SortNum(5, 4);

}

猜你喜欢

转载自blog.csdn.net/ksmtnsv37297/article/details/89392835