一、笔记
1.初始化数组 int oook[4] = {25,32,22,16}; (C++11允许 = 号省略)
(只有在定义数组时才能初始化,其他时候不能初始化,也不能将一个数组赋予给另外一个数组。)
2.strlen()只计算可见的字符。
3.读取一行字符串
cin.getline(name,20) : 丢弃换行符。其中第一个参数用来储存输入行数组的名称,第二个参数是要读取的字符数,如果此参数为20,则函数最多读取19个字符。
cin.get(name,20):不丢弃换行符。
4.String 类
4.1 不能将一个数组赋予另一个数组,但是可以将一个string对象赋予另一个string对象
4.2可以使用运算符 ”+“ ,”+=“。
4.3 以下是两种确定字符串中字符数的方法:
int len1 = str1.size();
int len2 = strlen(charr1);
其中,str1和charr1是数组名
4.4
下面是将一行输入读取到数组的代码:
cin.getling(charr, 20); 【cin是一个istream对象】
下面是将一行输入读取到string对象中的代码:
getline(cin,str);
4.5 cout << R“ /n/b ”,显示/n/b
4.6 在char数组和cstring头文件中,使用cin.getline(name,20)
在string和string头文件中,使用getline(cin.name)
5.结构 struct name
{
char name[20];
float volume;
double price;
};
初始化 name guest = {“Daphe”, 0.12 ,9.98};
6.枚举
enum spe {red, orange, yellow}; 对应0,1,2
spe band;
band = red; 【没有为枚举定义算术运算】
7.指针
7.1int jumbo = 23;
int* pe = &jumbo;
| 值 23 | 地址0x2ac8 |
|---|---|
| jumbo | &jumbo |
| *pe | pe |
【一定要在对指针应用解除引用运算符()之前,将指针初始化为一个确定的、适当的地址】
7.2 int pon = new int [10];
delete [] pon;
【只能使用delete来释放new分配的内存。】
eg.
#include<iostream>
int main()
{
using namespace std;
double* p3 = new double[3];
p3[0] = 1.1;
p3[1] = 2.2;
p3[2] = 3.3;
cout << "p3[1] is " << p3[1] << endl;
p3 = p3 + 1;
cout << "p3[0] is " << p3[0] << endl;
p3 = p3 - 1;
cout << "p3[2] is " << p3[2] << endl;
delete[]p3;
return 0;
}

