调用操作符和函数对象


p451-p467

今天不看完第三部分不睡觉~!!!!!!!!!!!!!!!!!!日,明天还要爬山。。

简单说来,就是可以把对象当成函数看

//函数对象,
#include"head.h"
struct absInt{
	int operator() (int val){//求绝对值
		return val < 0 ? -val : val;
	}
	
};

int main(){
	int i = -33;
	absInt absObj;
	unsigned int ui = absObj(i);
	std::cout << ui << std::endl;
}


pe14_31



//完成如果-则-否则。
#include"head.h"
struct func{
	int operator() (bool b, int val1, int val2){//求绝对值
		return b ? val1 : val2;
	}
};

int main(){
	int a = 33,b = 44;
	func func1;
	bool open;//状态标记
	open = 1;
	unsigned int ui = func1(open, a, b);
	std::cout << ui << std::endl;
	open = 0;
	ui = func1(open, a, b);
	std::cout << ui << std::endl;

}

习题14.32

Q:一个重载的函数调用操作符可以接受多少个操作数?

A:无数?!貌似是。。。。这问题目的何在(mark

int operator() (bool b, int val1, int val2,...);//声明

unsigned int ui = func1(open, a, b,a,a,a,a,a,a,a,a,a,a);//调用


=================================================================================================================================

接下来该讨论以前遗留的比较有印象的GT6(greater than 6)函数的缺陷了:代码(函数)重用性(我也不知到合适词汇)太低,6是写进去的,要改变只能改代码

bool GT6(const string &s){
return s.size() >= 6;
}

review:泛型算法,iterator,inserter,各种capacity对算法的支持与不支持,capacity的特征,顺序,可以随机读取之类的,前插入后插入。。。

另外,string也有几个函数是跳过来的,什么append,merge之类的

vector<string>::size_tyoe wc = count_if(words.begin(), words.end(), GT6);//标准库算法提供的计数算法,第三个是条件判断,返回bool类型,GT6有个title,叫谓词函数


//用GT_cls弥补GT6的缺陷
#include"head.h"
class GT_cls{
public:
	GT_cls(size_t val = 0): bound(val){}//构造函数接收一个边界值,有默认实参0	
	bool operator()(const std::string &s){//对输入的string对象进行判断
		return s.size() >= bound;
	}
private:
	std::string::size_type bound;
	
};


int main(){
	GT_cls gt_3(3);
	GT_cls gt_4(4);
	GT_cls gt_5(5);
	std::string str1("he");
	std::string str2("hell");
	std::string str3("world");
	
	std::vector<std::string> words;
	words.push_back(str1);
	words.push_back(str2);
	words.push_back(str3);

	new GT_cls(2);
	GT_cls(3);
	//这块的理解有困难,传入的是一个初始化对象的过程,怎么用取决于count_if()的实现,
	//我传入的应该是一个刚刚初始化的对象,他调用的确实对象函数加string形参
	//那就是说传入的是个函数,怎么填参数不关心,(通过指针之类的保存这个函数位置)
	//不管传入的式子怎样,count_if()就记住这个形式了,认为他是个函数,每次调用GT_cls( str.);
	std::cout << std::count_if(words.begin(), words.end(), GT_cls(3))
			<< " words 3 characters or longer" << std::endl;
	std::cout << std::count_if(words.begin(), words.end(), GT_cls(4))
			<< " words 4 characters or longer" << std::endl;
	std::cout << std::count_if(words.begin(), words.end(), GT_cls(5))
			<< " words 5 characters or longer" << std::endl;
	//更有用的是,输入变量,完成批量任务
	for(size_t i = 1; i != 11; ++i)
		std::cout << count_if(words.begin(), words.end(), GT_cls(i))
				<< " words " << i << " characters or longer" << std::endl;
	//还可以计算大于3小于6的
	std::cout <<
		count_if(words.begin(), words.end(), GT_cls(3)) - count_if(words.begin(), words.end(), GT_cls(6)) 
		<< std::endl;
}

pe14_33
//利用标准库算法和GT_cls类,查找第一个比指定值大的元素
	std::cout << *(find_if(words.begin(), words.end(), GT_cls(2))) << std::endl;
	std::cout << *(find_if(words.begin(), words.end(), GT_cls(4))) << std::endl;
	std::cout << *(find_if(words.begin(), words.end(), GT_cls(5))) << std::endl;
}


pe14_34


//判断是否相等,利用标准库算法,替换指定值,就是如果finde_if(),替换掉
#include"head.h"
class word_equal{
public:
	word_equal(const std::string str = ""): word(str){}//构造函数接收一个边界值,有默认实参0	
	bool operator()(const std::string &s){//对输入的string对象进行判断
		return s == word;
	}
private:
	std::string word;
	
};


int main(){
	std::string str1("he");
	std::string str2("hell");
	std::string str3("world");
	
	std::vector<std::string> words;
	words.push_back(str1);
	words.push_back(str2);
	words.push_back(str3);
	words.push_back(str1);
	
	for(std::vector<std::string>::iterator iter = words.begin(); iter != words.end(); iter++)
		std::cout << *iter << "\t";
	
	//进行替换
	std::string search("he");
	while(std::count_if(words.begin(), words.end(), word_equal(search))){//利用count_if()非零来判断是否存在相应对象
		 *(std::find_if(words.begin(), words.end(), word_equal(search))) = "HOLY GOD~!";//利用find_if()逐个更改
	}
	std::cout << std::endl << "After change: " << std::endl;
	
	for(std::vector<std::string>::iterator iter = words.begin(); iter != words.end(); iter++)
		std::cout << *iter << "\t";
	
}

pe14_35


//用对象替代GT6
//要求读不太懂,测试给定string对象的长度是否与其边界相匹配(好像是废话,就找1到10之间的就行了)
//报告输入中有多少单词的长度在1和10之间
//按原程序算,这是统计是删除了重复的以后了
#include"head.h"
using namespace std;


class GT_cls{
public:
	GT_cls(size_t val = 0): bound(val){}//构造函数接收一个边界值,有默认实参0	
	bool operator()(const std::string &s){//对输入的string对象进行判断
		return s.size() >= bound;
	}
private:
	std::string::size_type bound;
	
};

bool isShorter(const string &s1, const string &s2){
	return s1.size() < s2.size();
}
static string make_plural(vector<string>::size_type sz, string word, string append){
	return (sz <= 1) ? word : word + append;
}
int main(){
	vector<string> words;
	string next_word;
	while(cin >> next_word){
		words.push_back(next_word);
	}
	sort(words.begin(), words.end());//排序
	
	vector<string>::iterator end_unique = unique(words.begin(), words.end());//unique负责排序,并返回相应迭代器
	words.erase(end_unique, words.end());//删除最后几个重复的
	
	stable_sort(words.begin(), words.end(), isShorter);//自定义函数的介入
	vector<string>::size_type wc = count_if(words.begin(), words.end(), GT_cls(10));
	cout << wc << " " << make_plural(wc, "word", "s") << " 10 characters or longer" << endl;
	//本题要的结果
	std::cout << 
		count_if(words.begin(), words.end(), GT_cls(1)) - count_if(words.begin(), words.end(), GT_cls(10)) 
		<< " words GT1 but not GT10 " << std::endl;
	
	
	return 0;
}


14.36,太简单,输出长度1到9之间以及10以上的单词的数目

你可能感兴趣的:(调用操作符和函数对象)