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

C++ Primer 【第四版】第十六章 部分选做习题

2014-08-12 14:33 344 查看
16.1编写一个模板返回形参的绝对值,至少用三种不同类型的值调用模板。注意:在16.3节讨论编译器怎样处理模板实例化之前,你应该将每个模板定义和该模板的所有使用放在同一文件中。

//16.1_template.cpp :
定义控制台应用程序的入口点。

//

#include
"stdafx.h"

#include
<iostream>

template <typename
T> inline
T abs(
T tVal)

{

return tVal > 0 ?
tVal: -1*tVal;

}

int
_tmain(int
argc, _TCHAR* argv[])

{

std::cout <<
"\n\tabs(false ) is:\t" <<
abs(false) << std::endl

<<"\n\tabs( -3 ) is:\t\t" <<
abs( -3 ) << std::endl

<<"\n\tabs( -8.6 ) is:\t\t" <<
abs( -8.6 ) << std::endl;

system("pause");

return0;

}

16.2
编写一个函数模板,接受一个ostream引用和一个值,将该值写入流。用至少四种不同类型调用函数。通过写至cout、写至文件和写至stringstream来测试你的程序。

//16.2_template.cpp :
定义控制台应用程序的入口点。

//

#include
"stdafx.h"

#include
<iostream>

#include
<ostream>

#include
<fstream>

#include
<sstream>

#include
<cassert>

using
namespace std;

template <typename
T> inline

void
testOstream( ostream&
os, T
tVal )

{

os<< tVal;

}

int
_tmain(int
argc, _TCHAR* argv[])

{

// cout

cout<< " Test of testOstream( cout, 6):\t";

testOstream(cout, 6 );

cout<< "\n Test of testOstream( cout, true):\t";

testOstream(cout,
true );

cout<< "\n Test of testOstream( cout, 8.6):\t";

testOstream(cout, 8.6 );

cout<< "\n Test of testOstream( cout,\"Good!\" ):\t";

testOstream(cout,
"Good!");

cout<< endl;

// ofstream

ofstreamoutfile;

outfile.open("testOf_Ofstream.txt");

assert(outfile );

if ( !outfile )

{

returnNULL;

}

cout<< "\n\tTest of ofstream, Please checkthe file just create.\n";

testOstream(outfile,
"\n Test oftestOstream( outfile, 6 ):\t");

testOstream(outfile, 6 );

testOstream(outfile,
"\n Test oftestOstream( outfile, true ):\t");

testOstream(outfile,
true);

testOstream(outfile,
"\n Test oftestOstream( outfile, 8.6 ):\t");

testOstream(outfile, 8.6 );

testOstream(outfile,
"\n Test oftestOstream( outfile, \"Good!\" ):\t");

testOstream(outfile,
"Good!");

outfile.close();

cout<< endl;

// stringstream

stringstreamoss;

cout<< "\n Test of testOstream( oss, 6 )/(oss, true )/( oss, 8.6 )/( oss, \"Good!\"):" <<
endl;

testOstream(oss, 6 );

testOstream(oss,
"\n");

testOstream(oss,
true );

testOstream(oss,
"\n");

testOstream(oss, 8.6 );

testOstream(oss,
"\n");

testOstream(oss,
"Good!");

testOstream(oss,
"\n");

cout<< oss.str()<<
endl;

system("pause");

return0;

}

16.3当调用两个string对象的compare时,传递用字符串字面值初始化的两个string对象。如果编写以下代码会发生什么?

compare ( “hi”, “world” );

将会出编译错误,根据第一个实参”hi”
可将模板形参T推断为char[3],而第二个实参”world”可将模板形参T推断为char[6],T被推断为两个不同的类型,编译器无法实例化。

16.5定义一个函数模板,返回两个值中较大的一个。

template < typename T> T Max( T &val1, T &val2 )

{

return val1 > val2 ? val1 : val2;

}

16.6
(标记但暂时不做,待stl学习过后做)

16.8如果有,解释下面哪些声明是错误的并说明为什么。

(a) template <class Type> Type bar ( Type, Type );

template<class Type> Type bar ( Type, Type );

(b) template <class T1, class T2 > void bar ( T1, T2);

template<class C1, typename C2 > void bar ( C1, C2);

都正确。

16.9
(标记但暂时不做,待stl学习过后做)

16.11何时必须使用typename?

如果要在函数模板内部使用在类中定义的类型成员,必须在该成员名前加上关键字typename,告诉编译器将该成员当做类型。

16.12编写一个函数模板,接受表示未知类型迭代器的一对值,找出在序列中出现得最频繁的值。