8.数组的替代
8.1 vector
#include < vector >
…
using namesapce std;
vector< typeName >dv(n_elem);
【该声明创建一个名为vt的vector对象,可以储存n_elem个类型为typeName的元素,其中n_elem可以是整形常量也可以是整形变量。】
8.2 array
#include < array >
…
using namesapce std;
array< typename, n_elem> arr;
【该声明创建一个名为arr的array对象,可以储存n_elem个类型为typeName的元素,其中n_elem是整形常量。】
9.类型组合
#include<iostream>
using namespace std;
//创建结构
struct anta_end
{
int year;
};
int main()
{
//创建变量
anta_end s01, s02, s03;
//使用成员运算符访问其成员
s01.year = 1998;
//创建指向这种结构的指针
anta_end* pa = &s02;
//使用间接运算符来访问成员
pa->year = 1999;
//创建结构数组
anta_end trio[3];
//其中trio是一个数组,trio[0]是一个结构,trio[0].year 是该结构的一个成员
trio[0].year = 2003;
cout << trio->year << endl;
//创建指针数组
const anta_end* arp[3] = { &s01,&s02,&s03 };
cout << arp[1]->year << endl;
//可以创建指向上述数组的指针
const anta_end** ppa = arp;
auto ppb = arp;
cout << (*ppa)->year << endl;
cout << (*(ppb + 1))->year << endl;
return 0;
}
二、复习题答案
2.使用模板类array来声明actor是由30个char组成的数组。
答:arry<char, 30> actors;
6.声明一个char数组,并将其初始化为字符串“cheeseburger”。
7.声明一个string对象,并将其初始化为字符串“Waldor Salad”。
答:char num[] = “cheeseburger”;
string num2 = “Waldor Salad”;
8.设计一个描述鱼的结构声明。包括品种、重量、长度。
9.声明8中定义的结构的变量,并对其初始化。
15。给8中的结构动态分配内存,再读取成员值。
struct Fish {
char kind[20];
int weight;
float length;
};
Fish petes = { "little fish",12,12.2 };
Fish* pole = new Fish;
cout << "Enter kind of fish: ";
cin >> pole->kind;
10.用enum定义一个Response变量,并对其初始化。
11.假设ted是一个double变量,声明一个指向ted的指针,并使用该指针来显示ted的值。
12.假设treacle是一个包含十个元素的float数组,请声明一个指向treacle的第一个元素的指针,并使用该指针来显示数组的第一个元素和最后一个元素。
13.编写一段代码,要求用户输入一个正整数,然后创建一个动态的int数组,其中包含的元素数目等于用户输入的值,首先使用new来完成,再使用vector。
#include<iostream>
#include<vector>
int main()
{
using namespace std;
enum Response { Yes, No, Maybe }; //第10题
double ted; //第11题
double* pd = &ted;
cout << *pd << endl;
float treacle[10] = { 1.2,2.2, }; //第12题
float* pt = treacle;
cout << pt[0] << pt[9] << endl;
unsigned int n;//第13题
cin >> n;
int* num = new int[n];
vector<int> dv(n);
三、编程答案
1.编写一个C++程序,如下述输出示例所示的那样的请求并显示信息:
What is your first name?
What is your last name?
What letter grade do you deserve? B
What is your age?
Name:
Grade:C
Age:
将成绩下调一个等级。
#include<iostream>
using namespace std;
int main()
{
char f_name[50];
char l_name[50];
char grade;
int age;
cout << "What is your first name? ";
cin.getline(f_name, 50);
cout << "What is your last name? ";
cin.getline(l_name, 50);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << l_name << " , " << f_name << endl;
cout << "Grade: " << char(grade + 1) << endl;
cout << "Age: " << age << endl;
return 0;
}
2.使用string而不是char。
#include<iostream>
#include<string>
int main()
{
using namespace std;
const int ArSize = 20;
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return 0;
}
5.结构CandyBar包含三个成员。第一个成员储存了糖块的品牌,第二个重量,第三个卡路里。编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化我为“Mocha Munch”,2.3和350.初始化应在声明snack时进行。最后,显示snack变量的内容。
#include<iostream>
#include<cstring>
struct CandyBar
{
char brand[50];
float weight;
int calories;
}sanck;
int main()
{
using namespace std;
CandyBar sanck= { "Mocha Munch",2.3,350 };
cout << sanck.brand << " , " << sanck.weight << " , " << sanck.calories << endl;
return 0;
}
7.William Wingate从事披萨分析服务,对于每个披萨饼,他记录下列信息:
*披萨饼公司的名称,可以有多个单词组成
*披萨饼的直径
*披萨饼的重量
请设计一个能观储存这些信息的结构,并编写一个使用这种结构变量的程序,程序将请求用户输入上述信息,然后显示信息。请使用cin和cout。
#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
string name;
float dia;
float wei;
}pizza;
int main()
{
cout << "Enter name: ";
getline(cin,pizza.name);
cout << "\nEnter diameter: ";
cin >> pizza.dia;
cout << "\nEnter weight: ";
cin >> pizza.wei;
cout << "Your PIzza's name is: " << pizza.name << " diameter is: "
<< pizza.dia << " weight is: " << pizza.wei << endl;
return 0;;
}
8.完成7,但使用new来为结构分配内存,而不是声明一个结构变量,另外。让程序在请求输入比萨饼公司名称之前输入披萨饼的直径。
#include<iostream>
#include<string>
using namespace std;
struct Pizza
{
string name;
float dia;
float wei;
};
int main()
{
Pizza* pi = new Pizza; //将地址赋给pi指针
cout << "Enteer diameter: ";
cin >> pi->dia;
cin.get(); //去除换行符
cout << "Enter name: ";
getline(cin, pi->name);
cout << "Enter weight: ";
cin >> pi->wei;
delete pi;
return 0;
}
9.完成6,使用new
#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calories;
};
int main()
{
CandyBar* snack = new CandyBar[3]; //[3]这个很重要!!!!!
snack[0] = { "m m", 2.2, 330 };
snack[1] = { "mm m", 2.6, 365 };
snack[2] = { "mm ms", 2.7, 665 };
cout << snack[0].brand << "'s weight is " << snack[0].weight
<< ",and it include " << snack[0].calories << " caloris." << endl;
cout << snack[1].brand << "'s weight is " << snack[1].weight
<< ",and it include " << snack[1].calories << " caloris." << endl;
cout << snack[2].brand << "'s weight is " << snack[2].weight
<< ",and it include " << snack[2].calories << " caloris." << endl;
return 0;
}
10.编写一个程序,让用户输入三次40m跑的成绩,并显示次数和平均成绩。
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<double, 3>grade;
double mean;
int i = 0;
cout << "Please enter the results of three 40-meter runs: ";
while(i<3)
{
cin >> grade[i];
i++;
}
mean = (grade[1] + grade[0] + grade[2]) / 3;
cout << "The " << i << " average grades are " << mean << endl;
return 0;
}
本文详细记录了C++ Primer Plus第四章的学习笔记,涵盖数组、String类、结构、枚举、指针、动态内存、数组替代方案如vector和array的使用。此外,还提供了复习题的答案及编程练习,包括用户输入处理、结构体的声明与初始化、动态内存管理和数组操作等。

1613

被折叠的 条评论
为什么被折叠?



