c++PrimerPlus第四章学习笔记(一)

一 数组

数组(array)是一种数据格式,可以存储多个同类型的值。数组申明时,需要包含以下三点:①存储数组元素值的数据类型,②数组名,③数组中元素个数,申明数组的通用格式如下: tyoeName arrayName[arraySize]。需要注意的是,数组的索引值从零开始,因而数组最后一个元素的索引值为(n-1),这里假设元素个数为n。

数组的初始化规则:

①只有在定义数组时才能使用初始化,此后就不能使用了,也不能将一个数组赋值给另外一个数组。

②初始化数组时,提供的值可以少于元素个数,此时其他元素被设置为零。

③初始化数组时,若[]内为空,则编译器将自动计算数组元素的个数,但其实这是一种比较糟糕的做法。

C++ 11数组初始化方法

①初始化数组时,可以省略‘=’。例如 int arr1[10] {11, 22, 33, 44}

②大括号可以不包含任何东西,此时所有数组元素被设置为零。 例如 int arr2[10] {}

③列表初始化禁止缩窄操作。例如 long arr3[5] {12, 22, 11.0},不允许因为浮点数转化为整型是缩窄操作。

二 字符串

字符串是存储在内存中的连续字节的一系列字符。C++处理字符串的方式有两种,一种是C-style string,另一种是基于string类库的方法。

C-风格字符串具有一个特殊的性质——以空字符(null character)结尾,空字符被写作“\0”。例如 char arr[6] {'h','h','h','h','h','\0'}就是一个char类型的字符串,当然他也是char型数组。

字符串常量(string constant),这是一种将数组初始化为字符串的方法(使用一个双引号)。例如 char arr[10] "hahahaha"。

const int Size = 20;
char name1[Size];
char name2[Size] = "C++owboy";

cout << "Hi,i'm " << name2 << " ! What's your name?\n" << endl;
cin >> name1;
cout << "Well " << name1 << ", your name has "
	<< strlen(name1) << " letters and stored in a array of "
	<< sizeof(name1) << " bytes." << endl;
// strlen() 返回储存在数组name1中的字符串的长度
// sizeof() 返回整个数组的长度
name2[3] = 0;
cout << "Here are the 3 first characters of my name: "
	<< name2 << endl;
// 修改数组name2的第四个字符为空字符(0),那么程序在读取name2数组时,在此结束。

每次读取一行的字符串函数有两个,一个是getline(),另一个是get()。两个函数都是每次读取一行输入,直到到达换行符。但是,getline()会丢弃换行符,而get()则会保留换行符。

const int Arsize = 20;
char name[Arsize];
char dessert[Arsize];

cout << "Enter your name:" << endl;
//cin >> name;
// cin 使用空白(空格,制表符,换行符)来确定用户输入的结尾
// 因此 若输入name中含有空格,那么cin 只能读取前一个单词
// 改进方法
// cin.getline(name, Arsize); //   让cin 以换行符作为输入的结尾
cin.get(name, Arsize).get();
cout << "Enter your favorite dessert:" << endl;
// cin >> dessert;
//cin.getline(dessert, Arsize);
cin.get(dessert, Arsize).get();
	 
cout << "I have some deliciours " << dessert
	<< " for you, " << name << "." << endl;

三 结构简介

using namespace std;

struct MyStruct
{
	char name[20];
	float volumn;
	double price;
};

int main()
{
        // 结构数组
	MyStruct arr1[2] = 
	{
		{"Heyuzhi",23.324,4353.43},   // first structure in array
		{"Yifanfan",23.121,234.234}   // second structure in array
	};
	cout << "The guests " << arr1[0].name << " and " << arr1[1].name
		<< " have a conbined value of "
		<< arr1[0].price + arr1[1].price << " cubic feet." << endl;
	// arr1 在本质上是数组,因此arr1.name 显然是无效的。
        
        /*
        // 初识结构
	MyStruct guest =
	{
		"Heyuzhi",    // name value
		47.24,        // volume value
		326.234       // price value
	}; // guest is a structure variable of type MyStruct
	MyStruct pal =
	{
		"Huayinger",
		33.234,
		2352.312
	};  // pal is another variable of type MyStruct
	cout << "Expand your guests list with "
		<< guest.name << " and " << pal.name << endl;
	cout << "You can have both for $"
		<< guest.price + pal.price << endl;
	// 数组可以存储同类型的多个数据,但是我们有时候希望有一种数据格式可以存储不同类型的数据
	// 结构帮助我们解决了这一需求。我们可以在同一个结构中存储多个不同类型的数据
	// 在使用结构时 我们需要先进行结构申明,一般我们把结构放在main()前面,也就是所谓的外部申明
	// 外部申明的好处是该申明的结构可以被后面的所有函数使用。
	*/

	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41950735/article/details/89317779