1数组逆置
2数组冒泡排序
3&取地址符
其作用就是让形参能作用到实参。
下面看一个实例
通讯录简单管理系统
#include<iostream> #include<string.h> using namespace std; #define MAX 1000 struct Person { string m_name; int m_sex; int m_age; string m_Phone; string m_Addr; }; struct AddressBook { struct Person personArray[MAX]; int size; }; void AddPerson(AddressBook* abs) { if (abs->size == MAX) { cout << "已满" << endl; } else { string name; cout << "输入姓名" << endl; cin >> name; //这里要打一遍就方便理解了,这个->即.即包含关系,翻译成 “的” abs->personArray[abs->size].m_name = name; int sex; cout << "输入性别" << endl; cout << "1男2女" << endl; cin >> sex; abs->personArray[abs->size].m_sex = sex; int age; cout << "输入年龄" << endl; cin >> age; abs->personArray[abs->size].m_age = age; int phone; cout << "输入电话" << endl; cin >> phone; abs->personArray[abs->size].m_Phone = phone; string address; cout << "输入地址" << endl; cin >> address; abs->personArray[abs->size].m_Addr = address; cout << "添加成功" << endl; abs->size++; system("pause");//按任意键继续 system("cls");//清屏的操作 } }; void Show_Person(AddressBook *abs) { if (abs->size == 0) { cout << "当前通讯录为空" << endl; } else { for (int i = 0; i < abs->size; i++) { cout << "姓名" << abs->personArray->m_name <<"/t"; cout << "年龄" << abs->personArray->m_age << "/t"; //作为一个判断当然可以,但是太麻烦了,所以一个三目运算符即可 cout << "性别" << (abs->personArray->m_sex == 1 ?"男":"女") << "/t"; cout << "电话" << abs->personArray->m_Phone << "/t"; cout << "地址" << abs->personArray->m_Addr<< "/t"; system("pause"); system("cls"); } } } //按照姓名删除需知道是否有这个人 int Exit_Person(AddressBook* abs, string name) { for (int i = 0; i < abs->size; i++) { if (abs->personArray->m_name == name) { cout << "找到了有这个人" << endl; return abs->size; } else { cout << "没有这个人" << endl; return -1; } } } void Delete_Person(AddressBook* abs,string name) { int res = Exit_Person(abs, name); if (res != -1) { for (int i = res; i < abs->size; i++) { //数组前移 abs->personArray[i] = abs->personArray[i + 1]; } abs->size--; cout << "删除成功" << endl; } else { cout << "game over" << endl; } } void ShowMenu() { cout << "1添加" << endl; cout << "2显示" << endl; cout << "3删除" << endl; cout << "4查找" << endl; cout << "5修改" << endl; cout << "6清空" << endl; cout << "7退出" << endl; }; int main() { AddressBook abs; abs.size = 0; int select; string input_name; while (true) { ShowMenu(); cin >> select; switch (select) { case 1: //只有加了取地址符&才能通过形参改变实参 AddPerson(&abs); break; case 2: Show_Person(&abs); break; case 3: cout << "输入删除姓名" << endl; cin >> input_name; Delete_Person(&abs, input_name); break; case 4: break; case 5: break; case 6: break; case 7: cout << "退出" << endl; system("pause"); return 0; break; default: break; } } system("pause"); return 0; }
4c++内存四区
程序运行前
程序运行后
下面看一个栈区错误,指针实质也是一个局部变量也是栈区
堆区
new
int *arr = new int [10];
引用
参数提高
1.函数默认参数
2.函数占位符
3.函数重载
类和对象
参看c++2
https://www.cnblogs.com/yangj-Blog/p/14224328.html