编程练习
-
下面是一个简短程序的一部分:
int main() { using namespace std; // list of double deduced from list contents auto q = average_list ({15.4, 10.7, 9.0}); cout << q << endl; // list of int deduced from list contents cout << average_list({20, 30, 19, 17, 45, 38} ) << endl; // forced list of double auto ad = average_list<double>({'A', 70, 65.33 }); cout << ad << endl; return 0; }
请提供函数 average_list(),让该程序变得完整。它应该是一个模板函数,其中的类型参数指定了用作函数参数的 initialize_list 模板的类型以及函数的返回类型。
答:#include<iostream> #include<initializer_list> template<typename T> T average_list(std::initializer_list<T> List){ T t; for(auto iter = List.begin(); iter != List.end(); iter++ ){ t += *iter; } t = t / List.size(); return t; } int main() { using namespace std; // list of double deduced from list contents auto q = average_list ({15.4, 10.7, 9.0}); cout << q << endl; // list of int deduced from list contents cout << average_list({20, 30, 19, 17, 45, 38} ) << endl; // forced list of double auto ad = average_list<double>({'A', 70, 65.33 }); cout << ad << endl; return 0; }
-
下面是类 Cpmv 的声明:
class Cpmv { public: struct Info { std::string qcode; std::string zcode; }; private: Info *pi; public: Cpmv(); Cpmv(std::string q, std::string z); Cpmv(const Cpmv & cp); Cpmv(Cpmv && mv); ~Cpmv(); Cpmv & operator=(const Cpmv & cp); Cpmv & operator=(Cpmv && mv); Cpmv operator+(const Cpmv & obj) const; void Display() const; };
函数 operator+() 应创建一个对象,其成员 qcode 和 zcode 有操作数的相应成员拼接而成。请提供为移动构造函数和移动复制运算符实现移动语义的代码。编写一个使用所有这些方法的程序。为方便测试,让各个方法都显示特定的内容,以便知道它们被调用。
#include<string> #include<iostream> class Cpmv { public: struct Info { std::string qcode; std::string zcode; }; private: Info *pi; public: Cpmv(); Cpmv(std::string q, std::string z); Cpmv(const Cpmv & cp); Cpmv(Cpmv && mv); ~Cpmv(); Cpmv & operator=(const Cpmv & cp); Cpmv & operator=(Cpmv && mv); Cpmv operator+(const Cpmv & obj) const; void Display() const; }; Cpmv::Cpmv() { std::cout << "default constructer" << std::endl; pi = new Info; pi->qcode = "null"; pi->zcode = "null"; } Cpmv::Cpmv(std::string q, std::string z){ std::cout << "constructer" << std::endl; pi = new Info; pi->qcode = q; pi->zcode = z; } Cpmv::Cpmv(const Cpmv & cp){ std::cout << "copy constructer" << std::endl; pi = new Info(*cp.pi); } Cpmv::Cpmv(Cpmv && mv){ std::cout << "move constructor" << std::endl; pi = mv.pi; mv.pi = nullptr; } Cpmv::~Cpmv() { std::cout << "destructor" << std::endl; delete pi; } Cpmv & Cpmv::operator=(const Cpmv & cp) { std::cout << "assigment" << std::endl; if(this == &cp){ return *this; } delete pi; pi = new Info(*cp.pi); return *this; } Cpmv & Cpmv::operator=(Cpmv && mv) { std::cout << "move assignment" << std::endl; delete pi; pi = mv.pi; mv.pi = nullptr; return *this; } Cpmv Cpmv::operator+(const Cpmv & obj) const { std::cout << "operator+" << std::endl; Cpmv c; c.pi->qcode = pi->qcode + obj.pi->qcode; c.pi->zcode = pi->zcode + obj.pi->zcode; return c; } void Cpmv::Display() const { std::cout << pi->qcode << " " << pi->zcode << std::endl; } int main(){ { Cpmv c1; c1.Display(); Cpmv c2("c2c2c2", "2c2c2c"); c2.Display(); Cpmv c3("c3c3c3", "c3c3c3"); c3.Display(); Cpmv c4(c3); c4.Display(); Cpmv c5(c2+c3); c5.Display(); c1 = c5; c1.Display(); c1 = c5+c5; c1.Display(); } return 0; }
-
编写并测试可变参数模板函数 sum_value(),它接受任意长度的参数列表(其中包含数值,但可以是任何类型),并以 long double 的方式返回这些数值的和。
#include <ios> #include<iostream> template<typename T> long double sum_value(const T & t){ return t; } template<typename T, typename... Args> long double sum_value(const T & t, const Args&... args){ return t + sum_value(args...); } int main() { std::cout << sum_value(1,2,3,4,5) << std::endl; std::cout << sum_value(1,2,3,4,5,6,7) << std::endl;; std::cout.precision(5); std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); std::cout << sum_value(1,2,3,4,5,6,7,8,9) << std::endl; return 0; }
-
使用 lambda 重新编写程序清单 16.15。具体地说,使用一个有名称的 lambda 替换函数 outint(),并将函数符替换为两个匿名 lambda 表达式。
// functor.cpp -- using a functor #include <iostream> #include <list> #include <iterator> #include <algorithm> // template<class T> // functor class defines operator()() // class TooBig{ // private: // T cutoff; // public: // TooBig(const T & t) : cutoff(t) {} // bool operator()(const T & v) { return v > cutoff; } // }; //void outint(int n) { std::cout << n << " ";} auto outint = [](int n) { std::cout << n << " ";}; int main(){ using std::list; using std::cout; using std::endl; //TooBig<int> f100(100); // limit = 100 int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; list<int> yadayada(vals, vals+10); // range constructor list<int> etcetera(vals, vals+10); // C++11 cna use the following instead // list<int> yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; // list<int> etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201}; cout << "Original lists:\n"; for_each(yadayada.begin(), yadayada.end(), outint); cout << endl; std::for_each(etcetera.begin(), etcetera.end(), outint); cout << endl; yadayada.remove_if([](int n){return n > 100;}); etcetera.remove_if([](int n){return n > 200;}); cout << "Trimmed lists:\n"; for_each(yadayada.begin(), yadayada.end(), outint); cout << endl; for_each(etcetera.begin(), etcetera.end(), outint); cout << endl; return 0; }