引言
下面我们来看下C++中复合类型的数据使用案例。
getline()
#include <iostream>
int main() {
using namespace std;
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;
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
Enter your name:
zhangpan
Enter your favorite dessert:
打游戏,看书
I have some delicious 打游戏,看书 for you, zhangpan.
Process finished with exit code 0
string
#include <iostream>
#include <string>
#include <cstring>
int main() {
using namespace std;
string str1;
string str2 = "alspd";
char charr1[20] = "zhangpan";
char charr2[20];
strcpy(charr2, "hello");
cout << "charr2:" << charr2 << endl;
cout << "str2.size(): " << str2.size() << endl;
cout << str2 << endl;
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
charr2:hello
str2.size(): 5
alspd
Process finished with exit code 0
结构体
C++允许再声明结构体变量时省略关键字struct,C++提倡使用外部结构变量
#include <iostream>
struct inflatable
{
char name[20];
float volume;
double price;
};
int main() {
using namespace std;
inflatable guest = {
"zhangpan",
1.88,
29.99
};
inflatable pal = {
"alspd",
3.12,
32.99
};
cout << "guest.name:" << guest.name << endl;
cout << "pal.name:" << pal.name << endl;
cout << "pal.price:" << pal.price << endl;
//省略=
inflatable duck {"zhangsan", 2.3, 3.3};
inflatable mayor {};//默认哥哥成员设置为零
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
guest.name:zhangpan
pal.name:alspd
pal.price:32.99
Process finished with exit code 0
枚举
创建符号常量得方式,可以代替const
enum spectrum{red, orange, yellow, green, blue, violet, indigo, ultraviolet};
枚举量得值从0开始
指针
指针是一个变量,存储得是值的地址,而不是值本身。只需对变量应用地址运算符(&)
*运算符为解引用运算符
#include <iostream>
int main() {
using namespace std;
int updates = 6;
int * p_updates;
p_updates = &updates;
//express values two ways
cout << updates << endl;
cout << *p_updates << endl;
//express address two ways
cout << &updates << endl;
cout << p_updates << endl;
// use pointer to change value
*p_updates = *p_updates + 1;
cout << updates << endl;
return 0;
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
6
6
0x61fe14
0x61fe14
7
Process finished with exit code 0
指针危险性:一定要在对指针应用解引用运算符(*)之前,将指针初始化为一个确定的地址。
指针真正的用武之地,在于运行阶段分配未命名的内存以存储值,下面来看看
指针 new delete
只能用delete来释放使用new分配的内存。
#include <iostream>
int main() {
using namespace std;
int nights = 1001;
int * pt = new int;
* pt = 1001;
cout << nights << endl;
cout << pt << endl;
cout << *pt << endl;
delete pt;
double * pd = new double;
*pd = 1000001.0;
cout << *pd << endl;
*pd = 2000002.0;
cout << *pd << endl;
delete pd;
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
1001
0x21730
1001
1e+06
2e+06
Process finished with exit code 0
使用new来创建动态数组
格式:
int * pt = new int [10]
delete [] pt
#include <iostream>
int main() {
using namespace std;
double * p3 = new double [3];
p3[0] = 0.2;
p3[1] = 0.5;
p3[2] = 0.8;
cout << p3[1] << endl;
p3 = p3 + 1;//increment the pointer
cout << p3[1] << endl;
cout << p3[0] << endl;
}
输出:
D:\CODE\cpp\cmake-build-debug\cpp.exe
0.5
0.8
0.5
Process finished with exit code 0
结语
指针这块非常重要,后面会专门讲。