10.11
#include <iostream>
#include <vector>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
bool isShorter(const string &s1,const string &s2 ){
return s1.size()<s2.size();
}
void elimDups(vector<string> &word){
//按字典排序word,便于查找重复单词
sort(word.begin(),word.end());
//unique重排输入范围,使得每个单词出现一次
//unique返回最后一个不重复位置之后的迭代器
auto end_unique = unique(word.begin(),word.end());
//用erase删除后面的无用元素
word.erase(end_unique,word.end());
}
int main()
{
ifstream ifs("C:\\study\\c++test\\endless.txt");
string str;
vector<string> vec;
while(ifs>>str){
vec.push_back(str);
}
for(auto &c: vec){
cout<<c<<" ";
}
cout<<endl;
elimDups(vec);
stable_sort(vec.begin(),vec.end(),isShorter);
for(auto c: vec){
cout<<c<<" ";
}
return 0;
}
10.12
首先是方法文件
#ifndef FUNCTION_H_INCLUDED
#define FUNCTION_H_INCLUDED
#include<vector>
#include<string>
using namespace std;
bool compareIsbn(const Sales_data &s1,const Sales_data &s2){
return s1.isbn().size()<s2.isbn().size();
}
#endif // FUNCTION_H_INCLUDED
然后是执行文件
#include <iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include"function.h"
using namespace std;
int main(char **addr)
{
Sales_data d1("aa"), d2("aaaa"), d3("aaa"), d4("z"), d5("aaaaz");
vector<Sales_data> vec{d1,d2,d3,d4,d5};
sort(vec.begin(),vec.end(),compareIsbn);
for(auto v:vec){
cout<<v.isbn()<<endl;
}
return 0;
}
10.13
#include <iostream>
#include <vector>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
bool isShorter(const string &s1,const string &s2 ){
return s1.size()<s2.size();
}
bool isFive(const string &s){
return s.size()>=5;
}
void elimDups(vector<string> &word){
//按字典排序word,便于查找重复单词
sort(word.begin(),word.end());
//unique重排输入范围,使得每个单词出现一次
//unique返回最后一个不重复位置之后的迭代器
auto end_unique = unique(word.begin(),word.end());
//用erase删除后面的无用元素
word.erase(end_unique,word.end());
}
int main()
{
ifstream ifs("C:\\study\\c++test\\endless.txt");
string str;
vector<string> vec;
while(ifs>>str){
vec.push_back(str);
}
for(auto &c: vec){
cout<<c<<" ";
}
cout<<endl;
elimDups(vec);
stable_sort(vec.begin(),vec.end(),isShorter);
auto previt =std::partition(vec.begin(),vec.end(),isFive);
for(auto iter=previt;iter!=vec.end();iter++){
cout<<*iter<<" ";
}
return 0;
}
10.14
auto sum=[](int a,int b){return a+b;};
10.15
int a=9;
[a](int b){return a+b;};
10.16
我没有求有多少个大于4的元素
void biggies(vector<string> &words,vector<string>::size_type sz){
elimDups(words);//按照字典排序删除重复单词
//按照长度排序,长度相同的字符串按照字典顺序排序
stable_sort(words.begin(),words.end(),[](const string &a,const string &b){return a.size()<b.size();});
//获取一个迭代器,指向第一个满足size()>=sz的元素
auto wc = find_if(words.begin(),words.end(),[sz](const string &s){return s.size()>=sz;});
for_each(wc,words.end(),[](const string &s){cout<<s<<" ";});
cout<<endl;
}
10.17
不使用原本的complareIsbn方法
使用lambda表达式
#include <iostream>
#include"Sales_date.h"
#include<fstream>
#include <iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include"function.h"
using namespace std;
int main(char **addr)
{
Sales_data d1("aa"), d2("aaaa"), d3("aaa"), d4("z"), d5("aaaaz");
vector<Sales_data> vec{d1,d2,d3,d4,d5};
sort(vec.begin(),vec.end(),[](const Sales_data &s1,const Sales_data &s2){return s1.isbn().size()<s2.isbn().size();});
for(auto v:vec){
cout<<v.isbn()<<endl;
}
return 0;
}
10.18
#include <iostream>
#include <vector>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &word);
bool isShorter(const string &s1,const string &s2 ){
return s1.size()<s2.size();
}
bool isFive(const string &s){
return s.size()>=5;
}
void biggies(vector<string> &words,vector<string>::size_type sz){
elimDups(words);//按照字典排序删除重复单词
//按照长度排序,长度相同的字符串按照字典顺序排序
stable_sort(words.begin(),words.end(),[](const string &a,const string &b){return a.size()<b.size();});
//获取一个迭代器,指向第一个满足size()>=sz的元素
auto wc = find_if(words.begin(),words.end(),[sz](const string &s){return s.size()>=sz;});
for_each(wc,words.end(),[](const string &s){cout<<s<<" ";});
cout<<endl;
}
void elimDups(vector<string> &word){
//按字典排序word,便于查找重复单词
sort(word.begin(),word.end());
//unique重排输入范围,使得每个单词出现一次
//unique返回最后一个不重复位置之后的迭代器
auto end_unique = unique(word.begin(),word.end());
//用erase删除后面的无用元素
word.erase(end_unique,word.end());
}
int main()
{
ifstream ifs("C:\\study\\c++test\\endless.txt");
string str;
vector<string> vec;
while(ifs>>str){
vec.push_back(str);
}
for(auto &c: vec){
cout<<c<<" ";
}
cout<<endl;
//去重复并且排序
elimDups(vec);
stable_sort(vec.begin(),vec.end(),isShorter);
//使用partition
auto previt =std::partition(vec.begin(),vec.end(),[](const string &s){return s.size()>=5;});
for(auto iter=previt;iter!=vec.end();iter++){
cout<<*iter<<" ";
}
return 0;
}
10.19
18题中
auto previt =std::partition(vec.begin(),vec.end(),[](const string &s){return s.size()>=5;});
要改成stable_partition方法即可
auto previt =std::stable_partition(vec.begin(),vec.end(),[](const string &s){return s.size()>=5;});
10.20
#include <iostream>
#include <vector>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &word);
bool isShorter(const string &s1,const string &s2 ){
return s1.size()<s2.size();
}
void elimDups(vector<string> &word){
//按字典排序word,便于查找重复单词
sort(word.begin(),word.end());
//unique重排输入范围,使得每个单词出现一次
//unique返回最后一个不重复位置之后的迭代器
auto end_unique = unique(word.begin(),word.end());
//用erase删除后面的无用元素
word.erase(end_unique,word.end());
}
int main()
{
ifstream ifs("C:\\study\\c++test\\endless.txt");
string str;
vector<string> vec;
while(ifs>>str){
vec.push_back(str);
}
for(auto &c: vec){
cout<<c<<" ";
}
cout<<endl;
elimDups(vec);
auto count_vec = count_if(vec.begin(),vec.end(),[](const string &s){return s.size()>6;});
cout<<count_vec<<endl;
return 0;
}
10.21
//定义一个int型的变量
int number=6;
//变量递减
auto _number=[&number]()-> bool{return (number==0?true:!(number--));};
while(!_number()){//lambda返回flase的时候执行循环
cout<<number<<endl;
}
10.22
首先是方法函数
void num_6(const string &s,int &i){
if(s.size()>5){
i++;
}
}
检验函数的正确性
int main()
{
ifstream ifs("C:\\study\\c++test\\endless.txt");
string str;
vector<string> vec;
while(ifs>>str){
vec.push_back(str);
}
int ii=0;
for(auto c:vec){
num_6(c,ii);
}
cout<<ii<<endl;
cout<<"--------------------"<<endl;
return 0;
}
10.23
比绑定的函数参数多一个
10.24
老规矩上函数
bool check_size(const string& s, size_t i)
{
return i > s.size();
}
接下来是用bind绑定参数
int main()
{
vector<int> vec{1,2,3,4,5,6,7};
string str="xxx";
//注意bind的参数,vec中的元素充当是check_size的第二个参数
auto iter = find_if(vec.begin(),vec.end(),bind(check_size, str,_1));
cout<<*iter<<endl;
return 0;
}
10.25
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string& s, size_t i)
{
return i > s.size();
}
void elimdups(vector<string> &words){
sort(words.begin(),words.end());
auto end_unique = unique(words.begin(),words.end());
words.erase(end_unique,words.end());
}
void biggies(vector<string> &words,vector<string>::size_type sz){
elimdups(words);
//stable_partition的三个参数,前两个表示范围,最后一个对元素进行划分,从返回true开始
auto previt =std::stable_partition(words.begin(),words.end(),bind(check_size,_1,sz));
//for_each的三个参数 前两个表示范围,后一个表示对元素的操作
for_each(previt,words.end(),[](const string &s){cout<<s<<" ";});
}
int main()
{
std::vector<std::string> v{ "the", "quick", "red", "fox", "jumps",
"over", "the", "slow", "red", "turtle" };
biggies(v, 4);
return 0;
}
网友评论