【c++ primer 5th 练习3.43】教程文章相关的互联网学习教程文章

c++ primer 5th 练习3.43【代码】

#include <iostream> usingnamespace std;int main() {int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};/* for(int (&i)[4]:a)        for(int (&j):i)cout<<j<<endl;*//* for(int i=0;i<3;i++)for(int j=0;j<4;j++)cout<<a[i][j]<<endl;*//* for(int (*p)[4]=a;p<a+3;p++)for(int *q=*p;q<*p+4;q++)cout<<*q<<endl;*//* for(auto &i:a)for(auto &j:i)cout<<j<<endl;*/for(auto p=a;p<a+3;p++)for(auto q=*p;q<*p+4;q++)cou...

C++例题练习(1)【代码】

环境:Dev-C++( Version:5.6.1)一.求2个或3个正整数中的最大数,用带有默认参数的函数实现  代码实现: 1 #include <iostream>2usingnamespace std;3int max(int num1,int num2,int num3);4int main()5{6int firstNum,secondNum,thirdNum = 0;7int maxNumber;8 cout<<"please input two integer number:(Numbers are separated by spaces)"<<endl;9 cin>>firstNum>>secondNum; 10 maxNumber = max(firstNum,secondNu...

C++primer 8.3.2节练习【代码】

练习8.13 1 #include <iostream>2 #include <fstream>3 #include <sstream>4 #include <vector>5 6usingnamespace std;7 8struct PersonInfo9{ 10string name; 11 vector<string> phones; 12}; 1314string format(string) 15{ 16return"format"; 17} 1819bool valid(string s) 20{ 21if (s != "") 22returntrue; 23else24returnfalse; 25} 2627int main() 28{ 29 ifstream in_file("people.txt"); 30if (!in_file) 31 {...

String类(C++练习二)【代码】

字符串类(String),熟悉内存管理与拷贝控制类定义#include <iostream> //#include <cstring>using std::cout; using std::cin;class String{using iterator = char *;friend std::ostream &operator<< (std::ostream &, const String &);friend std::istream &operator>> (std::istream &, String &);friend String operator + (const String &, const String &);friend String operator + (const String &, constchar *); ...

《C++ primerplus》第13章练习题【代码】【图】

1.对CD类的派生练习。基类CD类存储作者和作品号等信息,派生类Classic额外增加一格“主要作品”的信息。主函数使用拷贝构造函数、按引用传递参数的函数和指针来测试基类和派生类的功能。注意继承类和基类的权限关系、初始化成员列表的使用。class.h#ifndef _CLASS_H_ #define _CLASS_H_#include <iostream> using std::cin; using std::cout;class CD { private:char performers[20] = {}; //初始化为空,不然输出一堆烫char la...

C++primer 7.2节练习

练习7.16没有,一个类可以有0个或多个访问说明符,而且对于某个访问说明符能出现多少次也没有严格限定;定义在public说明符之后的应该是程序的接口,即是成员函数的一些声明,还有一些构造函数;定义在private后的应该是数据成员和某些成员函数的定义;练习7.17有,class和struct的区别在于两者默认的访问权限不同,class默认的访问权限是private,struct则相反;练习7.18封装的意义在于将一些成员函数的定义和数据成员隐藏起来,只...

第十九章,指针小练习(C++)

#include <iostream>int main(int argc, char** argv) {int i = 1;int *n ;n=&i;<span style="white-space:pre"> </span>//*n=i 这种赋值方式也可以std::cout<<"i为:"<<i<<std::endl;std::cout<<"n为:"<<n<<std::endl;std::cout<<"*n为:"<<*n<<std::endl; return 0; }调试截图:从调试结果可以看出,n是地址值,*是取,*n是取值;&是取,&i是取地址值。原文:http://blog.csdn.net/qingbowen/article/details/46469095

[C++]C++ Primer中练习5.11的想法【代码】【图】

