#include<iostream>#include<algorithm>#include<sstream>#include<vector>#include<cstring>#include<functional>//bind函数的头文件//#include <boost/tokenizer.hpp>usingnamespace std;usingnamespace placeholders;//_n占位符所要使用的命名空间/*** 本程序首先将一个字符串划分成单词存储到容器中去* 在使用系统提供的算法时,讨论了一元谓词与二元谓词* 接下来讨论了lambda表达式及其用法** 在很多地方使用同一个函数,一个操作需要很多语句才能完成* 通常采用使用函数而不是lambda表达式,为了解决一元谓词等的限制,引入标准库bind函数*/void elimDups(vector<string>& words){sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());words.erase(end_unique, words.end());}bool compElement(const string & str){return str.length()<5?1:0;}void displayVector(vector<string>::iterator beginPos,vector<string>::iterator endPos){while(beginPos != endPos){cout <<*(beginPos++)<< endl;}cout << endl;}/**< 值捕获 */void fcn1(){size_t v1 =42;//将v1拷贝到名为f的可调用对象auto f =[v1]{return v1;};//从lambda类类型生成一个类对象f。在f创建时,使用捕获变量V1给其数据成员v1进行初始化(值拷贝),v1 =0;v1 = f();cout <<"显式捕获--值赋值--"<< v1 << endl;}/**< 默认情况下,值捕获是不能改变变量的值,若要改变则需加上mutable关键字 */void fcn4(){size_t v4 =42;auto f4 =[v4]()mutable{return++v4;//观察与fcn1的区别};cout <<"mutable--"<< f4()<< endl;cout <<"mutable--v--"<< v4 << endl;//非引用捕获无法更改捕获变量的值}/**< 引用捕获 */void fcn2(){size_t v1 =42;auto f2 =[&v1]//在f2被创建时,其成员变量v1将引用外部的v1{v1 =100;//v1是非const类型,故而能被修改return v1;};//构建一个lambda类对象,使用v1给对象的数据成员赋值,此时函数体并未执行cout << v1 << endl;//42---函数体未执行cout << f2()<< endl;//100---函数体开始执行cout <<"显式捕获--引用赋值--"<< v1 << endl;//42---采用的是引用捕获,在函数体被执行时,v1被修改}/**< 隐式捕获 */void fcn3(){int v3 =0;auto f3 =[=]{return v3;};cout <<"隐式捕获--值赋值--"<< f3()<< endl;auto f4 =[&]{v3 =100;return v3;};cout <<"隐式捕获--引用赋值--"<< f4()<< endl;cout << v3 << endl;}/*** 混合捕获方式(前边的符号代表隐式捕获的类型,后边的参数列表要与其不同)* [&,identifier_list] identifier_list采用值捕获方式,identifier_list列表中的各个名字前不能使用&* 而任何隐式捕获的变量都采用引用方式捕获* [=,identifier_list] identifier_list采用引用捕获方式,identifier_list列表中的各个名字之前必须使用&* 而任何隐式捕获的变量都采用引用方式捕获*//**< bind函数的实现 *///先实现要绑定的函数,然后在要使用的地方使用bind函数进行绑定bool newCompElement(const string & str,constint sz){return str.length()< sz ?1:0;}int main(){char wordLine[]="The consequences of today are determined by the actions of the past.To change your future, alter your decisions today.";vector<string> strVector;/**istringstream lineStream(wordLine);//istringsream默认以空格或 或 切割字符串,不能进行设定while(lineStream>>word){strVector.push_back(word);}*///strtok()char seps[]=" ,. ";char* word = strtok(wordLine, seps);while(word != NULL){strVector.push_back(word);word = strtok(NULL, seps);}for(auto val : strVector){cout << val <<" ";}cout << endl;// elimDups(strVector);/**< 根据字符串长度是否 < 5,将容器中的字符串重新排列 */vector<string>::iterator partPos = partition(strVector.begin(), strVector.end(), compElement);displayVector(partPos, strVector.end());/*** 尾置返回类型* auto func(int i) -> int(*)[10];* 这是一个函数声明,该函数返回一个指针,该指针指向含有10个int类型的数组** lambda表达式:* [capture list] (parameter list) -> return type {function body}* 通常定义一个可调用f,使其等于该lambda表达式* 例如:auto f = [] {return 42;};** lambda的调用方式与普通函数的调用方式相同,都是使用调用运算符* cout << f() << endl;//打印42* lambda表达式可以忽略参数列表和返回类型,但必须包含捕获列表和函数体(内容可以为空)* 捕获列表只用于局部非static变量,lambda可以直接使用局部static变量和在其函数之外声明的名字*/size_t sz =5;auto compSize =[sz](const string & str)->bool{return str.length()< sz;};partPos = partition(strVector.begin(), strVector.end(), compSize);//这里也可以直接使用lambda表达式替换compSizedisplayVector(partPos, strVector.end());/**< 使用bind函数进行绑定 */auto newCompEle = bind(newCompElement, _1, sz);partPos = partition(strVector.begin(),strVector.end(), newCompEle);//相当于partition(strVector.begin(),strVector.end(), bind(newCompElement, _1, sz));displayVector(partPos, strVector.end());/**< 测试lambda的各种捕获 */fcn1();fcn2();fcn3();fcn4();return0;}/*** 默认情况下,bind那些不占位符在给bind对象进行赋值时采用的是值拷贝的方式* 如要要用引用方式传递或绑定对象无法进行拷贝,使用ref来实现引用方式传递** bind(words.begin(),words.end(),bind(print,ref(os),_1,' ');* ostream &print(ostream &os,const string &s,char c);** 函数ref返回一个对象,包含给定的引用,此对象是可以拷贝的。* cref函数保存const引用的类*/