c/c++语言开发共享C++primer第14章习题解答

  练习14.1:在什么情况下重载的运算符与内置运算符有所区别?在什么情况下重载的运算符又与内置运算符一样? 不同点: 重载运算符必须具有至少一个class或枚举类型的操作数。 重载运算符

 

练习14.1:在什么情况下重载的运算符与内置运算符有所区别?在什么情况下重载的运算符又与内置运算符一样?

不同点:

重载运算符必须具有至少一个class或枚举类型的操作数。

重载运算符不保证操作数的求值顺序,例如对&&和||的重载版本不再具有“短路求值”的特性,两个操作数都要求值,而且不规定操作数的求值顺序。

相同点:

对于优先级和结合性级操作数的数目都不变。

 

练习14.2:为sales_data编写重载的输入、输出、加法和复合赋值运算符的声明。

 

  class sales_data  {  friend std::istream& operator>>(std::istream&, sales_data &);  friend std::ostream& operator<<(std::ostream&, const sales_data&);    public:  	sales_data& operator+=(const sales_data&);  };    sales_data operator+(const sales_data&, const sales_data&);

 

练习14.3:string和vector都定义了重载的==以比较各自的对象,假设svec1和svec2是存放string的vector,确定在下面的表达式中分别使用了哪个版本的==?

 

(a) "cobble" == "stone" (b) svec1[0] ==svec2[0] (c)svec1 ==svec2 (d) svec[0] == "stone"

(a)应用了c++内置版本的==,比较两个指针。(b) 应用了string版本的==。(c)应用了vector版本的==。(d)应用了string版本的==。

 

练习14.4:如何确定下列运算符是否应该是类的成员?

(a) % (b) %= (c) ++ (d) -> (e) << (f) && (g) == (h) ()

(a) %通常定义为非成员。

(b) %=通常定义为类成员,因为它会改变对象的状态。

(c) ++通常定义为类成员,因为它会改变对象的状态。

(d) ->必须定义为类成员,否则编译报错

(e) <<通常定义为非成员

(f) && 通常定义为非成员。

(g) ==通常定义为非成员。

(h) ()必须定义为类成员,否则编译会报错。

 

练习14.5:在7.5.1节的练习7.40中,编写了下列类中的某一个框架,请问在这个类中应该定义重载的运算符吗?如果是,请写出来。

(a)book (b)date (c)employee (d)vehicle (e)object (f)tree

 

  #include   using std::ostream;  using std::endl;    class date  {  public:  	date() { }  	date(int y, int m, int d) {year = y; month = m; day = d;}  	friend ostream& operator<<(ostream &os, const date &dt);    private:  	int year, month, day;  };    ostream& operator<<(ostream& os, const date& d)  {  	const char sep = 't';  	os << "year:" << d.year << sep << "month:" << d.month << sep << "day:" << d.day << endl;  	return os;  }

 