https://www.cnblogs.com/DouglasLuo/p/13221356.html这道练习题要求我们使用C++统计输入的字符串中a, e, i, o, u这五个字符及其大写的数量。这里其实不难,使用switch语句对输入的字符串挨个进行判断,即可得出算出这五个字符的数量。  后面练习题要求不仅要统计a, e, i, o, u这五个字符及其大写的数量。练习题还要求统计出空格,制表符, 换行符的数量。这里我百度搜了很久都没有搜到符合我心意的答案。这道题难的地方在于,我...

复数类(C++练习一)【代码】

写一个复数类,实现基本的运算,目的熟悉封装与数据抽象。类的定义#include <iostream> #include <vector> usingnamespace std; class Complex{friend ostream & operator << (ostream & os, const Complex &); //重载输出标识符friend Complex operator + (const Complex&, const Complex &);friend Complex operator - (const Complex&, const Complex &);friend Complex operator * (const Complex&, const Complex &);frien...

C++_练习—继承_保护继承【代码】

保护继承protected:保护继承保护继承:当保护继承时,基类的公有和保护成员变成派生类的保护成员,私有成员不可直接访问! 1 #include <iostream>2 3usingnamespace std;4 5class info {6public:7void setter(int a,int b){8 num = a;9 age = b; 10 } 1112int getter(void) { 13int a = age; 14return a; 15 } 1617void pri(void) { 18 cout << num << endl; 19 cout << age << endl; 20 ...

1000道C语言和C++语言的练习题及答案 #0000-0050

汇集1000道C语言和C++语言的练习题 (例题、笔试题、编程题、算法设计题),由我亲自配上答案(点此查看)。 语法题我就不出了,那个没意思,看书就可以了。本贴汇集50题 (0000-0050),持续更新...0001. 抽签题 从10个数中随机抽取5个数。(相当于双色球抽签问题)0002. 插入排序 已有10个按增序排列好的整数1,3,5,7,9,11,13,15,17,19。要求把一个整数n插到数组中,保持增序排列。0003. 数学公式类计算题 已经圆柱体的底半...

C++primer 14.2.2节练习【代码】

练习14.9 1 istream &operator >> (istream &is, Sales_data &item)2{3double price;4is >> item.bookNo >> item.units_sold >> price;5if (is)6 item.revenue = item.units_sold * price;7else 8 item = Sales_data();9returnis; 10 }练习14.10a)将一个Sales_data的对象逐一赋值;b)类型不对,类型无法转换导致输入发生错误,Sales_data对象被重置为初始状态;练习14.11有错误,没有给出当输入错误时程序应该给...

C++primer 11.1节练习【代码】

练习11.1map他是一个有序的且元素不重样的关联容器,他存放元素的方式是以键值对的方式存放的;vector容器没有什么特别的要求练习11.2list适用于在在任何地方添加删除元素,因为他是个双向的链表;vector适用于需要随机访问的程序,只能在尾后添加元素,在其他位置添加或删除元素效率会很低;代价过大deque适用于在容器首尾添加删除元素且随机访问的程序;map是键值对的集合,他能在查找的同时进行高效的访问;set就是关键字的简单...

C++PrimerPlus 练习题 循环【代码】

第四题 单利和复利// 单利投资和复利投资 本金为100元int count = 1;// 本金int principal = 100;// A的第一年的收获float a_alone = principal * 0.1;// B第一年的收获 利息float b_double = principal * 0.05;// b 收获的总和float b_Allhavest = b_double;while (b_Allhavest < a_alone * count){b_double += b_double * 0.05;b_Allhavest += b_double;cout << "b的利息-------" << b_double << endl;count++;}int year =...

C++primer 6.7节练习【代码】

练习6.541int fun(int, int); 2 typedef int (*pf) (int, int); 3 vector<pf> a; 1int funAdd(int i, int j)2{3return i + j;4}5int funDel(int i, int j)6{7return i - j;8}9int funMul(int i, int j) 10{ 11return i * j; 12} 13int funDiv(int i, int j) 14{ 15return i / j; 16} 171819int main() 20{ 21 typedef int (*pf) (int, int); 22 vector<pf> a(2); 23 a[0] = funAdd; 24 a[1] = funDel; 25 a[2]...