c++ 学习笔记

该博客介绍了C++中结构体的使用,包括如何清空结构体,以及结构体数组的赋值、排序和显示。通过`memset`函数初始化结构体,`addBook`、`takeInfo`、`sortInfo`和`showInfo`函数展示了结构体成员的操作。此外,还提及了共用体的概念和特点。
摘要由CSDN通过智能技术生成

清空结构体

memset(&lucky, 0, sizeof(lucky))

结构体赋值问题


#pragma execution_character_set("utf-8")
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include "echoclient.h"
#include <iostream>
#include  <stdio.h>
using namespace std;
extern void log(string str);
struct Book {
    int id;
    char name[20];
    int time;

};


void addBook(Book *book){
    log(book -> name);
}
void takeInfo(Book *book) {
//    int n = sizeof(*book)/sizeof((*book)[0]);
    cout << (book + 1)->id;
    for (int i = 0; i < 3; i ++) {
        cout << "input the" << i << "book" << endl;
        cin >> (book +1) -> id >> (book +i)->name >> (book + i)-> time;
        cout << "end input the" << i << "book"<< endl;
    }

}
void sortInfo(Book *book) {
    for (int i = 0; i < 2; i ++) {
        if((book + i) ->time > (book + i + 1) -> time) {
            Book temp = *(book+i);
            *(book + i) = *(book +i +1);
            *(book+i+1) = temp;
        }
    }
}
void showInfo(Book *book) {
    for (int i = 0; i < 3; i ++) {
        cout << (book +i)->id << " " << (book +i)->name << " " << (book +i)->time << endl;
    }
}
int main(int argc, char *argv[])
{
    Book book[3] = {{123, "yolov3", 3}, {234, "yolov5", 2023}, {333, "car", 555}};
//    takeInfo(book);
    sortInfo(book);
    showInfo(book);

}

共用体

union
最大成员空间决定。操作的是同一空间。(类似指针))
每个成员可操作空间是由本身决定的。
在这里插入图片描述

引用

在这里插入图片描述
在这里插入图片描述

内联函数

  • 减少函数调用的开销。 编译阶段替换
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东哥爱编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值