template <typename
T > T::value_type
MostFreq( T
first, T last )

{

// 计算需分配内存的大小

std::size_t
amount = 0;

T start =
first;

while (start !=
last)

{

amount++;

start++;

}

// 定义类型别名

typedefstd::vector<typename
T::value_type> VecType;

// 创建vector对象,用于保存输入序列的副本

VecTypevec(amount);

VecType::iterator
newFirst =vec.begin();

VecType::iterator
newLast = vec.end();

// 将输入序列复制到vector对象

std::uninitialized_copy(
first,last,
newFirst);

std::sort (
newFirst, newLast );
// 对副本序列排序,使得相同的值出现在相邻位置

std::size_t
maxOccu = 0,occu = 0;
// 出现最频繁的次数,当前值的出现次数

VecType::iterator
preIter = newFirst;
//指向当前值的前一个值

VecType::iterator
maxOccuElement= newFirst;
// 指向当前出现最频繁的值

while( newFirst !=
newLast)

{

if( *newFirst != *preIter)
// 当前值与前一值不同

{

if ( occu >
maxOccu )
// 当前值的出现次数为目前最大次数

{

maxOccu = occu;
// 修改最大次数

maxOccuElement =
preIter; // 修改指向当前出现最频繁的值的迭代器

}

occu = 0;

}

++occu;

preIter= newFirst;

++newFirst;

}

// 最后一个值的出现次数与目前的最大次数进行比较

if ( occu >
maxOccu )

{

maxOccu= occu;

maxOccuElement=
preIter;

}

return*maxOccuElement;

}

16.17在3.3.2节的“关键概念”中,我们注意到,C++程序员习惯于使用
!= 而不用< ,
解释这一习惯的基本原理。

标准库中的类及泛型算法大多定义为模板类及模板函数,它们的实例化版本是否合法取决于用作模板实参的类型是否支持模板所要求的操作。对于用作模板实参的类型,支持相等操作符==和不等操作符!=的可能性要比支持关系操作符
< 的可能性更大,因此使用 !=
而不用<.

16.21指出对模板实参推断中涉及的函数实参允许的类型转换。

const转换:接受const引用或const指针的函数可以分别用非const对象的引用或指针来调用,无需产生新的实例化。如果函数接受非引用类型,形参类型和实参都忽略cosnt,即无论传递const或非const对象给接受非引用类型的函数,都使用相同的实例化。

数组或函数到指针的转换:如果模板形参不是引用类型,则对数组或函数类型的实参应用常规指针转换。数组实参将当做指针其第一个元素的指针,函数实参将当做指向函数类型的指针。

16.22对于下面的模板:

template < class Type>

Type calc ( const Type* array, int size);

template < class Type >

Type fcn( Type p1, Type p2)

下面这些调用有错吗?如果有,哪些是错误的?为什么?

double dobj;
float fobj; char cobj;

in tai[5] = { 511, 16, 8, 63, 34 };

(a) calc ( cobj, ‘c’ );

(b) calc( dobj, fobj);

(c) fcn( ai, cobj);

(a)
第一个实参为char类型,不能使用函数模板产生第一个形参为非指针类型的函数实例。

(b)
第一个实参为double类型,不能使用函数模板产生第一个形参为非指针类型的函数实例。

(c)
两个实参的类型不一样,不能使用函数模板fcn进行实例化。

16.26
对于下面的sum模板定义:

template <class T1, class T2,
class T3 > T1 sum( T2, T3 );

解释下面的每个调用,是否有错?如果有,指出哪些是错误的,对每个错误,解释错在哪里。

double dobj1, dobj2;
float fobj1, fobj2; char cobj1, cobj2;

(a) sum ( dobj1, dobj2 );

(b) sum< double, double, double > (fobj2, fobj2);

(c) sum<int> ( cobj1, cobj2 );

(d) sum < double, , double > (fobj2,dobj2);

(a)错误,没有显示指定T1实参类型。

(b)正确,产生double
sum ( double, double) 的实例,并将两个函数实参由float转换为double类型来调用该实例。

(c)正确,产生intsum(
char, char )的实例。

(d)错误,只有最右边形参的显示模板实参可以省略,不能用空格代替被省略的显示模板实参。

16.28如果所用的编译器支持分别编译模型,将类模板的成员函数和static数据成员的定义放在哪里?为什么?

如果编译器支持分别编译模型,则类模板的内联成员函数与类模板的定义一起放在头文件中,而非内联成员函数和static数据成员的定义应该放在实现文件中。因为内联函数的定义必须在函数被扩展时能被编译器所见,而非内联函数一般并不希望被用户看见,所以将其放在实现文件中。

