c++ 学习笔记之数组

数组指针

 int arr[4] = {1, 2, 3, 4};
    int i;
    int *p = arr;
    int n = sizeof(arr)/sizeof(arr[0]);
    for (i =0; i < n; i ++) {
        cout << *(p+i) << endl;
    }
    cout << 1[arr] << endl;
    count << *(1 + arr) << endl;
    [] 是*() 的缩写, [] 左边的值,放在+ 号左边, []里面的值, 放在 右边

存储区

在这里插入图片描述

字符串指针

在这里插入图片描述
一个是栈区,一个是常量区

 char *str = (char *)"hello word";

    cout << str[4] << endl;

    char *p = str;

    cout << sizeof (p) << endl;

    str[4] = 'h';
    cout << str << endl;

指针数组

  int a = 0;
   int b = 1;
   int c = 2;
   int *arr[3] = {&a, &b, &c};
   cout << arr[0] << endl;
   cout << *arr[0] << endl;
   cout << *arr[0]+ 1 << endl;

字符串数组

char *arr[4] = {"hahaha", "hehehe", "xixixi", "lalala"};
   cout << arr[0] << endl;
   cout << *arr << endl;
   cout << arr[0]+ 1 << endl;

在这里插入图片描述

多级指针

 int a = 0;
    int *p = &a;
    int **q = &p;

   cout << *q << endl; //p的值
   cout << **q << endl; // a的值
   cout << q << endl; //p的地址

   cout << p << endl;
}

在这里插入图片描述

数组指针

  • 对数组首元素地址, &arr[0] arr + 1 跳过一个元素
  • 对数组首地址, &arr arr +1 跳过一整个数组
    在这里插入图片描述


    int (*p)[3] = NULL;
    int arr[3] = {5, 2, 3};
    p = &arr;
    cout << sizeof(*p) << endl;
    cout << *(*p + 1) << endl;

在这里插入图片描述
在这里插入图片描述
https://www.bilibili.com/video/BV1gS4y1872b/?p=75&vd_source=c7a17cdbe5b7a027401c329724b632e9

在这里插入图片描述

初始化列表

构造函数是 对象成员的-》 类对象
在这里插入图片描述

单例写法

#include <iostream>
#include <string>
using namespace std;

void log(string str) {

    cout << str << endl;
}

class Data {
    private:
        static Data *const p;
        Data(){};
    public:
        int a;
        static int b;
        static Data * _instance();
        void show();


};
Data * const Data::p = new Data;

Data * Data::_instance(){
    return Data::p;
}
void Data::show(){
    cout << "show singleTon"<< endl;
}


友元



#include <iostream>
#include  <stdio.h>
#include "link.h"
#include "log.cpp"
#include <string.h>
using namespace std;

//friend
class Room{
    friend void enterRoom(Room &r);
private:
    void bedRoom();
    string bedName;
public:
    void setingRoom();

    string setName;
    Room(string bed, string setName) {
        this->bedName = bed;
        this->setName = setName;
    };
};
void enterRoom(Room &room){
    cout << "into" << room.bedName << endl;
}
int main(int argc, char *argv[])
{
   Room room("bed","seting");
    enterRoom(room);
}


  • 不能被继承
  • 单向的关系,A 是B的友元, B 不一定是A的
  • 不能传递
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东哥爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值