C++ Primer Plus 笔记4——复合类型
数组
- 一种存放多个同类型数据的数据结构
- 形式:Type NAME[size];
- 实例代码
#include <iostream>
using namespace std;
int main()
{
// 定义int 数组
int intArray[3];
// 数组赋值
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
// 输出
for (int i = 0; i < (sizeof(intArray) / sizeof(int)); i++)
{
cout << intArray[i] << endl;
}
system("pause");
return 0;
}
字符串
- C风格字符串(char 数组)
- 实例代码
#include <iostream>
using namespace std;
int main()
{
// 多的一位是‘\0’
char str[7] = "string"; // 字符串实际上就是指针
cout << str << endl;
cout << str[6] << endl;
cout << str[5] << endl; // \0
cout << str[4] << endl;
cout << str[3] << endl;
cout << str[2] << endl;
cout << str[1] << endl;
cout << str[0] << endl;
// 使用指针
char* pStr = (char*)"Test";
cout << pStr << endl;
system("pause");
return 0;
}
- string 字符串
- 实例代码(string 存在很多的方法可供调用)
#include <iostream>
// 包含string
#include <string>
using namespace std;
int main()
{
// 定义字符串
string str = "HelloWorld";
cout << str << endl;
system("pause");
return 0;
}
结构体
- 类似于类(Class),存放多种数据类型的数据结构。
- 实例代码
#include <iostream>
#include <string>
using namespace std;
// 创建结构体
typedef struct Student
{
string name;
int age;
char sex;
}_Student;
int main()
{
// 创建结构体对象
_Student s;
// 初始化结构体
s.name = "上官翠花";
s.age = 18;
s.sex = 'F';
// 输出信息
cout << "******INFO******" << endl;
cout << s.name << endl;
cout << s.age << endl;
cout << s.sex << endl;
system("pause");
return 0;
}
共用体
-
结构体虽然解决了存放多种不同数据类型的数据的问题,但是在现实生活中,
还存在一种,同一种数据类型在不同的场景下,数据类型也不相同,就如上面的学生结构体,
在这里是用char 表示性别,在另一处可能就是使用int等来表示,那么这种时候就可以使用共用体 -
实例代码
#include <iostream>
#include <string>
using namespace std;
// 创建结构体
typedef struct Student
{
string name;
int age;
// 普通共用体
union sex
{
char cSex;
int nSex;
} _sex;
// 匿名共用体
union
{
char c;
int n;
};
}_Student;
void print(_Student s);
int main()
{
_Student s;
s.name = "上官翠花";
s.age = 18;
s._sex.cSex = 'F';
s.c = 'A';
_Student s1;
s1.name = "欧阳铁柱";
s1.age = 19;
s1._sex.nSex = 1;
s1.n = 1;
print(s);
print(s1);
system("pause");
return 0;
}
void print(_Student s)
{
cout << "******INFO******" << endl;
cout << s.name << endl;
cout << s.age << endl;
if (s._sex.cSex == 'F' || s._sex.cSex == 'M')
{
cout << s._sex.cSex << endl;
}
else
{
cout << s._sex.nSex << endl;
}
if (s.n > 0)
{
cout << s.n << endl;
}
else
{
cout << s.c << endl;
}
}
枚举
#include <iostream>
using namespace std;
enum MyEnum
{
A, B, C, D, F='A'
};
int main()
{
MyEnum myEnum = A;
cout << myEnum << endl;
myEnum = B;
cout << myEnum << endl;
myEnum = C;
cout << myEnum << endl;
myEnum = D;
cout << myEnum << endl;
myEnum = F;
cout << (char)myEnum << endl;
system("pause");
return 0;
}
指针
- 指针是C语言最牛逼的一个机制,当然,这个机制也很好的被C++ 继承了下来,虽然C++
拥有了面向对象的机制,使用对象也可以达到使用指针的效果,而且一定作用上降低了使用指针带来的风险,
但是这并不妨碍指针的强大 - 这里只是简单介绍一下指针,之后的《C和指针》会详细介绍指针
- 概念:指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明。
- 指针变量的声明形式:
Type *point;
或者Type* point;
- 实例代码
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
// 声明指针并创建空间
int *pNum = new int;
scanf("%d", pNum);
cout << *pNum << endl;
// 创建指针指向pNum
int* pNum1 = pNum;
cout << *pNum1 << endl;
system("pause");
return 0;
}
vector 和array 类
- vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// 声明长度为10 的int 组
vector<int> vNum(10);
for (int i = 0; i<10; i++)
{
vNum[i] = rand()%10 + 1;
}
for (int i = 0; i < vNum.size(); i++)
{
cout << vNum[i] << endl;
}
system("pause");
return 0;
}
- array
#include <iostream>
#include <array>
using namespace std;
int main()
{
// 声明长度为10 的int 组
array<int, 10> intArray;
for (int i = 0; i < 10; i++)
{
intArray[i] = rand() % 10 + 1;
}
for (int i = 0; i < intArray.size(); i++)
{
cout << intArray[i] << endl;
}
system("pause");
return 0;
}