c++ 学习笔记

清空结构体

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

结构体赋值问题


#pragma execution_character_set("utf-8")
#include 
#include 
#include 
#include "echoclient.h"
#include 
#include  
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
最大成员空间决定。操作的是同一空间。(类似指针))
每个成员可操作空间是由本身决定的。
c++ 学习笔记_第1张图片

引用

c++ 学习笔记_第2张图片
c++ 学习笔记_第3张图片

内联函数

  • 减少函数调用的开销。 编译阶段替换
    c++ 学习笔记_第4张图片

你可能感兴趣的:(笔记,c++,学习,开发语言)