C++PrimerPlus(第6版)中文版十八章实例代码

实例18.1rvref.cpp

#include <iostream>

inline double f(double tf)
{
	return 5.0 * (tf - 32.0) / 9.0;
}

int main()
{
	using namespace std;
	double tc = 21.5;
	double &&rd1 = 7.07;
	double &&rd2 = 1.8 * tc + 32.0;
	double &&rd3 = f(rd2);
	cout << " tc value and address: " << tc << ", " << &tc << endl;
	cout << "rd1 value and address: " << rd1 << ", " << &rd1 << endl;
	cout << "rd2 value and address:" << rd2 << ", " << &rd2 << endl;
	cout << "rd3 value and address:" << rd3 << ", " << &rd3 << endl;
	cin.get();

	system("pause");
	return 0;
}

实例18.2useless.cpp

#include <iostream>
using namespace std;

// interface
class Useless
{
private:
	int n;
	char *pc;
	static int ct;
	void ShowObject() const;
public:
	Useless();
	explicit Useless(int k);
	Useless(int k, char ch);
	Useless(const Useless &f);
	Useless(Useless &&f);
	~Useless();
	Useless operator+(const Useless &f) const;
	// neew operator=() in copy and move versions
	void ShowData() const;
};

// implementation
int Useless::ct = 0;

Useless::Useless()
{
	++ct;
	n = 0;
	pc = nullptr;
	cout << "default constructor called; number of objects: " << ct << endl;
	ShowObject();
}


Useless::Useless(int k) : n(k)
{
	++ct;
	cout << "int constructor called; number of objects: " << ct << endl;
	pc = new char[n];
	ShowObject();
}

Useless::Useless(int k, char ch) : n(k)
{
	++ct;
	cout << "int, char constructor called; number of objects: " << ct << endl;
	pc = new char[n];
	for(int i = 0 ; i < n; ++i)
	{
		pc[i] = ch;
	}
	ShowObject();
}

Useless::Useless(const Useless &f) : n(f.n)
{
	++ct;
	cout << "copy const called; number of objects: " << ct << endl;
	pc = new char[n];
	for (int i = 0; i < n; ++i)
	{
		pc[i] = f.pc[i];
	}
	ShowObject();
}

Useless::Useless(Useless &&f) : n(f.n)
{
	++ct;
	cout << "move constructor called; number of objects: " << ct << endl;
	pc = f.pc;
	f.pc = nullptr;
	f.n = 0;
	ShowObject();
}

Useless::~Useless()
{
	cout << "destructor called; objects left: " << --ct << endl;
	cout << "deleted object:\n";
	ShowObject();
	delete []pc;
}

Useless Useless::operator+(const Useless &f) const
{
	cout << "Entering operator+()\n";
	Useless temp = Useless(n + f.n);
	for(int i = 0; i < n; ++i)
	{
		temp.pc[i] = pc[i];
	}
	for(int i = n; i < temp.n; ++i)
	{
		temp.pc[i] = f.pc[i - n];
	}
	cout << "temp object:\n";
	cout << "Leaving operator+()\n";
	return temp;
}

void Useless::ShowObject() const
{
	cout << "Number of elements: " << n;
	cout << " Data address: " << (void*)pc << endl;
}

void Useless::ShowData() const
{
	if(n == 0)
	{
		cout << "(object empty)";
	}
	else
	{
		for(int i = 0; i < n; ++i)
		{
			cout << pc[i];
		}
	}
	cout << endl;
}

// application
int main()
{
	{
		Useless one(10, 'x');
		Useless two = one;
		Useless three(20, 'o');
		Useless four(one + three);
		cout << "object one: ";
		one.ShowData();
		cout << "object two: ";
		two.ShowData();
		cout << "object three: ";
		three.ShowData();
		cout << "object four: ";
		four.ShowData();
	}
}

实例18.3stdmove.cpp

#include <iostream>
#include <utility>

// interface
class Useless
{
private:
	int n;
	char *pc;
	static int ct;
	void ShowObject() const;
public:
	Useless();
	explicit Useless(int k);
	Useless(int k, char ch);
	Useless(const Useless &f);
	Useless(Useless &&f);
	~Useless();
	Useless operator+(const Useless &f) const;
	Useless& operator=(const Useless &f);
	Useless& operator=(Useless &&f);
	void ShowData() const;
};

// implementation
int Useless::ct = 0;

Useless::Useless()
{
	++ct;
	n = 0;
	pc = nullptr;
}

Useless::Useless(int k) : n(k)
{
	++ct;
	pc = new char[n];
}

Useless::Useless(int k, char ch) : n(k)
{
	++ct;
	pc = new char[n];
	for(int i = 0; i < n; ++i)
	{
		pc[i] = ch;
	}
}

