C++ Primer Plus 第四章復(fù)習(xí)題答案
因為期末復(fù)習(xí)周和自己的懈怠荒廢了好久。。。寒假就把這邊書看完!干了兄弟們,奧利給
沖!
4.12? 復(fù)習(xí)題
? char actor[30];????short betsie[100];????float chuck[13];????long double dipsea[64];
?arrary<char,30> actor;????array<short,100> betsie;????array<float,13>;????????????????????????? ? ??array<long double,64> dipsea;
int arr[5] {1,3,5,7,9};
int even = arr[0] + arr[4];
cout<<ideas[1]<<endl;
char str1[] = "cheeseburger";
string str2 = "Waldorf Salad";
struct fish
{
char kind[30];
int weight;
double length;
}
fish a ={"shark",100,100.1};
enum Response{No,Yes,Maybe};
double *ptr = &ted;
cout<<*ptr<<endl;
float *ptr = treacle;
cout<<*ptr<<endl;//第一個元素
cout<<*(ptr+9)<<endl;//最后一個,注意是加9
int num;
cin>>num;
int *ptr = new int [num];
vector<int> vi(num);有效,打印出該字符串首字符的地址
struct fish{char name[10];int weight;double length;};
fish *ptr = new fish;
cin>>ptr->length;
cout<<ptr->weight<<endl;
delete ptr;第二句沒有指明應(yīng)該讀取多少數(shù)據(jù)。所以address被分配的內(nèi)存可能不夠用來存放用戶輸入的數(shù)據(jù),從而導(dǎo)致數(shù)組越界,發(fā)生程序崩潰或者某些不可預(yù)知的后果。
#include <vector>
#include <string>
#include <array>
const int count = 10;
std::vector<string> vs(count);
std::array<string,10> as;

4.13 編程練習(xí)
編程練習(xí)的所有源代碼我都放在了GitHub上
https://github.com/Lemy28/CPP-primer-plus_exercise
另外發(fā)現(xiàn)了GitHub上c++進階的方向,好像是清華大佬的項目
github.com/Light-City/CPlusPlusThings
我自己是看完每一章都會看一下里面的內(nèi)容的,感興趣的話家人們也可以去看一看。