C++ Primer plus 第四章

4.1 arrayone.cpp

代码

#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 = "
		<< yams[0] + yams[1] + yams[2] << endl;

	cout << "The package with " << yams[1] << " yams costs "
		<< 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
		<< " bytes.\n";
	cout << "Size of one element = " << sizeof yams[0]
		<< " bytes.\n";

	return 0;
}

结果

在这里插入图片描述

知识点

1.数组的初始化
2.数组元素通过下标取得
3.sizeof 数组名 = 总字节(整个数组的长度)
sizeof 单个数组元素 = 每个数组元素字节数(单个元素的长度)
补充:数组元素个数 = sizeof 数组名/sizeof 单个数组元素

4.2 string.cpp

代码

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	const int Size = 15;
	
	//字符数组(字符串)
	char name1[Size];
	char name2[Size] = "C++owboy";

	cout << "Howdy! I'm " << name2;
	cout << "! What's your name?\n";
	cin >> name1;
	cout << "Well, " << name1 << ", your name has ";
	cout << strlen(name1) << " letters and is stored\n";
	cout << "in an array of " << sizeof(name1) << " bytes.\n";
	cout << "Your initial is " << name1[0] << ".\n";
	name2[3] = '\0';
	cout << "Here are the first 3 characters of my name: ";
	cout << name2 << endl;
	

	return 0;
}

结果

在这里插入图片描述

知识点

1.sizeof()函数求数组长度arr_length
2.strlen()函数求字符串长度str_length,注意不是数组长度

4.3 instr1.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	const int ArSize = 20;
	char name[ArSize];
	char dessert[ArSize];

	cout << "Enter your name: \n";
	cin >> name;
	cout << "Enter your favorite dessert:\n";
	cin >> dessert;
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";


	return 0;
}

结果

在这里插入图片描述

知识点

1.字符串的输入(通过cin)

4.4 instr2.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	const int ArSize = 20;
	char name[ArSize];
	char dessert[ArSize];

	cout << "Enter your name: \n";
	cin.getline(name, ArSize);
	cout << "Enter your favorite dessert:\n";
	cin.getline(dessert, ArSize);
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";

	return 0;
}

结果

在这里插入图片描述

知识点

  1. cin.getline()函数读取整行

4.5 instr3.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	const int ArSize = 20;
	char name[ArSize];
	char dessert[ArSize];

	cout << "Enter your name: \n";
	cin.get(name, ArSize).get();
	cout << "Enter your favorite dessert:\n";
	cin.get(dessert, ArSize).get();
	cout << "I have some delicious " << dessert;
	cout << " for you, " << name << ".\n";

	return 0;
}

结果

在这里插入图片描述

知识点

1.cin.get()函数

4.6 numstr.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	cout << "What year was your house built?\n";
	int year;
	cin >> year;
	
	cout << "What is its street address?\n";
	char address[80];
	cin.getline(address, 80);
	
	cout << "Year build: " << year << endl;
	cout << "Address: " << address << endl;
	
	cout << "Done!\n";

	return 0;
}

结果

在这里插入图片描述
结果中没有输入address就结束了,解决方法:在
cin >> year;后面加入cin.get();
在这里插入图片描述

知识点

1.cin.get()

4.7 strtype1.cpp

代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	cout << "Enter a kind of feline: ";
	cin >> charr1;

	cout << "Enter another kind of feline: ";
	cin >> str1;

	cout << "Here are some felines:\n";
	cout << charr1 << " " << charr2 << " "
		<< str1 << " " << str2 << endl;

	cout << "The third letter in " << charr2 << " is "
		<< charr2[2] << endl;
	cout << "The third letter in " << str2 << " is "
		<< str2[2] << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.C++中有string类型数据,与C中字符数组功能相同。

4.8 strtype2.cpp