练习14.6:为你的sales_data类定义输出运算符。

 

  class sales_data  {  friend ostream& operator<<(ostream &os, const sales_data &item);  //其他成员  };    ostream& operator<<(ostream &os, const sales_data &item)  {  	const char *sep = ' ';  	os << item.isbn() << sep << item.units_sold << sep << item.revenue <

 

练习14.7:你在13.5节的练习中曾经编写了一个string类,为它定义一个输出运算符。

 

  class string  {  public:  	string();  	string(const char *str);  	friend ostream& operator<<(ostream &os, const string &str);  private:  	char *str;  };    ostream& operator<<(ostream &os, const string &str)  {  	cout << str;  	return os;  }

 

练习14.8:你在7.51节的练习7.40中曾经选择并编写了一个类,为它定义一个输出运算符。

 

见练习14.5。

 

练习14.10:对于sales_data的输入运算符来说如果给定了下面的输入将发生什么情况?

(a)0-201-99999-9 10 24.95 (b) 10 24.95 0-210-99999-9

(a)参数中传入的sales_data对象将会得到输入的值,其中bookno、units_sold、price的值分别是0-201-99999-9、10、24.95,同时revenue的值是249.5.

(b)输入错误,参数中传入的sales_data对象将会得到默认值。

 

练习14.11:下面的sales_data输入运算符存在错误吗?如果有,请指出来。对于这个输入运算符如果仍然给定上一个练习的输入将发生什么情况?

 

  istream& operator>>(istream& in, sales_data& s)  {  	double price;  	in >> s.bookno >> s.unite_sold >> price;  	s.revenue = s.unite_sold * price;  	return in;  }

 

这个实现没有判断输入数据的正确性,是错误的。

 

(a)如果输入的是0-201-99999-9 10 24.95,程序不会报错,sales_data能得到正确的值。

(b)如果输入的是10 24.95 0-201-99999-9,sales_data会得到错误的值。

 

练习14.12:你在7.5.1节的练习中曾经选择并编写了一个类,为它定义一个输入运算符并确保该运算符可以处理输入错误。

 

  
  #include  using std::istream; class date { public: date() { } date(int y, int m, int d) {year = y; month = m; day = d;} friend istream& operator>>(istream &is, date &dt); private: int year, month, day; }; istream& operator>>(istream &is, date &dt) { is >> dt.year >> dt.month >> dt.day; if (!is) dt = date(0, 0, 0); return is; }
  

练习14.13:你认为sales_data类还应该支持 哪些其他算术运算符?如果有的话,请给出它们的定义。

 

可以定义一个减法运算符

 

  class sales_data  {  	friend sales_data operator-(const sales_data &lhs, const sales_data &rhs);  public:  	sales_data& operator-=(const sales_data &rhs);  	//其他成员  };  sales_data operator-(const sales_data &lhs, const sales_data &rhs)  {  	sales_data sub = lhs;  	sub -= rhs;  	return sub;  }  sales_data& sales_data::operator-=(const sales_data &rhs)  {  	units_sold -= rhs.units_sold;  	revenue -= rhs.revenue;  	return *this;  }

 

练习14.14:你觉得为什么调用operator+=来定义operator+比其他方法要更有效?

 

从头实现operator+的方式与借助operator+=实现的方式相比,在性能上没有优势,而可读性上后者显然更好。

 

练习14.15:你在7.5.1节的练习7.40中曾经选择并编写了一个类。你认为它应该含有其他算术运算符吗?如果是,请实现它们;如果不是,解释原因。

在练习7.40中,变写了date类,算术运算对date没有太大意义,不需要为date重载算术运算符。

练习14.16:为你的strblob类、strblobptr类、strvec类和string类分别定义相等和不相等运算符。

 

  //strblob  class strblob  {  	friend bool operator==(const strblob &lhs, const strblob &rhs);  	friend bool operator!=(const strblob &lhs, const strblob &rhs);  	//其他成员  };  bool operator==(const strblob &lhs, const strblob &rhs)  {  	return lhs.data ==rhs.data;  }  bool operator!=(const strblob &lhs, const strblob &rhs)  {  	return !(lhs == rhs);  }    //strblobptr  class strblobptr  {  	friend bool operator==(const strblobptr &lhs, const strblobptr &rhs);  	friend bool operator!=(const strblobptr &lhs, const strblobptr &rhs);  	//其他成员  };  bool operator==(const strblobptr &lhs, const strblobptr &rhs)  {  	auto l = lhs.wptr.lock(), r = rhs.wptr.loc();  	if (l == r)  		return (!r || lhs.curr == rhs.curr);  	else  		return false;  }  bool operator!=(const strblobptr &lhs, const strblobptr &rhs)  {  	return !(lhs == rhs);  }    //strvec  class strvec  {  	friend bool operator==(const strvec &lhs, const strvec &rhs);  	friend bool operator!=(const strvec &lhs, const strvec &rhs);  	//其他成员  };  bool operator==(const strvec &lhs, const strvec &rhs)  {  	if (lhs.size() == rhs.size())  		return false;  	for (auto itr1 = lhs.begin(), itr2 = rhs.begin(); itr1 != lhs.end() && itr2 != rhs.end(); ++itr1, ++itr2)  	{  		if (*itr1 != *itr2)  			return false;  	}  	return true;  }  bool operator!=(const strvec &lhs, const strvec &rhs)  {  	return !(lhs == rhs);  }    //string  class string  {  	friend bool operator==(const string &lhs, const string &rhs);  	friend bool operator!=(const string &lhs, const string &rhs);  	//其他成员  private:  	const char *str;  };  bool operator==(const string &lhs, const string &rhs)  {  	return strcmp(lhs.str, rhs.str);  }  bool operator!=(const string &lhs, const string &rhs)  {  	return !(lhs == rhs);  }

 

练习14.17:你在7.5.1节的练习7.40中曾经选择并编写了一个类,你认为它应该含有相等运算符吗?如果是,请实现它;如果不是,解释原因。

 

  class date  {  	friend bool operator==(const date &dl, const date &d2);  	friend bool operator!=(const date &d1, const date &d2);  	//其他成员  };  bool operator==(const date &d1, const date &d2)  {  	return d1.year == d2.year && d1.month == d2.month && d1.day == d2.day;  }  bool operator!=(const date &d1, const date &d2)  {  	return !(da == d2);  }

 

14.18:为你的strblob类、strblobptr类、strvec类、string类定义关系运算符。

 

  class string  {  	friend bool operator<(const string &s1, const string &s2);  	friend bool operator<=(const string &s1, const string &s2);  	friend bool operator>(const string &s1, const string &s2);  	friend bool operator>=(const string &s1, const string &s2);  	//其他成员  };    friend bool operator<(const string &s1, const string &s2)  {  	return strcmp(s1.str, s2.str) < 0;  }  friend bool operator<=(const string &s1, const string &s2)  {  	return strcmp(s1.str, s2.str) <= 0;  }  friend bool operator>(const string &s1, const string &s2)  {  	return strcmp(s1.str, s2.str) > 0;  }  friend bool operator>=(const string &s1, const string &s2)  {  	return strcmp(s1.str, s2.str) >= 0;  }    class strblob  {  	friend bool operator<(const strblob &s1, const strblob &s2);  	friend bool operator<=(const strblob &s1, const strblob &s2);  	friend bool operator>(const strblob &s1, const strblob &s2);  	friend bool operator>=(const strblob &s1, const strblob &s2);  };  bool operator<(const strblob &s1, const strblob &s2)  {  	return *s1.data < *s2.data;  }  bool operator<=(const strblob &s1, const strblob &s2)  {  	return *s1.data <= *s2.data;  }  bool operator>(const strblob &s1, const strblob &s2)  {  	return *s1.data > *s2.data;  }  bool operator>=(const strblob &s1, const strblob &s2)  {  	return *s1.data >= *s2.data;  }    class strblobptr  {  	friend operator<(const strblobptr &s1, const strblobptr &s2);  	friend operator<=(const strblobptr &s1, const strblobptr &s2);  	friend operator>(const strblobptr &s1, const strblobptr &s2);  	friend operator>=(const strblobptr &s1, const strblobptr &s2);  };  bool operator<(const strblobptr &s1, const strblobptr &s2)  {  	auto l = s1.wptr.lock(), r = s2.wptr.lock();  	if (l == r)  	{  		if (!r)  			return false;  		return (s1.curr < s2.curr);  	}  	else  		return false;  }  bool operator<=(const strblobptr &s1, const strblobptr &s2)  {  	auto l = s1.wptr.lock(), r = s2.wptr.lock();  	if (l == r)  		return (!r || s1.curr <= s2.curr);  	else  		return false;  }  bool operator>(const strblobptr &s1, const strblobptr &s2)  {  	auto l = s1.wptr.lock(), r = s2.wptr.lock();  	if (l == r)  	{  		if (!r)  			return false;  		return (s1.curr > s2.curr);  	}  	else  		return false;  }  bool operator>=(const strblobptr &s1, const strblobptr &s2)  {  	auto l = s1.wptr.lock(), r = s2.wptr.lock();  	if (l == r)  		return (!r || s1.curr >= s2.curr);  	else  		return false;  }    class strvec  {  	friend operator<(const strvec &s1, const strvec &s2);  	friend operator<=(const strvec &s1, const strvec &s2);  	friend operator>(const strvec &s1, const strvec &s2);  	friend operator>=(const strvec &s1, const strvec &s2);  	//其他成员  };  bool operator<(const strvec &s1, const strvec &s2)  {  	for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)  	{  		if (*p1 < *p2)  			return true;  		else if (*p1 > *p2)  			return false;  	}  	if (p1 == s1.end() && p2 != s2.end())  		return true;  	return false;  }  bool operator<=(const strvec &s1, const strvec &s2)  {  	for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)  	{  		if (*p1 < *p2)  			return true;  		else if (*p1 > *p2)  			return false;  	}  	if (p1 == s1.end())  		return true;  	return false;  }  bool operator>(const strvec &s1, const strvec &s2)  {  	for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)  	{  		if (*p1 < *p2)  			return false;  		else if (*p1 > *p2)  			return true;  	}  	if (p1 == s1.end() && p2 != s2.end())  		return true;  	return false;  }  bool operator>=(const strvec &s1, const strvec &s2)  {  	for (auto p1 = s1.begin(), p2 = s2.begin(); p1 != s1.end(), p2 != s2.end(); ++p1, ++p2)  	{  		if (*p1 < *p2)  			return false;  		else if (*p1 > *p2)  			return true;  	}  	if (p2 == s2.end())  		return true;  	return false;  }

 

练习14.19:你在7.5.1节的练习7.40中曾经选择并编写了一个类,你认为它应该含有关系运算符吗?如果是,请实现它;如果不是,解释原因。

 

  class date  {  	friend operator<(const date &d1, const date &d2);  	friend operator<=(const date &d1, const date &d2);  	friend operator>(const date &d1, const date &d2);  	friend operator>=(const date &d1, const date &d2);  	//其他成员  };    bool operator<(const date &d1, const date &s2)  {  	return (d1.year < d2.year) || (d1.year == d2. year && d1.month < d2.month) || (d1.year == d2.year && d1.month == d2.month && d1.day < d2.day);  }  bool operator<=(const date &d1, const date &s2)  {  	return (d1 < d2) || (d1 == d2);  }  bool operator>(const date &d1, const date &s2)  {  	return !(d1 <= d2);  }  bool operator>=(const date &d1, const date &s2)  {  	return (d1 > d2) || (d1 == d2);  }

 

练习14.20:为你的sales_data类实现加法和复合赋值运算符。

 

  class sales_data  {  	friend sales_data operator+(const sales_data &lhs, const sales_data &rhs);  public:  	sales_data& operator+=(const sales_data &rhs);  	//其他成员  };  sales_data operator+(const sales_data &lhs, const sales_data &rhs)  {  	sales_data sum = lhs;  	sum += rhs;  	return sum;  }  sales_data& sales_data::operator+=(const sales_data &rhs)  {  	units_sold += rhs.units_sold;  	revenue += rhs.revenue;  	return *this;  }

 

练习14.21:编写sales_data类的+和+=运算符,是的+执行实际的加法操作,而+=调用+。相比于14.3节和14.4节对这两个运算符的定义,本题的定义有何缺点?试讨论之。

 

在性能上没有优势,可读性也不好。

 

  class sales_data  {  	friend sales_data operator+(const sales_data &lhs, const sales_data &rhs);  public:  	sales_data& operator+=(const sales_data &rhs);  	//其他成员  };  sales_data operator+(const sales_data &lhs, const sales_data &rhs)  {  	sales_data sum = lhs;  	units_sold += rhs.units_sold;  	revenue += rhs.revenue;  	return sum;  }  }  sales_data& sales_data::operator+=(const sales_data &rhs)  {  	*this = (*this) + rhs;  }

 

练习14.22:定义赋值运算符的一个新版本,使得我们能把一个表示isbn的string赋给一个sales_data对象。

 

  class sales_data  {  public:  	sales_data& operator=(const string &isbn);  	//其他成员  };  sales_data& sales_data::operator=(const string &isbn)  {  	bookno = isbn;  	return *this;  )

 

练习14.23:为你的strvec类定义一个initializer_list赋值运算符。

 

  class strvec  {  public:  	strvec& operator=(std::initializer_list il);  	//其他成员  };  strvec& strvec::operator=(std::initializer_list il)  {  	auto data = alloc_n_copy(il.begin(), il.end());  	free();  	elements = data.first;  	first_free = cap = data.second;  	return *this;  }

 

练习14.24:你在7.5.1节的脸7.40中曾经选择并编写了一个类,你认为它应该含有拷贝赋值和移动赋值运算符吗?如果是,请实现它们。

 

再联系7.40中,我们变细了date类,它只有三个int类型的数据成员,浅拷贝就能满足要求,因此不需要另外定义拷贝赋值和移动赋值运算符。

 

练习14.25:上题的这个类还需要定义其他赋值运算符吗?如果是,请实现它们;同时说明运算对象应该是什么类型并解释原因。

 

  class date  {  public:  	date& operator=(const string &date);  	//其他成员  };  date& sales_data::operator=(const string &date)  {  	istringstream in(date);  	char ch1, cha2;  	in >> year >> ch1 >> month >> ch2 >> day;  	if (!in || ch1 != '-' || ch2 != '-')  		throw std::invalid_argument("bad date");  	if (month < 1 || month >12 || day < 1 || day > 31)  		throw std::invalid_argument("bad date");  	return *this;  }

 

练习14.26:为你的strblob类、strblobptr类,strvec类和string类定义下标运算符。

 

  class strblob  {  public:  	std:;string& operator[](std:;size_t n) { return data[n]; }  	const std:;string& operator[](std:;size_t n) const { return data[n]; }  };  class strblobptr  {  	std::string& operator[](std::size_t n) { return (*wptr.lock())[n]; }  	const std::string& operator[](std::size_t n) const { return (*wptr.lock())[n]; }  };  class strvec  {  public:  	std:;string& operator[])(std:;size_t n) { return elements[n]; }  	const std:;string& operator[])(std:;size_t n) const { return elements[n]; }  };  class string  {  public:  	char& operator[](std::size_t n) { return (char) str[n]; }  	const char& operator[](std::size_t n) const { return (char) str[n]; }  private:  	char *str;  }

 

练习14.27:为你的strblobptr类添加递增和递减运算符。

 

  class strblobptr  {  public:  	//前缀  	strblobptr& operator++();  	strblobptr& operator--();  	//后缀  	strblobptr operator++(int);  	strblobptr operator--(int);  };  strblobptr& strblobptr::operator++()  {  	check(curr, " increment past end of strblobptr ");  	++curr;  	return *this;  }  strblobptr& strblobptr::operator--()  {  	--curr;  	check(-1, " decrement past begin of strblobptr ");	  	return *this;  }  strblobptr strblobptr::operator++(int)  {  	strblobptr ret = *this;  	++*this;  	return ret;  }  strblobptr strblobptr::operator--(int)  {  	strblobptr ret = *this;  	--*this;  	return ret;  }

 

练习14.28:为你的strblobptr类添加加法和减法运算符,使其可以实现指针的算术运算。

 

  class strblobptr  {  	friend strblobptr operator+(int n);  	friend strblobptr operator-(int n);  	//其他成员  };  strblobptr strblobptr::operator+(int n)  {  	auto ret = *this;  	ret.curr += n;  	return ret;  }  strblobptr strblobptr::operator-(int n)  {  	auto ret = *this;  	ret.curr -= n;  	return ret;  }

 

练习14.29:为什么不定义const版本的递增和递减运算符?

 

对于++和–运算符,无论他是前缀版本还是后缀版本,都会改变对象本身的值,因此不能定义成const的。

 

练习14.30:为你的strblob类和在12.1.6节练习12.22中定义的conststrblobptr类分别添加解引用运算符和箭头运算符。注意:因为conststrblobptr的数据成员指向const vector,所以conststrblobptr中的运算符必须返回常量引用。

 

  class strblobptr  {  public:  	std::string& operator*() const  	{  		auto p = check(curr, "dereference past end");  		return (*p)[curr];  	}   	std::string* operator->() const  	{  		return &(this->operator*());  	}  };  class conststrblobptr  {  public:  	const std::string& operator*() const  	{  		auto p = check(curr, "dereference past end");  		return (*p)[curr];  	}   	const std::string* operator->() const  	{  		return &(this->operator*());  	}  };

 

练习14.31:我们的strblobptr类没有定义拷贝构造函数、赋值运算符和析构函数,为什么?

 

对于strblobptr类,它的数据成员有两个,分别是weak_ptr<>>和size_t类型的,前者定义了自己的拷贝构造函数、赋值运算符和析构函数,后者是内置类型,因此默认的拷贝语义即可,无须为strblobptr定义拷贝构造函数、赋值运算符和析构函数。

 

练习14.32:定义一个类令其含有指向strblobptr对象的指针,为这个类定义重载的建投运算符。

 

  class myclass  {  public:  	std::string* operator->() const  	{  		return ptr->operator->();  	}  private:  	strblobptr *ptr;  }

 

练习14.33:一个重载的函数调用运算符应该接受几个运算对象?

 

0个或多个。

 

练习14.34:定义一个函数对象类,伶气质型if-then-else的操作:该类的调用运算符接受三个形参,它首先检查第一个形参,如果成功返回第二个形参的值,如果不成功返回第三个形参的值。

 

  class ifelsethen  {  public:  	ifelsethen() { }  	ifelsethen(int i1, int i2, int i3) : ival1(i1), ival2(i2), ival3(i3) { }  	int operator()(int i1, int i2, int i3)  	{  		return i1 ? i2 : i3;  	}  private:  	int ival1, ival2, ival3;  };

 

练习14.35:编写一个类似printstring的类,令其从istream中读取一行输入,然后返回一个表示我们所读内容的string。如果读取失败,返回空string。

 

  class readstring  {  public:  	readstring(istream &is = cin) : is(is) { }  	std:;string operator()()  	{  		string line;  		if (!getline(is, line))  		{  			line = " ";  		}  		return line;  	}  private:  	istream &is;  };

 

练习14.36:使用前一个练习定义的类读取标准输入,将每一行保存为vector的一个元素。

 

  void testreadstring()  {  	readstring rs;  	vector vec;  	while (true)  	{  		string line = rs();  		if (!line.empty())  		{  			vec.push_back(line);  		}  		else  			break;  	}  }

 

练习14.37:编写一个类令其检查两个值是否相等。使用该对象及标准库算法编写程序,令其替换某个序列中具有给定值的所有实例。

 

  class intcompare  {  public:  	intcompare(int v) : val(v) { }  	bool operator()(int v) { return val ==v; }  private:  	int val;  };    int main()  {  	vector vec = {1, 2, 3, 2, 1};  	const int oldvalue = 2;  	const int newvalue = 200;  	intcompare icmp(oldvalue);  	std::replace_if(vec.begin(), vec.end(), icmp, newvalue);    	return 0;  }

 

练习14.38:编写一个类令其检查某个给定的string对象的长度是否与一个阀值相等。使用该对象编写程序,统计并报告在输入的文件中长度为1的单词有多少个、长度为2的单词有多少个。

 

  #include   #include   #include   #include   using std::istream;  using std::cout;  using std::cin;  using std::endl;  using std::vector;  using std::string;    class strlenis  {  public:  	strlenis(int len) : len(len) { }  	bool operator()(const string &str) { return str.length() == len; }    private:  	int len;  };    void readstr(istream &is, vector &vec)  {  	string word;  	while (is >> word)  	{  		vec.push_back(word);  	}  }    int main()  {  	vector vec;  	readstr(cin, vec);  	const int minlen = 1;  	const int maxlen = 10;  	for (int i = minlen; i <= maxlen; ++i)  	{  		strlenis slenis(i);  		cout << "len: " << i << ", cnt: " << count_if(vec.begin(), vec.end(), slenis) << endl;  	}    	return 0;  }

 

练习14.39:修改上一题的程序令其报告长度在1至9之间的单词有多少个、长度在10以上的单词又有多少个。

 

  #include   #include   #include   #include   using std::istream;  using std::cout;  using std::cin;  using std::endl;  using std::vector;  using std::string;    class strlenbetween  {  public:  	strlenbetween(int minlen, int maxlen) : minlen(minlen), maxlen(maxlen) { }  	bool operator()(const string &str) { return str.length() >= minlen && str.length() <= maxlen; }    private:  	int minlen, maxlen;  };    class strnoshorterthan  {  public:  	strnoshorterthan(int len) : minlen(len) { }  	bool operator()(const string &str) { return str.length() >= minlen; }  private:  	int minlen;  };  void readstr(istream &is, vector &vec)  {  	string word;  	while (is >> word)  	{  		vec.push_back(word);  	}  }    int main()  {  	vector vec;  	readstr(cin, vec);  	strlenbetween slenbetween(1, 9);  	strnoshorterthan snoshorterthan(10);  	cout << "len 1-9 :" << count_if(vec.begin(), vec.end(), slenbetween) << endl;  	cout << "len >= 10 : " << count_if(vec.begin(), vec.end(), snoshorterthan) << endl;    	return 0;  }    

练习14.40:重新编写10.3.2节的biggies函数,使用函数对象替换其中的lambda表达式。

 

  class isshorter  {  public:  	bool operator()(const string &s1, const string &s2)  	{  		return s1.size() < s2.size();  	}  };  class notshorterthan  {  public:  	notshorterthan(int len) : minlen(len) { }  	bool operator()(const string &str)  	{  		return str.size() >= minlen;  	}  private:  	int minlen;  };  class printstring  {  public:  	void operator()(const string &str)  	{  		cout << str << " ";  	}  };    void biggies(vector &words, vector::size_type sz)  {  	elimdups(words);  	isshorter is;  	stable_sort(words.begin(), words.end(), is);  	notshorterthan nst(sz);  	auto wc = find_if(words.begin(), words.end(), nst);  	auto count = words.end() - wc;  	cout << count << " " << make_plural(count, "words", "s") << " of length " <, sz << " or longer" <

 

练习14.41:你认为c++11新标准为什么要增加lambda?对于你自己来说,什么情况下会使用lambda,什么情况下会使用类?

 

在c++11中,lambda是通过匿名的函数对象来实现的,因此我们可以把lambda看作是对函数对象在使用方式上进行的简化。当代码需要一个简单的函数,并且这个函数并不会在其他地方被使用时,就可以使用lambda来实现,此时它所起的作用类似于匿名函数。但如果这个函数需要多次使用,并且它需要保存某些状态的话,使用函数对象更合适一些。

 

练习14.42:使用标准库函数对象及适配器定义一条表达式,令其

(a)统计大于1024的值有多少个。

(b)找到第一个不等于pooh的字符串。

(c)将所有的值乘以2.

 

count_if(vec.begin(), vec.end(), bind2nd(greater(), 1024));  find_if(vec.begin(), vec.end(), bind2nd(not_equal_to(), "pooh"));  transform(vec.begin(), vec.end(), vec.begin(), bind2nd(multiplies(), 2));

 

练习14.43:使用标准库函数对象判断一个给定的int值是否能被int容器中的所有元素整除。

 

  bool pidebyall(vector &ivec, int pidend)  {  	return count_if(ivec.begin(), ivec.end(), bindlst(modulus, pidend)) == 0;  }

 

练习14.44:编写一个简单的桌面计算器使其能处理二元运算。

 

  #include   #include  #include   #include   #include   using std::function;  using std::map;  using std::cin;  using std::cout;  using std::endl;  using std::string;  using std::plus;  using std::minus;  using std::multiplies;  using std::pides;  using std::modulus;  map> binops ={  	{"+", plus()},  	{"-", minus()},  	{"*", multiplies()},  	{"/", pides()},  	{"%", modulus()}  };    int main()  {  	int a, b;  	string op;  	cin >> a >> op >> b;  	cout<< binops[op](a, b) << endl;    	return 0;  }

 

练习14.45:编写类型转换运算符将一个sales_data对象分别转换成string和double,你认为这些运算符的返回值应该是什么?

 

如果要转换成string,那么返回值应该是bookno。

如果要转换成double,那么返回值应该是revenue。

 

练习14.46:你认为应该为sales_data类定义上面两种类型转换运算符吗?应该把它们声明成explicit的吗?为什么?

sales_data不应该定义这两种类型转换运算符,因为对于类来说,它包含三个数据成员:bookno,units_sold和revenue,只有三者在一起才是有效的数据。但是如果确实想要定义这两个类型转换运算符的话,应该把它们声明成explicit的,这样可以防止sales_data 在默写情况下被默认转换成string或double类型,这有可能导致意料之外的运算结果。

 

练习14.47:说明下面这两个类型转换运算符的区别。

 

  struct integral  {  	operator const int();  	operator int() const;  };

 

前者将对象转换成const int,在接受const int值的地方才能够使用。

 

后者将对象转换成int值,相对来说更加通用一些。

 

练习14.48:你在7.5.1节的练习7.40中曾经选择并编写了一个类,你认为它应该含有bool的类型转换运算符吗?如果是,解释原因并说明该运算符是否应该是explicit的;如果不是,也请解释原因。

之前我们编写了date类,它含有3个数据成员:year、month和day。

我们可以为date提供一个bool类型的转换运算符,用来检查3个数据成员是否都是有效值,bool类型转换运算符应该声明为explicit的,因为我们是有意要在条件表达式中使用它的。

 

练习14.49:为上一题提到的类定义一个转换目标是bool的类型转换运算符,先不用在意这么做是否应该。

 

  class date  {  	explicit operator bool()  	{  		vector> days_per_month = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};  		return 1 <= month && month <= 12 && 1 <= day && day <= days_per_month[isleapyear()? 1 : 0][month - 1];  	}    	bool isleapyear()  	{  		return (year % 4 ==0 && year % 100 != 0) || (year % 400 == 0);  	}  };

 

练习14.50:在初始化ex1和ex2的过程中,可能用到哪些类类型的转换序列呢?说明初始化是否正确并解释原因。

 

 

  struct longdouble {  	longdouble(double = 0.0);  	operator double();  	operator float();  };  longdouble ldobj;  int ex1 = ldobj;  float ex2 ldobj;

 

对于int ex1 = ldob;,它需要把longdouble类型转换成int类型,但是longdouble并没有定义对应的类型转换运算符,因此它会尝试使用其他的来进行转换。题中给出的两个都满足需求,但编译器无法确定那一个更合适,因此会产生二义性错误。

 

对于foloat ex2 = ldobj;,它需要把longdouble转换成float类型,而我们恰好定义了对应的类型转换运算符,因此直接调用operator float()即可。

 

练习14.51:在调用calc的过程中,可能用到哪些类型转换序列呢?说明最佳可行函数是如何选拔出来的。

void calc(int);

void calc(longdouble);

double dval;

calc(dval);

这里会优先调用void calc(int)函数。因为double转换为int是标准类型转换,而转换为longdouble则是转换为用户自定义类型,实际上调用了转换构造函数,因此前者优先。

 

练习14.52:在下面的加法表达式中分别选用了哪个operator?列出候选函数、可行函数及为每个可行函数的实参执行的类型转换。

 

  struct longdouble {  	//用于演示的成员operator+; 在通常情况下+s是个非成员  	longdouble operator+(const smallint&);  	//其他成员与14.9.2节一致  };  longdouble operator+(longdouble&, double);  smallint si;  longdouble ld;  ld = si + ld;  ld = ld + si;

 

对于ld=si+ld,由于longdouble不能转换为smallint,因此smallint的成员operator+和friend operator都不可行。

 

由于smallint不能转换为longdouble,longdouble的成员operator+和非成员operator+也都不可行。

由于smallint可以转换为int, longdouble了可以转换为float和double,所以内置的operator+(int, float)和operator+(int, double)都可行,会产生二义性。

对于ld=ld+si,类似上一个加法表达式,由于smallint不能转换为double,longdouble也不能转换为smallint,因此smallint的成员operator+和两个非成员operator+都不匹配。

longdouble的成员operator+可行,且为精确匹配。
smallint可以转换为int,longdouble可以转换为float和double,因此内置的operator+(float, int)和operator(double, int)都可行。但它们都需要类型转换,因此longdouble的成员operator+优先匹配。

 

练习14.53:假设我们已经定义了如第522页所示的smallint,判断下面的加法表达式是否合法。如果合法,使用了哪个加法运算符?如果不合法,应该怎样修改代码才能使其合法?

samllint sl;

double d = s1 + 3.14;

内置的operator+(int, double)是可行的,而3.14可以转换为int,然后再转换为smallint,所以smallint的成员operator+也是可行的。两者都需要进行类型转换,所以会产生二义性。改为:double d = s1 +smallint(3.14);即可。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/606676.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