数组
数组创建声明:
- 存储元素类型
- 数组名
- 数组的元素个数
#include <iostream>
using namespace std;
int main()
{
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20, 30, 5};
cout << "Total yams = ";
cout << yams[0] + yams[1] + yams[2] << endl;
cout << "The package with " << yams[1] << "yams costs ";
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 << "\n Size of yam array = " << sizeof yams << " byte.\n";
cout << "Size of one element =" << sizeof yams[0] << " byte.\n";
return 0;
}
字符串
拼接字符串
#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 << "! Whats 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] << " bytes.\n";
name2[3] = '\0';
cout << "Here are the first 3 characters of my name: ";
cout << name2 << "\n";
return 0;
}
字符串输入
#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 << " for you, " << name << endl;
return 0;
}
输出结果:
cin 使用空格来确定字符串的界,所以只能读取一个单词,之后在结尾添加空字符。
每次读取一行字符串输入
面向行的输入:getline() 是cin的一个成员函数,通过回车键来确定输出结尾。
#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 << " for you, " << name << endl;
return 0;
}
面向行的输入:get()
区别是在get()每次都读取到行尾,但是不读取和丢弃换行符
处理换行符的方法:
#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 << " for you, " << name << endl;
return 0;
}
get() 读取到空行会设置失效位,用cin.clear()来恢复输入
输入字符数比指定的多,getline() 和 get() 会把余下的输入留在输入队列中。
混合输入字符串和数字
问题代码:
#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.get(address, 80);
cout << "Year built:" << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
return 0;
}
没有输入地址的机会,cin在读取年份的时候将生成的换行符留给了之后的getline()
解决方法:添加cin.get()
#include <iostream>
using namespace std;
int main()
{
cout << "What year was your house built?\n";
int year;
cin >> year;
cin.get();
cout << "What is its street address? \n";
char address[80];
cin.get(address, 80);
cout << "Year built:" << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
return 0;
}
String类简介
“类” 表示一种数据类型。 String理解成一个种类名称。
和字符数组比较,string 声明的是一种简单变量而不是数组。
String对象和字符数组的相同点:
#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;
}
赋值,拼接,和附加
赋值:
拼接
附加:
#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 C-style string to a string object.\n";
cout << "s2 = \" buzzard\"\n";
s2 = "buzzard";
cout << "s2 = " << s2 << endl;
cout << "You can concatenate strings : s3 = s2 + s1\n";
s3 = s2 + s1;
cout << "s3 = " << s3 << endl;
cout << "Yon can append string.\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;
}
运行结果
String 类的其他操作
可以使用C语言中的函数,需要引用头文件cstring。
#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;
strcpy(charr1, charr2);
str1 += " paste";
strcat(charr1, " juice");
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;
}
运行结果:
String类的输入输出(I/O)
代码
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
string str;
char charr[20];
cout << "Length of string in charr before input: "
<< strlen(charr) << endl;
cout << "Length of string in str before input: "
<< str.size() << endl;
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;
return 0;
}
结果
结构简介
结构的使用
结构是用户定义的数据类型,结构声明定义这种类型的数据属性。
结构声明:
#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 have both for $";
cout << guest.price + pal.price << "!\n";
return 0;
}
运行结果
结构中能使用string吗?
一般可以的,如下声明是可行的
结构的其他属性
赋值:
#include <iostream>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable bouquet =
{
"sunflower",
0.20,
12.49};
inflatable choice;
cout << "bouquet: " << bouquet.name << " for $";
cout << bouquet.price << endl;
choice = bouquet;
cout << "choice: " << choice.name << " for $" << choice.price << endl;
return 0;
}
结果:
结构数组
#include <iostream>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable guest[2] =
{
{"Bambi", 0.5, 21.99},
{"Godzilla", 2000, 565.99}};
cout << "The guests " << guest[0].name << " and " << guest[1].name << "\nhave a combined volume of "
<< guest[0].volume + guest[1].volume << " cubic feet.\n";
return 0;
}
运行结果
枚举
上面语句完成了工作
枚举只定义赋值操作
枚举量属于int
设置枚举量的值
显式设置
隐式设置
枚举的取值范围
每个枚举都有取值范围,通过强制类型转化,取值范围中的int值会赋值给枚举变量。
下面的操作是合法的
指针和自由存储空间
存储需要满足
#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 << " and cups address = " << &cups << endl;
return 0;
}
运行结果
指针代码:
#include <iostream>
using namespace std;
int main()
{
int updates = 6;
int *p_updates;
p_updates = &updates;
cout << "Values: updated = " << updates << ", p_updates = "
<< *p_updates << endl;
cout << "Addresses: updated = " << &updates << ", p_updates = " << p_updates << endl;
*p_updates += 1;
cout << "Now updates = " << updates << endl;
return 0;
}
运行结果
通过指针改变了被指向的值
声明和初始化指针
初始化(两边的空格可选,看习惯)
注意
指向其他类型的指针
指针代码
#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
<< "; Address of pt = " << pt << endl;
}
结果
指针和数字
指针不能相乘
使用new来分配内存
OOP技术:在程序运行时分配内存——new操作符
方法1
方法2
两种方法都是创建了一个int变量的地址给指针。但是第二种情况可以通过higgens来访问。
pn指向的一个数据对象,所以pn指向的是内存而不是变量。他使得程序管理内存有控制权
#include <iostream>
using namespace std;
int main()
{
int *pt = new int;
*pt = 1001;
cout << "int ";
cout << "value = " << *pt << ": location = " << pt << endl;
double *pd = new double;
*pd = 1000001.0;
cout << "double ";
cout << "value = " << *pd << ": location = " << 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;
return 0;
}
结果
使用delete来释放内存
使用new之后,需要将内存归还给内存池,否则被分配的内存再也无法使用了(内存泄漏)。
使用new来创建动态数组
在运行阶段需要数组则创建他,否则不创建。这种数组为动态数组,静态数组需要编写的时候指定数组长度,动态数组运行时确定数组长度。
创建
释放
#include <iostream>
using namespace std;
int main()
{
double *p3 = new double[3];
p3[0] = 0.3;
p3[1] = 0.5;
p3[2] = 0.8;
cout << "p3[1] is " << p3[1] << ".\n";
p3 = p3 + 1;
cout << "Now p3[0] is " << p3[0] << " and "
<< "p3[1] is " << p3[1] << ".\n";
p3 = p3 - 1;
cout << "Then p3[0] is " << p3[0] << " and "
<< "p3[1] is " << p3[1] << ".\n";
delete[] p3;
return 0;
}
运行结果
指针,数组和指针算术
#include <iostream>
using namespace std;
int main()
{
double wages[3] = {1000.0, 2000.0, 3000.0};
short stacks[3] = {3, 2, 1};
double *pw = wages;
short *ps = stacks;
cout << "pw = " << pw << ", pw = " << *pw << endl;
pw += 1;
cout << " add 1 to the pw pointer: \n";
cout << "pw = " << pw << ", pw = " << *pw << endl;
cout << "ps = " << ps << ", ps = " << *ps << endl;
ps += 1;
cout << " add 1 to the ps pointer: \n";
cout << "ps = " << ps << ", ps = " << *ps << endl;
cout << "access two elements with array notation\n";
cout << "stacks[0] = " << stacks[0]
<< ", stacks[1] = " << stacks[1] << endl;
cout << "access two elements with pointer notation\n";
cout << "*stack = " << *stacks
<< ", *(stack + 1) = " << *(stacks + 1) << endl;
cout << sizeof(wages) << " = size of wages array\n";
cout << sizeof(pw) << " = size of pw pointer\n";
return 0;
}
结果
C++的数组名为数组第一个元素的地址,所以
表示将pw指向一个double类型的指针,然后初始化为wages数组中第一个元素的地址
下列有关数组和指针的语法;
数组:
指针
算术运算
区别是,sizeof对于数组名和指针的结果不同
指针和字符串
如果给cout一个地址,cout将从这个地址开始打印,直到遇到空字符。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char animal[20] = "bear";
const char *bird = "wren";
char *ps;
cout << animal << ", and " << bird << ".\n";
cout << "Enter a kind animal: ";
cin >> animal;
ps = animal;
cout << ps << "s!\n";
cout << "Before using strcpy(): \n"
<< animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
ps = new char[strlen(animal) + 1];
strcpy(ps, animal);
cout << "After using strcpy(): \n"
<< animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
delete[] ps;
return 0;
}
结果
bird 存储的是“wren” 的地址,对于cout而言,animal和bird是一样的。
使用new来创建动态结构
创建动态结构时,成员操作符“.”不能用于结构名,因为这种结构没有名称,只是直到他的地址。对于这种情况c++提供箭头操作符(->).
另外一种方式是(*ps).price。price为结构成员,ps为指向结构的指针。
#include <iostream>
#include <cstring>
using namespace std;
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
inflatable *ps = new inflatable;
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;
cout << "Name: " << (*ps).name << endl;
cout << "Volume: " << ps->volume << endl;
cout << "Price $: " << ps->price << endl;
delete ps;
return 0;
}
结果
使用new和delete的范例
如下可以节省大量内存
#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;
char *pn = new char[strlen(temp) + 1];
strcpy(pn, temp);
return pn;
}
自动存储,静态存储和动态存储
自动存储
静态存储
特点:存在程序的整个生命周期。
动态存储
标签:main,primer,cout,int,c++,char,using,include,第四章 From: https://blog.csdn.net/weixin_48442204/article/details/142025331