代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1 = "penguin";
	string s2, s3;

	cout << "You can assign one string object to another: s2 = s1\n";
	s2 = s1;
	cout << "s1: " << s1 << ", s2: " << s2 << endl;

	cout << "You can assign a C-style string to a string object.\n";
	cout << "s2 = \"buzzard\"\n";
	s2 = "buzzard";
	cout << "s2: " << s2 << endl;

	cout << "You can concatenate strings: s3 = s1 + s2\n";
	s3 = s1 + s2;
	cout << "s3: " << s3 << endl;

	cout << "You can append strings.\n";
	s1 += s2;
	cout << "s1 += s2 yields s1 = " << s1 << endl;

	s2 += " for a day";
	cout << "s2 += \" for a day\" yields s2 =" << s2 << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.string的赋值(=)、拼接(+)、附加(+=)

4.9 strtype3.cpp

代码

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	str1 = str2;//复制字符串str2给str1
	strcpy_s(charr1, charr2);//复制字符数组charr2给charr1

	
	str1 += " paste";//将" paste"附加到str1
	strcat_s(charr1, " juice");//将" juice"附加到charr1

	int len1 = str1.size();//求字符串长度
	int len2 = strlen(charr1);//求字符数组长度

	cout << "The string " << str1 << " contains "
		<< len1 << " characters.\n";

	cout << "The string " << charr1 << " contains "
		<< len2 << " characters.\n";

	return 0;
}

结果

在这里插入图片描述

知识点

1.str复制 附加 求字符串长度

4.10 strtype4.cpp

代码

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
	char charr[20];
	string str;

//字符串长度
	cout << "Length of string in charr before input: "
		<< strlen(charr) << endl;
	cout << "Length of string in str before input: "
		<< str.size() << endl;

//str io
	cout << "Enter a line of text:\n";
	cin.getline(charr, 20);
	cout << "You entered: " << charr << endl;
	
	cout << "Enter another line of text:\n";
	getline(cin, str);
	cout << "You entered: " << str << endl;
	
	cout << "Length of string in charr after input; "
		<< strlen(charr) << endl;
	cout << "Length of string in str after input: "
		<< str.size() << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

4.11 structur.cpp

代码

#include <iostream>

using namespace std;

//结构的定义
struct inflatable
{
	char name[20];
	float volume;
	double price;
};