Useless::Useless(const Useless &f) : n(f.n)
{
	++ct;
	pc = new char[n];
	for(int i = 0; i < n; ++i)
	{
		pc[i] = f.pc[i];
	}
}

Useless::Useless(Useless &&f) : n(f.n)
{
	++ct;
	pc = f.pc;
	f.pc = nullptr;
	f.n = 0;
}

Useless::~Useless()
{
	delete []pc;
}

Useless& Useless::operator=(const Useless &f)
{
	std::cout << "copy assignment operator called:\n";
	if(this == &f)
	{
		return *this;
	}
	delete []pc;
	n = f.n;
	pc = new char[n];
	for(int i = 0; i < n; ++i)
	{
		pc[i] = f.pc[i];
	}
	return *this;
}

Useless& Useless::operator=(Useless &&f)
{
	std::cout << "move assignment operator called:\n";
	if(this == &f)
	{
		return *this;
	}
	delete []pc;
	n = f.n;
	pc = f.pc;
	f.n = 0;
	f.pc = nullptr;
	return *this;
}

Useless Useless::operator+(const Useless &f) const
{
	Useless temp = Useless(n + f.n);
	for(int i = 0;  i < n; ++i)
	{
		temp.pc[i] = pc[i];
	}
	for(int i = 0; i < temp.n; ++i)
	{
		temp.pc[i] = f.pc[i - n];
	}
	return temp;
}

void Useless::ShowObject() const
{
	std::cout << "Number of elements: " << n;
	std::cout << " Data address: " << (void*)pc << std::endl;
}

void Useless::ShowData() const
{
	if(n == 0)
	{
		std::cout << "(object empty)";
	}
	else
	{
		for(int i = 0; i < n; ++i)
		{
			std::cout << pc[i];
		}
	}
	std::cout << std::endl;
}

// application
int main()
{
	using std::cout;
	{
		Useless one(10, 'x');
		Useless two = one + one;
		cout << "object one: ";
		one.ShowData();
		cout << "object two: ";
		two.ShowData();
		Useless three, four;
		cout << "three = one\n";
		three = one;
		cout << "now object three = ";
		three.ShowData();
		cout << "and object one = ";
		one.ShowData();
		cout << "four = one + two\n";
		four = one + two;
		cout << "non object four = ";
		four.ShowData();
		cout << "four = move(one)\n";
		four = std::move(one);
		cout << "now object four = ";
		four.ShowData();
		cout << "and object one = ";
		one.ShowData();
	}
}

实例18.4lambda0.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>

const long Size1 = 39L;
const long Size2 = 100 * Size1;
const long Size3 = 100 * Size2;
bool f3(int x)
{
	return x % 3 == 0;
}
bool f13(int x)
{
	return x % 13 == 0;
}

int main()
{
	using std::cout;
	std::vector<int> numbers(Size1);

	std::srand(std::time(0));
	std::generate(numbers.begin(), numbers.end(), std::rand());

	// using function pointers
	cout << "Sample Size = " << Size1 << '\n';

	int count3 = std::count_if(numbers.begin(), . numbers.end(), f3);
	cout << "Count of numbers divisible by 3: " << count3 << '\n';
	int count13 = std::count_if(numbers.begin(), numbers.end(), f13);
	cout << "Count of numbers divisible by 13: " << count13 << "\n\n";

	// increase number of numbers
	numbers.resize(Size2);
	std::generate(numbers.begin(), numbers.end(), std::rand());
	cout << "Sample size = " << Size2 << endl;

	// using a functor
	class f_mod
	{
	private:
		int dv;
	public:
		f_mod(int d = 1) : dv(d)
		{

		}
		bool operator()(int x)
		{
			return x % dv == 0;
		}
	};

	count3 = std::count_if(numbers.begin(), numbers.end(), f_mod(3));
	cout << "Count of numbers divisible by 3:" << count3 << '\n';
	count13 = std::count_if(numbers.begin(), numbers.end(), f_mod(13));
	cout << "Count of numbers divisible by 13: " << count13 << "\n\n";

	// increase number of numbers again
	numbers.resize(Size3);
	std::generate(numbers.begin(), numbers.end(), std::rand);
	cout << "Sample size = " << Size3 << '\n';

	// using lambdas
	count3 = std:::count_if(numbers_begin(), numbers.end(), [](int x) { return x % 3 == 0; });
	cout << "Count of numbers divisible by 13: " << count13 << '\n';

	system("pause");
	return 0;
}

实例18.5lambda1.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
const long Size = 390000L;

