77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-03-16 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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 );

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C++Primer 第14章-重载操作符与转换-课后习题答案(4)在线全文阅读。

C++Primer 第14章-重载操作符与转换-课后习题答案(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/528921.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: