C++Primer 第14章-重载操作符与转换-课后习题答案(4)

2019-03-16 18:02

31。定义一个函数对象执行“如果??则??否则”操作;该函数对象应接受三个形参;它应该测试第一个形参;如果测试成功,则返回第二个形参;否则,返回第三个形参。 class NoName {

public: NoName() { } NoName( int i1, int i2, int i3 ): iVal1( i1 ), iVal2( i2 ), iVal3( i3) { } int operator() ( int i1, int i2, int i3 ) { return i1? i2: i3; } private: int iVal1; int iVal2; int iVal3; };

32。一个重载的函数调用操作符可以接受多少个操作数? 0个或多个。

33。使用标准库算法和GT_cls类,编写一个程序查找序列中第一个比指定值大的元素。 // 14.33_Gt_cls_and_algorithm.cpp : 定义控制台应用程序的入口点。 //

#include \#include #include #include #include using namespace std;

class GT_cls {

public: GT_cls( const string gW = \ ) : givenWord( gW ) { } bool operator() ( const string &s ) { return ( s > givenWord ); }

private: std::string givenWord;

};

int _tmain(int argc, _TCHAR* argv[]) { std::cout << \ << std::endl; vector text; string word; while ( std::cin >> word ) { text.push_back( word ); } // end of input the text // input the given word std::cin.clear(); std::cout << \; string gWord; std::cin >> gWord; // deal with text , to realize find the first word which is bigger than the given word vector::iterator it = find_if( text.begin(), text.end(), GT_cls( gWord )); if ( it != text.end() ) std::cout << \ << std::endl << \ << std::endl << \ << *it << std::endl; system(\); return 0; }

34。编写类似于GT_cls的类,但测试两个值是否相等。使用该对象和标准库算法编写程序,替换序列中给定值的所有实例。

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

#include \#include #include #include #include using namespace std;

class GT_cls {

public: GT_cls( int iVal = 0 ) : val( iVal ) { } bool operator() ( const int & iv ) { return ( iv == val ); }

private: int val; };

int _tmain(int argc, _TCHAR* argv[]) { std::cout << \ << std::endl; vector iVec; int iVal; while ( std::cin >> iVal) { iVec.push_back( iVal ); } // end of input the iVec

}

cin.clear();

cout << \; int rp; cin >> rp;

cout << \; int givenNum; cin >> givenNum;

// replace

replace_if( iVec.begin(), iVec.end(), GT_cls( rp ), givenNum ); cout << \;

for ( vector::iterator it = iVec.begin(); it != iVec.end(); ++it ) { cout << *it << \; }

system(\); return 0;

35。编写类似于GT_cls的类,但测试给定string对象的长度是否与其边界相匹配。报告输入中有多少单词的长度在1到10之间。

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

#include \#include #include #include #include using namespace std;

class BT_cls {

public: BT_cls( size_t len1 = 0, size_t len2 = 0 ) { if ( len1 < len2 ) { minlen = len1; maxlen = len2; } else { minlen = len2; maxlen = len1; } } bool operator() ( const string &s ) { return ( s.size() >= minlen && s.size() <= maxlen ); }

private: std::string::size_type minlen, maxlen; };

bool isShorter( const string &s1, const string &s2 ) { return s1.size() < s2.size(); }

int _tmain(int argc, _TCHAR* argv[]) { std::cout << \ << std::endl; vector text; string word; while ( std::cin >> word ) { text.push_back( word ); } // end of input the text // deal with text sort( text.begin(), text.end() ); text.erase( unique( text.begin(), text.end() ), text.end() ); stable_sort( text.begin(), text.end(), isShorter );


C++Primer 第14章-重载操作符与转换-课后习题答案(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2013北京大兴区高三数学(文)一模试题及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: