您的位置:首页 > 编程语言 > C语言/C++

《C++ Primer Plus(第六版)》(45)(第十八章 探讨C++新标准 编程练习和答案)

2017-01-05 17:19 567 查看
18.12 编程练习

1.下面是一个简短程序的一部分:

int main(int argc, char* argv[])
{
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;
}
请提供函数average_list(),让该程序变得完整。它应该是一个模板函数,其中的类型参数指定了用作函数参数的initillize_list模板的类型以及函数的返回类型。
//
// main.cpp
// HelloWorld
//
// Created by feiyin001 on 17/01/04.
// Copyright (c) 2016年 Fable. All rights reserved.
//
#include <iostream>
#include <array>
using namespace std;

template<typename T>
T average_list(initializer_list<T> a)
{
T b = 0;
if (a.size() == 0)
{
return b;
}
for_each(a.begin(), a.end(), [&b](T x){ b += x; });
b /= a.size();
return b;
}
int main(int argc, char* argv[])
{
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;
}


2.下面是类Cpmv的声明:
class Cpmv
{
public:
struct Info
{
string qcode;
string zcode;
};
private:
Info* pi;
public:
Cpmv();
Cpmv(string q, 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有操作数的相应成员拼接而成。请提供为移动构造函数和移动赋值运算符实现移动语义的代码。编写一个使用所有这些方法的程序。为了方便测试,让各个方法都显示特定的内容,以便知道他们被调用。

Test.h

//
// Test.h
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//

#ifndef _Test_H_
#define _Test_H_
#include <iostream>
#include <string>
using namespace std;
namespace FableGame
{
class Cpmv
{
public:
struct Info
{
string qcode;
string zcode;
};
private:
Info* pi;
public:
Cpmv();
Cpmv(string q, 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;
};
}
#endif Test.cpp
//
// Test.cpp
// HelloWorld
//
// Created by feiyin001 on 16/12/21.
// Copyright (c) 2016年 FableGame. All rights reserved.
//

#include "Test.h"
#include <iostream>
using namespace std;
using namespace FableGame;

FableGame::Cpmv::Cpmv()
{
pi = new Info;
cout << "Cpmv::Cpmv()" << endl;
}

FableGame::Cpmv::Cpmv(string q, string z)
{
pi = new Info{ q, z };
cout << "Cpmv::Cpmv(string q, string z)" << endl;
}

FableGame::Cpmv::Cpmv(const Cpmv& cp)
{
pi = new Info;
pi->qcode = cp.pi->qcode;
pi->zcode = cp.pi->zcode;
cout << "Cpmv::Cpmv(const Cpmv& cp)" << endl;
}

FableGame::Cpmv::Cpmv(Cpmv&& mv)
{
pi = mv.pi;
mv.pi = nullptr;
cout << "Cpmv::Cpmv(Cpmv&& mv)" << endl;
}

FableGame::Cpmv::~Cpmv()
{
cout << "Cpmv::~Cpmv()" << endl;
}

Cpmv& FableGame::Cpmv::operator=(const Cpmv& cp)
{
pi->qcode = cp.pi->qcode;
pi->zcode = cp.pi->zcode;
cout << "Cpmv::operator=(const Cpmv& cp)" << endl;
return *this;
}

Cpmv& FableGame::Cpmv::operator=(Cpmv&& mv)
{
delete pi;
pi = mv.pi;
mv.pi = nullptr;
cout << "Cpmv::operator=(Cpmv&& mv)" << endl;
return *this;
}

FableGame::Cpmv FableGame::Cpmv::operator+(const Cpmv& obj) const
{
Cpmv temp;
temp.pi->qcode = pi->qcode + obj.pi->qcode;
temp.pi->zcode = pi->zcode + obj.pi->zcode;
cout << "Cpmv::operator+(const Cpmv& obj) const" << endl;
return temp;
}

void FableGame::Cpmv::display() const
{
cout << "qcode: " << pi->qcode << endl;
cout << "zcode: " << pi->zcode << endl;
}
main.cpp
//
// main.cpp
// HelloWorld
//
// Created by feiyin001 on 17/01/04.
// Copyright (c) 2016年 Fable. All rights reserved.
//
#include <iostream>
#include "Test.h"
using namespace std;
using namespace FableGame;

int main(int argc, char* argv[])
{
{
Cpmv cpmv1;
Cpmv cpmv2("11111", "22222");
Cpmv cpmv3(cpmv2);
Cpmv cpmv4(cpmv1 + cpmv2);
Cpmv cpmv5;
cpmv5 = cpmv2 + cpmv3;
Cpmv cpmv6;
cpmv6 = cpmv5;
cpmv1.display();
cpmv2.display();
cpmv3.display();
cpmv4.display();
cpmv5.display();
cpmv6.display();
}

return 0;
}


3.编写并测试可变参数模板函数sum_value(),它接受任意长度的参数列表(其中包含数值,但可以是任何类型),并以long double的方式返回这些数值的和。
//
//  main.cpp
//  HelloWorld
//
//  Created by feiyin001 on 17/01/04.
//  Copyright (c) 2016年 Fable. All rights reserved.
//
#include <iostream>
#include "Test.h"
using namespace std;
using namespace FableGame;

long double sum_value()
{
return 0;
}

template<typename T>
long double sum_value(T value)
{
return value;
}

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

int main(int argc, char* argv[])
{
cout << sum_value(1, 2, 3) << endl;
cout << sum_value('A', 123.456, 789, 10);
return 0;
}


4.使用lambda重新编写程序清单16.5。具体地说,使用一个有名称的lambda替换函数outint(),并将函数符替换为两个匿名lambda表达式。
尼玛竟然是16.15的代码。翻译本的书到后面经常漏这漏那的。。。找了网上的答案,再对照了好久才发现原来是16.15.。。。。。。

//
// main.cpp
// HelloWorld
//
// Created by feiyin001 on 17/01/04.
// Copyright (c) 2016年 Fable. All rights reserved.
//
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
using namespace std;

auto outint = [](int n){ cout << n << " "; };

int main(int argc, char* argv[])
{
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 v){return v > 100; });
etcetera.remove_if([](int v){return v > 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息