复习书目:C++ primer Plus
编译器:VS2013
第4章 复合类型
4.1 数组
数组声明应指出以下三点:1 存储在每个元素中的值得类型;2 数组名;3 数组中的元素数。
short months[12];//元素类型为short型,数组名为months,数组中元素数为12
C++数组从0开始编号的!
#include <iostream>
using namespace std;
int main()
{
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 9;
int yamcosts[3] = { 20, 30, 5 };
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << endl;
cout << yamcosts[1] << " cents per yam.\n";
int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
total = total + yams[2] * yamcosts[2];
cout << "The total yam expense is " << total << " cents.\n";
cout << "\nSize of yams array = " << sizeof yams;
cout << " bytes.\n";
cout << "\nSize of one element = " << sizeof yams[0];
cout << " bytes.\n";
cin.get();
cin.get();
return 0;
}
数组的初始化在定义时才可以使用。
int cards[4]={3,4,5,6};
如果只对部分初始化,则将其他部分设置为0。
4.2 字符串
C++处理字符串的方式有两种。第一种来自C语言,常被称为C-风格字符串(C-style string)。另一种为string类库的方法。
C-风格字符串具有一种特殊的性质:以空字符结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾。
char dog[8] = { 'b', 'e', 'a', 'u', ' ', 'i', 'i' };
char cat[8] = { 'f', 'a', 't', '\0' };
这两个都是char数组,但只有第二个数组才是字符串。空字符对C-风格字符串而言至关重要。C++中很多字符串函数都是逐个地处理字符串中的字符,直到到达空字符为止。
初始化不必用'b'的形式,用字符串常量(字符串字面值)来初始化即可:
char bird[11]="Mr.Cheeps";char fish[]="Bubbles";
用引号括起的字符串隐式地包括结尾的空字符(会用\0自动填充)。应确保数组足够大,能够存储字符串中所有字符——包括空字符!,让编译器计算元素数目更为安全。(即采用char fish[]="Bubbles")在确定字符串所需的最短数组时,必须将空字符计算在内。
“S”是字符串常量,实际上是‘S’‘\0’,是一个数组,本身使用是指数组的地址。
‘S’是字符常量,ASCII中 83 的另一种写法。
4.2.1拼接字符串常量
会将第一部分的字符串的最后的'\0'填为第二部分的字符串。
4.2.2在数组中使用字符串
cstring库中使用strlen()来确定字符串的长度(不包含'\0')。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int Size = 15;
char name1[Size];
char name2[Size] = "C++boy";
cout << "Howdy! I am " << name2;
cout << "!What's your name?\n";
cin >> name1;
cout << "My name is " << name1 << ", your name has ";
cout << strlen(name2) << " letters and is stored\n";
cout << " in an array of " << sizeof(name2) << " bytes.\n";
name2[3] = '\0';
cout << "Here are my first 3 characters in my name: ";
cout << name2 << endl;
cin.get();
cin.get();
return 0;
}
strlen()返回的是字符串的长度,而sizeof()返回的是整个数组的长度。当name2[3]='\0'后,输出name2时,遇到'\0'就停止,故只有C++了。
4.2.3 字符串输入( 如果是数字输入,空白将保留在输入队列之中)
cin作为字符串接收,使用空白(空格,制表符,换行符)来确定字符串的结束位置。例子就是将Jack录入为一个字符串,
然后将“Chen”放在输入队列当中,准备下一次使用。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int Size = 15;
char name[Size];
char dessert[Size];
cout << "Enter your name: \n";
cin >> name;
cout << "Enter your favorate dessert: \n";
cin >> dessert;
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
cin.get();
cin.get();
return 0;
}
cin读取字符串以空白(空格、制表符、换行符)作为字符串的结束,然后将空白之后的内容作为下一次的字符串输入。
采用面向行而非面向单词的方法。getline()和get(),都是遇到换行符结束录入,getline()丢弃换行符,get()保留换行符在输入队列中。
1, 面向行的输入:getline()
getline(数组名,数组最大长度(包括\0)),键盘按回车表示录入时,数组将回车字符转换为'\0'来表示字符串的结束。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int Size = 15;
char name[Size];
char dessert[Size];
cout << "Enter your name: \n";
//cin >> name;//ctrl K C/U
cin.getline(name, Size);
cout << "Enter your favorate dessert: \n";
//cin >> dessert;
cin.getline(dessert, Size);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
cin.get();
cin.get();
return 0;
}
可以发现,getline()是遇回车结束录入的。
但getline()有一些问题,录入结束并不知道是因为遇到回车还是数组已满,于是可以使用get()函数。
2 面向行的输入:get()
读取到行尾,但并不再读取并丢弃换行符,而是将其留在输入队列中(即下次系统找录入时会发现换行符)。
cin.get(name ,Size);
cin.get(dessert, Size);
第一次调用后,换行符留在队列中,因此第二次调用时看到的第一个字符便是换行符,此时get()认为已经达到行尾,而不读取任何内容。如果不借助于帮助将无法跳过该换行符。
这种帮助方式是:cin.get(),不带任何参数的cin.get()可以读取下一个字符(换行符也是可以的)
cin.get(name, Size);
cin.get();
cin.get(dessert, Size);
或者,将两个类成员函数拼接起来(合并),如下所示:
cin.get(name,Size).get();
也可以连续录入两个数组
cin.getline(name, Size).getline(dessert, Size);
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int Size = 15;
char name[Size];
char dessert[Size];
cout << "Enter your name: \n";
//cin >> name;//ctrl K C/U
cin.get(name, Size).get();
cout << "Enter your favorate dessert: \n";
//cin >> dessert;
cin.getline(dessert, Size);//加了.get(),窗口上要敲两次回车,很奇怪
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
cin.get();
cin.get();
return 0;
}
4.2.5 混合输入字符串和数字
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
cout << "What year was your house built?" << endl;
int year;
cin >> year;
cout << "What is your name?\n";
char name[20];
cin.getline(name, 20);
cout << "Year building: " << year;
cout << "\nName: " << name << endl;
cout << "Done!\n";
cin.get();
cin.get();
return 0;
}
录入数字时,回车符会保留在输入队列中。(和录入字符串不同,字符串遇到空白停止但回车符就被替换为\0了)
解决办法就是将回车符也读一个。
(cin>>year).get();
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
cout << "What year was your house built?" << endl;
int year;
(cin >> year).get();//将输入队列中的回车符采样掉
cout << "What is your name?\n";
char name[20];
cin.getline(name, 20);
cout << "Year building: " << year;
cout << "\nName: " << name << endl;
cout << "Done!\n";
cin.get();
cin.get();
return 0;
}