C++ Primer Plus第十八章练习

18.12.1

#include <iostream>
#include <initializer_list>
using namespace std;
template <typename T>
T average_list(initializer_list<T>);
int main(void)
{
    auto q = average_list({15.4, 10.7, 9.0});
    cout << q << endl;
    cout << average_list({20, 30, 19, 17, 45, 38}) << endl;
    auto ad = average_list<double>({'A', 70, 65.33});
    cout << ad << endl;
    return 0;
}
template <typename T>
T average_list(initializer_list<T> list)
{
    T sum = 0;
    for(auto x : list)
        sum += x;
    return sum;
}

18.12.2
没什么难度 test程序网上找了一份https://blog.csdn.net/leowinbow/article/details/86483861

#include <iostream>
#include <string>
using namespace std;
class Cpmv
{
public:
    struct Info
    {
        string qcode;
        string zcode;
    };
private:
    Info * pi;
public:
    Cpmv(void);
    Cpmv(string q, string z);
    Cpmv(const Cpmv & cp);
    Cpmv(Cpmv && mv);
    ~Cpmv(void);
    Cpmv & operator=(const Cpmv & cp);
    Cpmv & operator=(Cpmv && mv);
    Cpmv operator+(const Cpmv & obj) const;
    void Display(void) const;
};
int main(void)
{
    Cpmv temp;
    cout << "Object 0:\n";
    temp.Display();
 
    Cpmv temp1("number one", "number two");
    cout << "Object 1:\n";
    temp1.Display();
 
    Cpmv temp2(temp1);
    cout << "Object 2:\n";
    temp2.Display();
 
    cout << "Object 3 = Object 1:\n";
    Cpmv temp3;
    temp3 = temp1;
    cout << "Object 3:\n";
    temp3.Display();
    cout << "Object 1:\n";
    temp1.Display();
 
    cout << "Object 4 = move(Obejct 2):\n";
    Cpmv temp4;
    temp4 = Cpmv("xswl", "nssb");
    cout << "Object 4:\n";
    temp4.Display();
    cout << "Object 2:\n";
    temp2.Display();
 
    cout << "Obejct 5 = Object 3 + Obejct 4:\n";
    Cpmv temp5 = temp3 + temp4;
    cout << "Obejct 5:\n";
    temp5.Display();
    return 0;
}
Cpmv::Cpmv(void)
{
    pi = new Info;
}
Cpmv::Cpmv(string q, string z)
{
    pi = new Info;
    pi->qcode = q;
    pi->zcode = z;
}
Cpmv::Cpmv(const Cpmv & cp)
{
    pi = new Info;
    pi->qcode = cp.pi->qcode;
    pi->zcode = cp.pi->zcode;
}
Cpmv::Cpmv(Cpmv && mv)
{
    pi = mv.pi;
    mv.pi = nullptr;
}
Cpmv::~Cpmv(void)
{
    delete pi;
}
Cpmv & Cpmv::operator=(const Cpmv & cp)
{
    if(this == &cp)
        return *this;
    delete pi;
    pi = new Info;
    pi->qcode = cp.pi->qcode;
    pi->zcode = cp.pi->zcode;
    return *this;
}
Cpmv & Cpmv::operator=(Cpmv && mv)
{
    if(this == &mv)
        return *this;
    delete pi;
    pi = mv.pi;
    mv.pi = nullptr;
    return *this;
}
Cpmv Cpmv::operator+(const Cpmv & obj) const
{
    Cpmv temp;
    temp.pi->qcode = pi->qcode += obj.pi->qcode;
    temp.pi->zcode = pi->zcode += obj.pi->zcode;
    return temp;
}
void Cpmv::Display(void) const
{
    cout << "qcode:" << pi->qcode << endl;
    cout << "zcode:" << pi->zcode << endl;
}

18.12.3

#include <iostream>
using namespace std;
template <typename T, typename... Args>
long double sum_value(T head, Args...);
int sum_value(void);
int main(void)
{
    cout << sum_value(1, 2, 3, 4);
    return 0;
}
template <typename T, typename... Args>
long double sum_value(T head, Args... args)
{
    return head + sum_value(args...);
}
int sum_value(void)
{
    return 0;
}

18.12.4

#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
#define outint ([](int n) { cout << n << endl; })
int main(void)
{
    int vals[10] = { 50, 100, 90, 180, 60, 210, 415, 88, 188, 201 };
    list<int> yadayada(vals, vals + 10);
    list<int> etcetera(vals, vals + 10);
    cout << "Original lists:\n";
    for_each(yadayada.begin(), yadayada.end(), outint);
    cout << endl;
    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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值