C++ primer plus课后练习题

第十八章:探讨C++新标准

        一、复习题

        1. 

std::vector<int> ai {3,9,4,7,1};

        2. r1(w+1)合法,会产生一个临时变量;r1(up(w))合法,会产生一个临时变量;r2(w+1)和r2(up(w))都不对,因为w+1和up(w)的返回值均为右值。r3(w)不对,因为w是一个左值,应该调用r1或者r2。

        3. a. double & rx

                const double & rx

                const double & rx                

            b. double & rx

                double && rx

                double && rx

            c. const double & rx

                double && rx

                double && rx

        4. 默认构造函数,复制构造函数,赋值构造函数,析构函数,移动复制构造函数,移动赋值构造函数;特殊在如果你不提供上述方法,编译器会默认提供。

        5. 将double数组改为一个指针

        6. 

show2(18.0,[](double x){return 1.8*x + 32;}

        7. 

sum(temp_c, [&total](double w){total += w;}

        二、编程练习

        1.

#include <iostream>

template<typename T>
T average_list(std::initializer_list<T> list)
{
    T sum = 0;
    for(T elem : list)
        sum += elem;
    return sum / list.size();
}

double average_list(std::initializer_list<double> list)
{
    double sum = 0;
    for(double elem : list)
        sum += elem;
    return sum / list.size();
}

int main()
{
    using namespace std;

    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({'A',70,65.33});
    cout << ad << endl;
    return 0;
}

        2.

#include <iostream>
#include <utility>

class Cpmv
{
  public:
    struct Info
    {
        std::string qcode;
        std::string zcode;
    };

  private:
    Info *pi;

  public:
    Cpmv() : pi(new Info)
    {
        pi->qcode = "none";
        pi->zcode = "none";
    }
    Cpmv(std::string q, std::string z)
    {
        pi = new Info;
        pi->qcode = q;
        pi->zcode = z;
    }
    Cpmv(const Cpmv &cp)
    {
        pi = new Info;
        pi->qcode = cp.pi->qcode;
        pi->zcode = cp.pi->zcode;
    }
    Cpmv(Cpmv &&mv) noexcept
    {
        pi = mv.pi;
        mv.pi = new Info;
        mv.pi->qcode = "none";
        mv.pi->zcode = "none";
    }
    ~Cpmv()
    {
        delete pi;
    }
    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 &operator=(Cpmv &&cp)
    {
        if (this == &cp)
            return *this;
        delete pi;
        pi = cp.pi;
        cp.pi = new Info;
        cp.pi->qcode = "none";
        cp.pi->zcode = "none";
        return *this;
    }
    Cpmv operator+(const Cpmv &obj) const
    {
        Cpmv temp;
        temp.pi->qcode = obj.pi->qcode + pi->qcode;
        temp.pi->zcode = obj.pi->zcode + pi->zcode;
        return temp;
    }
    void Display() const
    {
        std::cout << pi->qcode << std::endl;
        std::cout << pi->zcode << std::endl;
    }
};

int main()
{
    using namespace std;
    Cpmv temp;
    cout << "Object 0:\n";
    temp.Display();

    Cpmv temp1("number one", "number two");
    cout << "Object 1:\n";
    temp1.Display();

    Cpmv temp2(temp);
    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 = std::move(temp2);
    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;
}

         3. 

#include <iostream>

long double sum_value(){}

template<typename T, typename ... Args>
long double sum_value(T elem, Args...args)
{
    return elem + sum_value(args...);
}

int main()
{
    int n = 100;
    double m = 99.66;
    long y = 100000;
    char ch = 'x';
    std::cout << sum_value(n, m, y, ch, 'A', 77, 99.52);

    return 0;
}

        4. 

#include <iostream>
#include <list>
#include <algorithm>

int main()
{
    using namespace std;
    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);

    for_each(yadayada.begin(),yadayada.end(),[](int x){cout << x << " ";});
    cout << endl;
    for_each(etcetera.begin(),etcetera.end(),[](int x){cout << x << " ";});
    cout << endl;

    yadayada.remove_if([](int x){return x > 100;});
    etcetera.remove_if([](int x){return x > 200;});

    for_each(yadayada.begin(),yadayada.end(),[](int x){cout << x << " ";});
    cout << endl;
    for_each(etcetera.begin(),etcetera.end(),[](int x){cout << x << " ";});
    cout << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值