C++day12光速入门指南--结构体排序及内存模型

结构体排序

#include <iostream>
#include <string>
using namespace std;
struct Hero{
    string name;
    int popular;
    string pifu;
};
void bubbleSort(Hero *arr, int len){
    for(int i = 0;i < len-1 ;i++){
        for(int j = 0; j < len-i-1;j++){
            if(arr[j].popular > arr[j+1].popular){
                Hero temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}
void printData(const Hero *arr, int len){
    for (int i = 0; i < len; i++) {
        cout<< arr[i].name << "\t"<< arr[i].popular<<"\t"<< arr[i].pifu <<endl;
    }
}
int main (){
    Hero arr[5] = {
            {"鲁班", 18882, "电玩小子"},
            {"后羿", 7888, "黄金射手座"},
            {"妲己", 9999, "爱心"},
            {"孙悟空", 7867, "地狱火"},
            {"孙尚香", 6777, "时之恋人"},
            };
    bubbleSort(arr, 5);
    printData(arr, 5);
    return 0;
}
}


随机成绩版本

#include <iostream>
#include <cstring>
#include <ctime>

using namespace std;
struct Student{
    string name;
    int score ;
};
struct Teacher{
    string name;
    struct Student sArray[5];
};
void allocateSpace(Teacher tArray[], int len){
    string tName = "teacher";
    string sName = "student";
    string nameSeed = "ABCDE";
    for (int i = 0; i < len ; ++i) {
        tArray[i].name = tName + nameSeed[i];
        for (int j = 0; j < 5; ++j) {
            tArray[i].sArray[j].name = sName + nameSeed[j];
            // [0, 100) 之间的随机整数
            //  tArray[i].sArray[j].score = rand()%100;
            // [40, 100] 之间的随机整数
            tArray[i].sArray[j].score = rand()%61 + 40;
        }
    }
}
void print(Teacher tArray[], int len){
    for (int i = 0; i < len ; ++i) {
        cout << tArray[i].name << endl;
        for (int j = 0; j < 5; ++j) {
            cout << tArray[i].sArray[j].name << endl;
            cout << tArray[i].sArray[j].score << endl;
        }
        cout << "------------------------------------" << endl;
    }
}
int main() {
    // 随机数种子  #include <ctime>
      srand((unsigned int)time(NULL));

      Teacher tArray[3]; // 老师数组
      allocateSpace(tArray, 3);
      print(tArray, 3);
}


内存分区模型

C++程序在执行时,将内存大方向划分为4个区域

  • 代码区:存放函数体的二进制代码,由操作系统进行管理的
  • 全局区:存放全局变量和静态变量以及常量
  • 栈区:由编译器自动分配释放, 存放函数的参数值,局部变量等
  • 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收

内存四区意义:
不同区域存放的数据,赋予不同的生命周期, 给我们更大的灵活编程

程序运行前

在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域

代码区:

  • 存放 CPU 执行的机器指令

  • 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可

  • 代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令

全局区:

  • 全局变量和静态变量存放在此.

  • 全局区还包含了常量区, 字符串常量和其他常量也存放在此.

  • 该区域的数据在程序结束后由操作系统释放.

#include <iostream>
using namespace std;
// 全局变量 存放 全局区
int g_a = 10;
int g_b = 10;
// 全局常量
const int c_g_a = 10;
const int c_g_b = 10;
int main() {
    // 局部变量存放在栈区
    int a = 10;
    int b = 10;

    cout<< "local varible a addr = " << &a << endl;
    cout<< "local varible a addr = " << &b << endl;
    cout<< "global varible g_a addr = " << &g_a << endl;
    cout<< "global varible g_b addr = " << &g_b << endl;
    // 静态变量存放 全局区
    static int s_a = 10;
    static int s_b = 10;

    cout<< "static varible s_a addr = " << &s_a << endl;
    cout<< "static varible s_b addr = " << &s_b << endl;


    // 字符串常量在常量区
    cout<<"const string"<< &"hello"<< endl;
    cout<<"const string"<< &"hello1"<< endl;
    // 全局常量在常量区
    cout<< "global const int c_g_a addr = " << &c_g_a << endl;
    cout<< "global const int c_g_b addr = " << &c_g_b << endl;
    
    const int c_l_a = 10;
    const int c_l_b = 10;
    // 局部常量存放在栈区
    cout<< "local const int c_l_a addr = " << &c_l_a << endl;
    cout<< "local const int c_l_b addr = " << &c_l_b << endl;

}

在这里插入图片描述
总结:

  • C++中在程序运行前分为全局区和代码区
  • 代码区特点是共享和只读
  • 全局区中存放全局变量、静态变量、常量
  • 常量区中存放 const修饰的全局常量 和 字符串常量
程序运行后

栈区:

由编译器自动分配释放, 存放函数的参数值,局部变量等

注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放

#include <iostream>
using namespace std;
int * func(){
    int a = 10;
    return &a;
}
int main() {
   int *p =  func();
   cout<< p << endl;
   cout<< *p << endl;

}


堆区:

由程序员分配释放,若程序员不释放,程序结束时由操作系统回收

在C++中主要利用new在堆区开辟内存

#include <iostream>
using namespace std;
int * func(){
    int * a =new int(10); // 在堆区开辟一个int 类型指针变量并且 里面的初始值是10
    return a;
}
int main() {
   int *p =  func();
   cout<< p << endl; // 0xe16f00
   cout<< *p << endl; // 10

}

new操作符

C++中利用new操作符在堆区开辟数据

堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符 delete

语法:new 数据类型

利用new创建的数据,会返回该数据对应的类型的指针

#include <iostream>
using namespace std;
int * func(){
    int * a =new int(10); // 在堆区开辟一个int 类型指针变量并且 里面的初始值是10
    return a;
}
int main() {
   int *p =  func();
   cout<< p << endl; // 0xe16f00
   cout<< *p << endl; // 10
   // 利用delete释放堆区数据
   delete p;  // 释放空间是不可以访问的
   cout<< *p << endl; // 0xe16f00
   cout<< p << endl; // 0xe16f00
}


开辟数组

#include <iostream>
using namespace std;

int main() {
    int* arr = new int[10];
    for (int i = 0; i < 10; ++i) {
        arr[i] = i + 1000;
    }
    for (int i = 0; i < 10; ++i) {
        cout<< arr[i] <<"\t";
    }
    //释放
    delete[] arr;
}


引用

引用的基本使用

作用: 给变量起别名

语法: 数据类型 &别名 = 原名

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int &b = a;
    cout<<"a = "<< a <<endl;
    cout<<"b = "<< b <<endl;

    // 其中一个被修改, 会影响到另外一个
    b = 1000;

    cout<<"a = "<< a <<endl; // 1000
    cout<<"b = "<< b <<endl; // 1000
}
  • 引用必须初始化
  • 引用在初始化后,不可以改变
int main() {
    int a = 10;
    int &b = a;
//    int &c; // 引用必须初始化
    int &c = a; // 一旦初始化不可以更改
//    &c = a;
    c = b; // 这是赋值操作不是更改引用
    cout<<"a = "<< a <<endl;
    cout<<"b = "<< b <<endl;

    // 其中一个被修改, 会影响到另外一个
    b = 1000;

    cout<<"a = "<< a <<endl; // 1000
    cout<<"b = "<< b <<endl; // 1000
}

引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参

优点:可以简化指针修改实参

#include <iostream>
using namespace std;
// 值传递
void Swap1(int a, int b){
    int temp = a;
    a = b;
    b = temp;
}
//地址传递

void Swap2(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}
//引用传递
void Swap3(int& a, int& b){
    int temp = a;
    a = b;
    b = temp;
}
int main() {
  int a = 10;
  int b = 20;
  Swap1(a, b);
  cout<<"a = "<< a << "b = " <<b<< endl;
  Swap2(&a, &b);
  cout<<"a = "<< a << "b = " <<b<< endl;
  Swap3(a, b);
  cout<<"a = "<< a << "b = " <<b<< endl;
}

通过引用参数产生的效果同按地址传递是一样的。引用的语法更清楚简单

引用做函数返回值

#include <iostream>
using namespace std;
int& test1(){
    int a = 10;
    return a;
}
int& test2(){
    static int a = 10;
    return a;
}

int main() {
  // 不能返回局部变量的引用
//  int& ref = test1();
//  cout<<" ref = "<< ref << endl;

  int& ref = test2();
  cout<<" ref = "<< ref << endl;
}

引用的本质

本质:引用的本质在c++内部实现是一个指针常量.

#include <iostream>
using namespace std;
// c++ 中, 发现是引用, 转换成int * const ref = &a;
void func(int& ref){
    ref = 100; // ref 是引用 转换 *ref = 100
}
int main() {
    int a = 10;
    // 自动转换为 int * const ref = &a;
    // 指针常量指针的指向不可以更改, 值可以更改
    // 说明了引用是不可以更改
    int& ref = a;
    ref = 20; //  ref 是引用 转换 *ref = 20;
    cout<<" a = "<< a<< endl;
    cout<<" ref = "<< ref<< endl;
    func(a);
    cout<<" a = "<< a<< endl;
    cout<<" ref = "<< ref<< endl;
}


C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值