随笔分类 - C++训练
摘要:class Solution {public:int InversePairs(vector data) {if(data.empty())return 0;int n=data.size();vector copy(n);return Inverse(data,copy,0,n-1);}int I...
阅读全文
摘要:19.18编写一个函数,使用count_if统计在给定的vector中有多少个空string。#include#include#include#include#includeusing namespace std;int main(){ vector svec={"fhhd","fdf",""...
阅读全文
摘要:17.39#include#include#include#includeusing namespace std;int main(){ fstream inOut("copyOut",fstream::ate|fstream::in|fstream::out); if(!inOut) ...
阅读全文
摘要:#include#include#includeusing namespace std;int main(){ ifstream file("1.txt"); char ch[20]; cout<<"getline: "<<endl; while(file.getline(c...
阅读全文
摘要:#include#include#includeusing namespace std;int main(){ cout>noskipws; while(cin>>ch) cout>skipws;}
阅读全文
摘要:17.28 编写函数,每次调用生成并返回一个均匀分布的随机unsigned int。 ...
阅读全文
摘要:17.10使用序列1、2、3、5、8、13、21初始化一个bitset,将这些位置置位。对另一个bitset进行默认初始化,并编写一小段程序将其恰当的位置位。#include#includeusing namespace std;int main(){ bitset bits("1000000...
阅读全文
摘要:17.4编写并测试findbook函数#include#include#include#include#include#include"Sales_data.h"using namespace std;typedef tuple::size_type,vector::const_iterator,v...
阅读全文
摘要:17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类。TextQuery.h#ifndef TEXTQUERY_H#define TEXTQUERY_H#include#include#include#include#include#include#include#...
阅读全文
摘要:16.62定义你自己版本的hash,并定义一个Sales_data对象的unordered_multiset。将多条交易记录保存到容器中,并打印其内容。Sales_data.h#ifndef SALES_DATA_H#define SALES_DATA_H#include#includeusing ...
阅读全文
摘要:16.58 为你的StrVec类添加emplace_back函数。StrVec.h(注意,函数模板和模板成员函数的定义和声明要放在一起,通常都放在头文件中)#ifndef STRVEC_H#define STRVEC_H#include#include#include#include#include...
阅读全文
摘要:16.53 编写你自己版本的print函数,并打印一个、两个及五个实参来测试它,要打印的每个实参都应有不同的类型。#include#includeusing namespace std;template ostream& print(ostream &os,const T &t){ osost...
阅读全文
摘要:16.47 编写你自己版本的翻转函数,通过调用接受左值和右值引用参数的函数来测试它。#include#include#includeusing namespace std;template int compare(const T &a ,const T &b){ if(aauto sum(T ...
阅读全文
摘要:16.21 编写你自己的DebugDelete版本。#include#includeusing namespace std;class DebugDelete{public: DebugDelete(ostream &s=cerr):os(s) {} template void ...
阅读全文
摘要:16.19 编写函数,接受一个容器的引用,打印容器中的元素,使用容器的size_type和size成员来控制打印元素的循环。16.20 重写上一题的函数,使用begin和end返回的迭代器来控制循环。#include#include#include#includeusing namespace st...
阅读全文
摘要:16.12编写你自己版本的Blob和BlobPtr模板,包含书中未定义的多个const成员。Blob.h(注意,成员函数的声明和定义要放在一个头文件中)/*记住,模板的头文件中通常既包括声明也包括定义。函数模板和类模板成员函数的定义通常放在头文件中,不能分开放。。。。谨记*/#ifndef BLOB...
阅读全文
摘要:16.4 编写行为类似标准库find算法的模板。函数需要两个模板类型参数,一个表示函数的迭代器参数,另一个表示值的类型。使用你的函数在一个vector和一个list中查找给定值。#include#include#include#include#includeusing namespace std;t...
阅读全文
摘要:15.26 定义Quote和Bulk_quote的拷贝控制成员,令其与合成的版本行为一致。为这些成员以及其他构造函数添加打印状态的语句,使得我们能够知道正在运行哪个程序。使用这些类编写程序,预测程序将创建和销毁哪些对象。重复实验,不断比较你的预测和实际输出结果是否相同,直到预测完全准确再结束。Quo...
阅读全文
摘要:15.7定义一个类使其实现一种数量受限的折扣策略,具体策略是:当购买书籍的数量不超过一个给定的限量时享受折扣,如果购买量一旦超过了限量,则超出的部分将以原价销售。Quote.h#ifndef QUOTE_H#define QUOTE_H#include#includeusing namespace ...
阅读全文
摘要:14.44编写一个简单的桌面计算器使其能处理二元运算。#include#include#include#includeusing namespace std;int add(int a,int b){ return a+b;}struct divide{ int operator()(i...
阅读全文

浙公网安备 33010602011771号