int main()
{
     //结构的使用
	inflatable guest =
	{
		"Glorious Gloria",
		1.88,
		29.99
	};

	inflatable pal =
	{
		"Audacious Arthur",
		3.12,
		32.99
	};

	cout << "Expand your guest list with " << guest.name;
	cout << " and " << pal.name << "!\n";
	cout << "You can have both for $";
	cout << guest.price + pal.price << "!\n";


	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.结构struct的定义和使用(实例)
注意:定义时花括号内用的分号,使用时用的逗号

4.12 assgn_st.cpp

代码

//4.12 assgn_st.cpp

#include <iostream>

using namespace std;

struct inflatabel
{
	char name[20];
	float volume;
	double price;
};

int main()
{
	inflatabel bouquet =
	{
		"sunflowers",
		0.20,
		12.49
	};

	inflatabel choice;
	cout << "bouquet: " << bouquet.name << " for $";
	cout << bouquet.price << endl;

	choice = bouquet;
	cout << "choice: " << choice.name << " for $";
	cout << choice.price << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.结构体的取值与赋值

4.13 arrstuc.cpp

代码

#include <iostream>

using namespace std;

struct inflatable
{
	char name[20];
	float volume;
	double price;
};

int main()
{
	//结构数组
	inflatable guests[2] =
	{
		{"Bambi",0.5,21.99},
		{"Godzilla",2000,565.99}
	};

	cout << "The guests " << guests[0].name << " and " << guests[1].name
		<< "\nhave a combined volume of "
		<< guests[0].volume + guests[1].volume << " cubic feet.\n";

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.结构数组

4.14

代码

// 4.14 address.cpp

#include <iostream>

using namespace std;

int main()
{
	int donuts = 6;
	double cups = 4.5;

	cout << "donuts value = " << donuts;
	cout << " and donuts address = " << &donuts << endl;
	cout << "cups value = " << cups;
	cout << " and cups address = " << &cups << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.取地址:&

4.15

代码

//4.15 pointer.cpp

#include <iostream>

using namespace std;

int main()
{
	int updates = 6;
	int* p_updates;
	p_updates = &updates;

	cout << "Values: updates = " << updates;
	cout << ", *p_updates = " << *p_updates << endl;//取指针指向的地址的值

	cout << "Addresses: &updates = " << &updates;
	cout << ", p_updates = " << p_updates << endl;

	*p_updates = *p_updates + 1;
	cout << "Now updates = " << updates << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.指针声明和赋值
2.指针指向地址的值 :*指针

4.16 init_ptr.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	int higgens = 5;
	int* pt = &higgens;

	cout << "Value of higgens = " << higgens
		<< "; Address of higgens = " << &higgens << endl;
	cout << "Value of *pt = " << *pt
		<< "; Value of pt = " << pt << endl;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.指针的初始化

4.17

代码

//4.17 use_new.cpp

#include <iostream>

using namespace std;

int main()
{
	int nights = 1001;//整型变量
	int* pt = new int;//new为int开辟一个内存,返回内存地址的指针
	*pt = 1001;//指针指向地址的值

	cout << "nights value = ";
	cout << nights << ": location " << &nights << endl;
	cout << "int ";
	
	cout << "value = " << *pt << ": location = " << pt << endl;
	double* pd = new double;//new为double开辟一个内存,返回内存地址的指针
	*pd = 10000001.0;

	cout << "double ";
	cout << "value = " << *pd << ": location = " << pd << endl;
	cout << "location of pointer pd: " << &pd << endl;
	cout << "size of pt = " << sizeof(pt);
	cout << ":size of *pt = " << sizeof(*pt) << endl;
	cout << "size of pd = " << sizeof pd;
	cout << ": size of *pd = " << sizeof(*pd) << endl;
  
    delete pt;
    delete pd;
    
	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.new 开辟内存,返回值为指针
2.delete 释放内存
3.有new的地方就要用delete
补充:delete释放指针数组,delete [] p

4.18 arraynew.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	double* p3 = new double[3];//动态数组
	p3[0] = 0.2;
	p3[1] = 0.5;
	p3[2] = 0.8;

	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 + 1;//数组名表示第一个元素的地址,+1表示地址的偏移,指向第二个元素
	cout << "Now p3[0] is " << p3[0] << " and ";
	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 - 1;
	delete[] p3;

	cin.get();
	return 0;

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.new开辟数组内存
2.数组名表示数组第一个元素的地址
3.指针偏移

4.19 addpntrs.cpp

代码

#include <iostream>

using namespace std;

int main()
{
	//初始化两个数组
	double wages[3] = { 10000.0, 20000.0, 30000.0 };
	short stacks[3] = { 3, 2, 1 };

	//初始化两个指针
	double* pw = wages;//数组名表示第一个元素的地址
	short* ps = &stacks[0];//用&取第一个元素的地址

	//wages数组
	//pw:指针的值(地址),*pw:指针指向地址的内容
	cout << "pw = " << pw << ", *pw = " << *pw << endl;
	//指针加1,指向数组下一个元素,即第二个元素20000.0
	pw = pw + 1;
	cout << "add 1 to the pw pointer:\n";
	//第二个元素的地址及元素值
	cout << "pw = " << pw << ", *pw = " << *pw << "\n\n";

	//stacks数组
	cout << "ps = " << ps << ", *ps = " << *ps << endl;
	ps = ps + 1;//指向第二个元素
	cout << "add 1 to the pw pointer:\n";
	cout << "ps = " << ps << ", *ps = " << *ps << endl<<endl;

	cout << "access two elements with array notation\n";
	//通过下标取数组的值
	cout << "stack[0] = " << stacks[0]
		<< ", stacks[1] = " << stacks[1] << endl;

	cout << "access two elements with pointer notation\n";
	//通过指针取数组的值,取第一个和第二个元素值
	cout << "*stacks = " << *stacks
		<< ", *(stacks + 1) = " << *(stacks + 1) << endl;

	cout << sizeof(wages) << " = size of wages array\n";
	cout << sizeof(pw) << " = size of pw pointer\n";

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.数组初始化
2.指针初始化
3.数组名与指针
4.获得数组元素的两种方法:下标和指针

4.20 ptrstr.cpp

代码

#define _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	char animal[20] = "bear"; //字符数组
	const char* bird = "wren";//指针常量
	char* ps;                 //声明指针

	cout << animal << " and "; //数组名=指向第一个元素的指针
	cout << bird << "\n";

	cout << "Enter a kind of animal: ";
	cin >> animal;
	ps = animal;//ps指针赋值,指向animal数组第一个元素
	cout << ps << "!\n";

	cout << "Before using strcpy():\n";
	cout << animal << " at " << (int*)animal << endl;
	cout << ps << " at " << (int*)ps << endl;

	ps = new char[strlen(animal) + 1];//开辟内存空间
	strcpy(ps, animal);

	cout << "After uding strcpy():\n";
	cout << animal << " at " << (int*)animal << endl;
	cout << ps << " at " << (int*)ps << endl;

	delete[] ps;//释放数组指针内存空间

	cin.get();
	return 0;
}

结果

在这里插入图片描述

知识点

1.字符串与指针
2.delete释放数组的内存

4.21 newstrct.cpp

代码

#include <iostream>

using namespace std;

struct inflatable
{
	char name[20];
	float volume;
	double price;
};

int main()
{
	inflatable * ps = new inflatable;//给struct分配内存
	
	cout << "Enter name of inflatable item: ";
	cin.get(ps->name, 20);
	cout << "Enter volume in cubic feet: ";
	cin >> (*ps).volume;
	cout << "Enter price: $";
	cin >> ps->price;

	//ps 指向结构的指针,*ps指向的值即结构本身
	//(*ps).name表示结构的name成员
	cout << "Name: " << (*ps).name << endl;

	//箭头成员运算符 ->,ps指向结构的指针
	//ps->volume被指向结构的volume成员
	cout << "Volume: " << ps->volume << " cubic feet\n";
	cout << "Price: " << ps->price << endl;
	
	delete ps;//释放内存

	return 0;
}

结果

在这里插入图片描述

知识点

1.给struct分配和释放内存
2.使用指针获取结构成员
3.使用箭头运算法获取结构成员

4.22 delete.cpp

代码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>

using namespace std;

//声明指针函数,返回的是指针类型
char* getname(void);

int main()
{
	char* name;//定义一个字符指针

	name = getname();//调用指针函数,
	cout << name << " at " << (int*)name << "\n";
	delete[] name;

	name = getname();
	cout << name << " at " << (int*)name << "\n";
	delete[] name;

	return 0;
}

//该函数作用就是将字符串重新分配内存,减少内存消耗
char* getname()
{
	char temp[80];//字符串
	cout << "Enter last name: ";
	cin >> temp;
	//为字符串分配内存,pn指向字符串
	char* pn = new char[strlen(temp) + 1];//1表示空字符
	//将字符串temp复制给pn
	strcpy(pn, temp);
	return pn;
}

结果

在这里插入图片描述

知识点

1.指针函数的声明、定义和使用
2.new和delete

4.23 mixtypes.cpp

代码

#include <iostream>

using namespace std;

struct antarctica_years_end
{
	int year;
};

int main()
{
	//结构体
	antarctica_years_end s01, s02, s03;
	s01.year = 1998;

	//指向结构体的指针
	antarctica_years_end* pa = &s02;
	pa->year = 1999;

	//结构数组
	antarctica_years_end trio[3];
	trio[0].year = 2003;
	cout << trio->year << endl;

	const antarctica_years_end* arp[3] = { &s01,&s02,&s03 };
	cout << arp[1]->year << endl;

	//二级指针
	const antarctica_years_end ** ppa = arp;
	cout << (*ppa)->year << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.混合数据类型

4.24

代码

//4.24 choices.cpp

#include <iostream>
#include <vector>
#include <array>

using namespace std;

int main()
{
	double a1[4] = { 1.2,2.4,3.6,4.8 };

	vector<double> a2(4);
	a2[0] = 1.0 / 3.0;
	a2[1] = 1.0 / 5.0;
	a2[2] = 1.0 / 7.0;
	a2[3] = 1.0 / 9.0;

	array<double, 4>a3 = { 3.14,2.72,1.62,1.41 };
	array<double, 4>a4;
	a4 = a3;

	cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
	cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
	cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

	a1[-2] = 20.2;//表示a1所指地方向前移动2个元素,并将20.2存储
	cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
	cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

	return 0;
}

结果

在这里插入图片描述

知识点

1.数组,vector,array等访问元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值