16.37
下面哪些模板实例化是有效的?解释为什么实例化无效?

template <class T, int size> classArray {
};

template< int hi, int wid > classScreen {
};

(a ) const int hi = 40, wi = 80; Screen<hi, wi + 32> sObj;

(b) const int arr_size = 1024; Array<string, arr_size > a1;

(c) unsigned int asize = 255; Array<int, assize > a2;

(e) const double db = 3.1415;
Array< double, db> a3;

(a)
和 (b)
有效。

(c)
无效,因为非类型模板实参
必须是
编译时常量表达式,不能用变量asize做模板实参。

(d)
无效,因为db是double型常量,与模板中的int型模板实参不符。

16.38
编写Screen类模板,使用非类型形参定义Screen的高度和宽度。

template< int hi, intwid >

class Screen

{

public:

typedef std::string::size_type index;

Screen() : contents( hi * wid, ‘#’ ), cursor(0), height( hi ),width( wid ) {
}

Screen ( const std::string & cont );

char get() cosnt

{

return contents[ cursor ];

}

char get( index ht, index wd ) const;

index get_cursor() const { return cursor; }

Screen& move( index r, index c );

Screen& set( char );

Screen& display( ostream &os );

const Screen& display( ostream &os ) cosnt;

private:

std::string contents;

index cursor;

index height, width;

};

template<int hi, intwid>
Screen < hi, wid>::Screen< hi, wid> ( cosnt std::string& cont ) :

cursor ( 0 ), height( hi ), width( wid )

{

contents.assgin( hi*wid, ‘ ‘ );

if ( cont.size() != 0 )

contents.replace (0, cont.size(), cont );

}

// …

16.53编写一个程序调用上题中定义的count函数,首先传给该函数一个double类型vector,然后传递一个int型vector,最后传递一个char型vector。

//16.53_template_count_vector.cpp :
定义控制台应用程序的入口点。

//

#include
"stdafx.h"

#include
<vector>

#include
<iostream>

using
namespace std;

template<
typename T1,
typename T2 > int
count( vector<T1>&
vec, T2tVal )

{

int iCnt = 0;

for ( vector<T1>::iterator
it = vec.begin();
it != vec.end(); ++it )

{

if ( (*it) ==
tVal )

{

iCnt++;

}

}

return iCnt;

}

int
_tmain(int
argc, _TCHAR* argv[])

{

vector<double>
dVec(10,8.6);

vector<int>
iVec(9, 6);

vector<char>
cVec(8, 'd');

cout<< "\n\tThe '8.6' in thevector<double> dVec(10, 8.6), its time is:\t" <<
count( dVec, 8.6 )<<
endl;

cout<< "\n\tThe '6' in the vector<int> iVec(9,6), its time is:\t" <<
count(iVec, 6 ) <<
endl;

cout<< "\n\tThe 'd' in the vector<char>cVec(8, 'd'), its time is:\t" <<
count(cVec,
'd' )<< endl;

system("pause");

return0;

}

16.60讨论这两个设计的优缺点:为const char*
定义该类的特化版本和只特化push和pop函数。具体而言,比较front的行为的异同以及用户代码中的错误破坏Queue元素的可能性。

在为const char*
定义该类的特化版本时,优点是:front函数返回string对象,该string对象时Queue中QueueItem节点中item部分所包含的string对象的副本,用户代码对该副本对象的任意使用都不会破坏Queue元素;缺点是:定义一个特化类需要修改全部的成员。

只特化push和pop函数时,优点是:front函数返回类型为const
char* 的指针,该指针时Queue中QueueItem节点中item部分所包含的指针的副本。两个指针指向同一块内存,用户代码对该指针的使用有可能破坏Queue元素;优点是只需要修改push和pop函数为特化版本即可。

16.61实现compare函数的三个版本。在每个函数中包含一个输出语句,指出正在调用哪个函数。使用这些函数检查对其余问题的回答。

template<typenameT> int compare ( const T& v1, const T& v2 )

{

cout << “Using compare 2 objects: int compare ( constT& v1, const T& v2 )” << endl;

if ( v1 < v2 ) return -1;

if ( v2 < v1 ) return 1;

return 0;

}

template < class U,class V > int compare ( U v1, U v2, V beg )

{

cout << “ Using compare elements int 2 sequences”<< endl;

return 0;

}

int compare( const char*p1, const char* p2 )

{

cout << “ Using ordinary function to handle C-stylecharacter strings” << endl;

return strcmp( p1, p2 );

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: