![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++primer 5课后编程练习
文章平均质量分 72
WJSZMD
暂无
展开
-
学习C++Primer第五版,课后练习。
P21 练习1.24输入表示多个ISBN的多条销售记录来测试上一个程序,每条ISBN的记录应该聚在一起。下面为我做的时候的程序:#include #include "Sales_item.h"int main(){ Sales_item curbook, book; std::cout << "请输入书籍记录: " << std::endl; if (std::cin原创 2016-11-29 17:07:24 · 512 阅读 · 0 评论 -
C++primeer 6.7节练习(函数指针,函数指针形参)
6.54#include #include #include using namespace std;/*函数指针*/int test(int a, int b);/*定义一个指向test函数类型的指针 pf*/int(*pf)(int, int); /*用decltype和typedef定义此函数类型指针*/typedef decltype(test) *pf1;/原创 2017-10-12 16:06:55 · 331 阅读 · 0 评论 -
c++ primer 13.3节练习(定义自己版本的swap函数并排序)
13.31#include #include #include #include #include #include "StrBlob.h"#include "StrBlobPtr.h"using namespace std;class HashPtr{ friend void swap(HashPtr&, HashPtr&);public: HashPtr(const原创 2017-12-06 15:33:35 · 489 阅读 · 0 评论 -
c++ primer 12.1.4节练习(自定义 shared_ptr的删除器)
12.14#include #include #include #include using namespace std;string destination = "目的";string connection = "连接";string Connect(string *) //获得一个连接{ cout << "获得一个连接" << endl; string temp原创 2017-12-01 13:17:23 · 307 阅读 · 0 评论 -
c++ primer 8.1.2节练习(关于流的知识)
8.1#include #include using namespace std;istream & Process(istream & i)//对于流对象,不能拷贝和赋值,必须使用引用来传递实参{ int test; while (i >> test||!i.eof()) { if (i.bad()) throw runtime_error("IO流发生不可恢原创 2017-12-01 13:21:24 · 569 阅读 · 0 评论 -
c++ primer 8.2.1节练习(读文件 写文件 fstream)
8.4#include #include #include #include using namespace std;int main(){ ifstream input("F:\\test\\input.txt");//打开文档,string中有'\'应用转义双写 vector Test{}; string temp1; if (getline(input, te原创 2017-12-01 13:25:28 · 396 阅读 · 0 评论 -
c++ Primer 5 12.1.6节练习 (头文件相互包含的问题及解决办法)
头文件相互包含的解决办法原创 2017-12-01 16:51:04 · 365 阅读 · 0 评论 -
C++ primer 8.3.2节练习(有关ostringstream清空的问题)
8.13输入:电话本输出:检测后并格式化后的电话本,输出错误电话号码#include #include #include #include using namespace std;bool Valid(const string& i){ return(i.size() == 9);}string& Format(string& i){ for (auto原创 2017-12-01 15:33:50 · 551 阅读 · 0 评论 -
c++ primer 12.2.2节练习(使用allocate五步走)
12.26#include #include #include using namespace std;int main(){ /**********1.定义allocator的一个allocator对象,要有类型T********/ allocator alloc; int n = 100; /**********2.allocate(n)分配n个为初始化的内存***原创 2017-12-03 15:57:42 · 384 阅读 · 0 评论 -
c++ primer 13.13练习(构造,拷贝,析构函数何时发生详解)
13.13理解构造函数,拷贝构造函数,析构函数何时发生,最简单的一个办法就是写一个很简单的类,观察在几个情况下是否发生。#include #include #include #include using namespace std;struct Test{ Test(){ cout << "构造函数" << endl; } Test(const Test &){ cou原创 2017-12-04 15:14:49 · 361 阅读 · 0 评论 -
c++ primer 13.22练习(值行为的类)
13.22#include #include #include #include using namespace std;class HashPtr{public: HashPtr(const string& s=string()) :ps(new string(s)), i(0) {} //定义值行为的拷贝构造函数 HashPtr(const HashPtr& h) :p原创 2018-01-17 13:41:17 · 321 阅读 · 0 评论 -
c++ primer 13.26 指针行为的类
13.26#include #include #include #include #include "StrBlob.h"#include "StrBlobPtr.h"using namespace std;class HashPtr{public: HashPtr(const string& s = string()) :ps(new string(s)), i(0),u原创 2018-01-17 13:43:55 · 300 阅读 · 1 评论 -
c++ primer 13.1.4节练习(合成的拷贝构造函数与自定义拷贝函数区别)
13.1.4节练习目的:Numbered类使每个对象都有唯一的标号情况1:使用合成的拷贝构造函数#include #include #include #include using namespace std;class Numbered{public: Numbered(){ mySeq = num++; }//构造函数 int mySeq; //Numbere原创 2017-12-04 16:27:41 · 575 阅读 · 0 评论 -
c++ primer 类中函数调用过程解析(虚函数与作用域) P550疑问解析
函数调用过程即c++ primer 第五版中P549所谈,这里强调几点。 假定是p->mem() 或者obj.mem()首先确定静态类型,即p的类型。然后在这个静态类型中名字查找函数,即查找有没有mem。即 名字查找。若没有则去静态类型(即p的类型)的直接基类中找,仍没有继续向基类中找。直到基类顶端。仍没有的话,就返回查找失败,发生错误。若找到了,接着类型检查,如果类型匹配...原创 2018-03-29 21:58:35 · 263 阅读 · 1 评论 -
C++ primer 当vector中的push_back遇到移动构造函数会发生什么
首先仔细阅读 C++ primer 第五版 P474 Note下面的一段话意思是说当我们在类中定义了移动构造函数的时候,假设这个移动构造函数是noexcept的,类似对应StrVec类的操作,vector可能会重新分配内存,也就是说会将元素从旧空间移动到新内存中去。这个过程中就会发生移动构造函数注意 这里要注意一点,C++11规定的是移后源对象是一定要处于可析构的状态,意思是说移后源对象有可能执行原创 2018-03-20 21:48:04 · 1376 阅读 · 1 评论 -
c++primer 6.3.3节练习(返回数组指针的4个方法)
#include #include #include using namespace std;/*返回数组指针或者引用的4种方法*//*返回数组指针,方法1,类型别名*/using arryS = string[10];arryS& func1();/*返回数组指针,方法2,声明函数(较为繁琐,不易理解)*/string(&func2())[10];/*返回数组指针,方原创 2017-10-11 19:45:10 · 304 阅读 · 0 评论 -
C++ primer6.21练习题(产生随机数的相关知识)
#include #include #include#includeusing namespace std;int MaxNum(const int xValue, const int *yValue) //有可能后面的int*指针是一个数组的首元素指针{ return xValue > *yValue ? xValue : *yValue;}int main(){原创 2017-10-10 17:01:59 · 378 阅读 · 0 评论 -
C++ primer第五版 7.1.3节练习
定义Sales_data的头文件#ifndef CH6_H_INCLUDED#define CH6_H_INCLUDED#include #include using namespace std;struct Sales_data{ string isbn() const { return bookNo; } Sales_data& Combine(const Sales_da原创 2017-10-18 20:22:39 · 329 阅读 · 0 评论 -
C++primer 第五版课后练习(二)auto与const 引用和指针
P62页 auto const的联系具体看代码情况吧#include #include "Sales_item.h"int main(){ /**********练习2.33 2.44**********/ int i = 0, &r = i;//r是整型引用 const int ci = i, &cr = ci; auto a = r;//将i的值给整型a aut原创 2016-12-09 11:13:40 · 797 阅读 · 0 评论 -
auto 和 decltype的区别
auto和decltype都是类型推断的两种方式,但之间又有区别。主要有这几个方面的区别:1.auto是通过编译器计算变量的初始值来推断类型的,decltype同样也是通过编译器来分析表达式进而得到它的类型,但是它不用将表达式的值计算出来。2.编译器推断出来的auto类型有可能和初始值类型不同,比如对于顶层const,auto会忽略顶层const性质,但是decltype是保留下来的,原创 2016-12-09 15:36:30 · 1749 阅读 · 0 评论 -
getline中的ctrl+z的问题
#include #include using namespace std;int main(){ string world; while (getline(cin, world)) cout << world << endl; system("pause"); return 0;}getine(cin,s)是将cin中的一行(以回车换行为结束标志)赋给s,返回cin原创 2017-03-04 15:32:01 · 742 阅读 · 0 评论 -
用迭代器实现二分查找&3.4.2练习(迭代器中.end()的注意事项)
1.用迭代器实现二分查找#include #include #include using namespace std;int main(){ int cnt = 0; vector s1 {"a","b","c","d","e","f","g","h","i","j","k","l","m"}; auto beg = s1.begin(), end = s1.end();原创 2017-03-20 15:49:23 · 513 阅读 · 0 评论 -
C++ primer 3.2.3节练习
3.2.3节练习原创 2017-03-06 21:38:18 · 895 阅读 · 0 评论 -
C++ primer 5 3.3.2&3节练习
课后编程练习原创 2017-03-08 19:52:32 · 393 阅读 · 0 评论 -
C++ primer 5 3.4.1节练习(cin和getline对于“回车”的处理)
主要发现cin和getline对于“回车”键的处理方式不同!原创 2017-03-14 22:00:39 · 745 阅读 · 0 评论 -
C++ primer 5 3.5.3节3.36(彩票数组判断是否相等)
3.36生活应用中是判断彩票是否中奖。#include #include #include #include using namespace std;int main(){ const int sz = 5;//首先用const int定义固定数组长度 int rdm[sz] = {}, result[sz] = {},i=0; srand((unsign原创 2017-03-20 21:53:17 · 357 阅读 · 0 评论 -
C++ primer 5 3.6节练习:遍历二维数组的各种形式
对于二维数组的遍历,范围for,类型别名,指针操作,下标操作等遍历方法原创 2017-04-04 22:05:54 · 312 阅读 · 0 评论 -
4.7 4.9 节练习
练习 4.21#include #include #include using namespace std;int main(){ vector pool; int i; while (cin >> i) pool.push_back(i); for (auto &a : pool) { a = (a % 2 == 0) ? a : 2*a; c原创 2017-04-18 15:39:31 · 315 阅读 · 0 评论 -
C++ primer 5 5.41节练习
练习5.14#include #include #include using namespace std;int main(){ int cnt = 1, cnt1 = 1; string rcd = "", rcd1 = ""; vector test; string a = ""; while (cin >> a) test.push_back(a);//原创 2017-04-18 21:23:29 · 302 阅读 · 0 评论 -
5.3.1节练习
练习5.5#include #include #include using namespace std;int main(){ const vector lev = { "A++", "A", "B", "C", "D", "F" ,"成绩无效"}; vector scores; vector ltrscr; int a = 0; string b = "";原创 2017-04-18 16:15:37 · 257 阅读 · 0 评论 -
5.4.2节练习--- c++中多次cin会出现的问题。
练习5.17#include #include #include using namespace std;int main(){ vector num1, num2; int a = 0,b=0; cout << "请输入num1的元素:" << endl; while (cin >> a) num1.push_back(a); cin.clear(); c原创 2017-04-19 20:42:52 · 620 阅读 · 0 评论 -
C++ Primer 第五版 5.51节&5.6.3节练习
5.20#include #include #include using namespace std;int main(){ string words; string test = ""; bool flag = true; while (cin>>words&&!words.empty()) { test = words; cin >> words;原创 2017-06-01 10:55:18 · 525 阅读 · 0 评论 -
C++ primer 详解派生类向基类转换的可访问性
派生类向基类转换的可访问性c++ primer第五版 P5441.首先谈谈protected作为成员访问符的一个情况。对类用户不可访问。对派生类的成员和友元可以访问。最重要的一条性质 派生类的成员及友元只能通过派生类对象去访问基类的protected成员,而不能通过基类对象去访问基类的proteced成员。 即如下代码: “` class Base { public...原创 2018-03-28 16:39:12 · 428 阅读 · 0 评论