int main()
{
	using std::cout;
	std::vector<int> numbers(Size);

	std::srand(std::time(0));
	std::generate(numbers.begin(), numbers.end(), std::rand);
	cout << "Sample size = " << Size << '\n';

	// using lambdas
	int count3 = std::count_if(numbers.begin(), numbers.end(), [](int x) {return x % 3 == 0; });
	cout << "Count of numbers divisible by 3: " << count3 << '\n';
	int count13 = 0;
	std::for_each(numbers.begin(), numbers.end(), [&count13](int x) {count13 += x % 13 == 0; });
	cout << "Count of numbers divisible by 13: " << count13 << '\n';

	// using a single lambda
	count3 = count13 = 0;
	std::for_each(numbers.begin(), numbers.end(), [&](int x) {count3 += x % 3 == 0; count13 += x % 13 == 0; });
	cout << "Count of numbers divisible by 3: " << count3 << '\n';
	cout << "Count of numbers divisible by 13: " << count13 << '\n';

	system("pause");
	return 0;
}

实例18.6somedef.h

#include<iostream>

template <typename T, typename F>
T use_f(T v, F f)
{
	static int count = 0;
	++count;
	std::cout << " use_f count = " << count
		<< ", &count = " << &count << std::endl;
	return f(v);
}

class Fp
{
private:
	double z_;
public:
	Fp(double z = 1.0) : z_(z){}
	double operator() (double p) { return z_ * p; }
};

class Fq
{
private:
	double z_;
public:
	Fq(double z = 1.0) : z_(z) {}
	double operator() (double q) { return z_ + q; }
};

实例18.7callable.cpp

#include "somedef.h"
#include <iostream>

double dub(double x) { return 2.0 * x; }
double square(double x) { return x * x; }
int main()
{
	using std::cout;
	using std::endl;
	
	double y = 1.21;
	cout << "Function pointer dub:\n";
	cout << " " << use_f(y, dub) << endl;
	cout << "Funciton pointer square:\n";
	cout << " " << use_f(y, square) << endl;
	cout << "Function object Fp:\n";
	cout << " " << use_f(y, Fp(5.0)) << endl;
	cout << "Function object Fq\n";
	cout << " " << use_f(y, Fq(5.0)) << endl;
	cout << "Lambda expression 1:\n";
	cout << " " << use_f(y, [](double u) {return u * u; }) << endl;
	cout << "Lambda expression 2:\n";
	cout << " " << use_f(y, [](double u) {return u + u / 2.0; }) << endl;

	system("pause");
	return 0;
}

实例18.8wrapped.cpp

#include "somedef.h"
#include <iostream>
#include <functional>

double dub(double x) { return 2.0 * x; }
double square(double x) { return x * x; }

int main()
{
	using std::cout;
	using std::endl;
	using std::function;

	double y = 1.21;
	function<double(double)> ef1 = dub;
	function<double(double)> ef2 = square;
	function<double(double)> ef3 = Fq(10.0);
	function<double(double)> ef4 = Fp(10.0);
	function<double(double)> ef5 = [](double u) { return u * u; };
	function<double(double)> ef6 = [](double u) {return u + u / 2.0; };

	cout << "Function pointer dub:\n";
	cout << " " << use_f(y, ef1) << endl;
	cout << "Function pointer square:\n";
	cout << " " << use_f(y, ef2) << endl;
	cout << "Function object Fp:\n";
	cout << " " << use_f(y, ef3) << endl;
	cout << "Function object Fq:\n";
	cout << " " << use_f(y, ef4) << endl;
	cout << "Lambda expression 1:\n";
	cout << " " << use_f(y, ef5) << endl;
	cout << "Lambda expression 2:\n";
	cout << " " << use_f(y, ef6) << endl;

	system("pause");
	return 0
}

实例18.9variadic1.cpp

#include <iostream>
#include <string>

void show_list3(){}

template<typename T, typename... Args>
void show_list3(T value, Args... args)
{
	std::cout << value << ", ";
	show_list3(args...);
}

int main()
{
	int n = 14;
	double x = 2.71828;
	std::string mr = "Mr. string objects:";
	show_list3(n, x);
	show_list3(x * x, '!', 7, mr);

	system("pause");
	return 0;
}

实例18.10variadic2.cpp

#include <iostream>
#include <string>

void show_list(){}

template<typename T>
void show_list(const T& value)
{
	std::cout << value << '\n';
}

template<typename T, typename... Args>
void show_list(const T& value, const Args&... args)
{
	std::cout << value << ", ";
	show_list(args...);
}

int main()
{
	int n = 14;
	double x = 2.71828;
	std::string mr = "Mr. String objects!";
	show_list(n, x);
	show_list(x * x, '!', 7, mr);

	system("pause");
	return 0;
}

实例18.11lexcast.cpp

#include <iostream>
#include <string>
#include "boost/lexical_cast.hpp"
int main()
{
	using namespace std;
	cout << "Enter your weight: ";
	float weight;
	cin >> weight;
	string gain = "A 10% increase raises ";
	string wt = boost::lexical_cast<string>(weight);
	gain = gain + wt + " to ";
	weight = 1.1 * weight;
	gain = gain + boost::lexical_cast<string>(weight) + ".";
	cout << gain << endl;

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值