VS 2022 C++ custom header file example

Foreword:

The blogger recently switched from VS Code to VS 2022, but found that the customization method is somewhat different from VS Code, so I published a blog on custom header files for VS 2022. Time is short, please feel free to enlighten me.

How to contact me? [email protected]

text:

Reference: Microsoft C++, C, and Assembler Documentation

Taking "lnode.h" as an example, you need to ensure that the header file and source file named "lnode" are created in both the header file and the source file;

lnode.h example:

#pragma once
#include <iostream>
#include <vector>

const int MaxSize = 1e3 + 10;
typedef int ElemType;

typedef struct LNode
{
    ElemType data;
    struct LNode* next;
} LNode;

void CreateLNodeRear(LNode*& L, ElemType a[], int n);
void InitLNode(LNode*& L);
void DestroyLNode(LNode*& L);
bool IfEmpty(LNode* L);
int LNodeLen(LNode* L);
void DisplayLNode(LNode* L);
bool GetElem(LNode* L, int i, ElemType& e);
int LocateElem(LNode* L, ElemType e);
bool InsertLNode(LNode*& L, int i, ElemType e);
bool DeleteLNode(LNode*& L, int i, ElemType& e);

Obviously, function declarations, class definitions, and structure definitions are all written in the header file;

Note that using namespace std is best not to be written in the header file.

Recommended reading: [C++] Do not use using namespace std in header files

lnode.cpp example:

#include <iostream>
#include <vector>
#include"lnode.h"

using namespace std;
void CreateLNodeRear(LNode*& L, ElemType a[], int n)
{
    LNode* s, * rear;
    L = (LNode*)malloc(sizeof(LNode));
    rear = L;
    for (int i = 0; i < n; i++)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = a[i];
        rear->next = s;
        rear = s;
    }
    rear->next = NULL;
}

void InitLNode(LNode*& L)
{
    L = (LNode*)malloc(sizeof(LNode));
    L->next = NULL;
}

void DestroyLNode(LNode*& L)
{
    LNode* pre = L, * p = L->next;
    while (p != NULL)
    {
        free(pre);
        pre = p;
        p = pre->next;
    }
    free(pre);
}

bool IfEmpty(LNode* L)
{
    return (L->next == NULL);
}

int LNodeLen(LNode* L)
{
    LNode* p = L->next;
    int cnt = 0;
    while (p != NULL)
    {
        cnt++;
        p = p->next;
    }
    return cnt;
}

void DisplayLNode(LNode* L)
{
    LNode* p = L->next;
    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}

bool GetElem(LNode* L, int i, ElemType& e)
{
    LNode* s = L; // s指向头结点,j置0;
    int j = 0;

    if (i <= 0)
        return false;
    while (j < i && s != NULL)
    {
        j++;
        s = s->next;
    }
    if (s == NULL)
        return false;
    else
    {
        e = s->data;
        return true;
    }
}

int LocateElem(LNode* L, ElemType e)
{
    LNode* s = L->next;
    int j = 1; //数据结点指向首结点,j置为1(表明此时位置指向1)
    while (s != NULL && s->data != e)
    {
        s = s->next;
        j++;
    }
    if (s == NULL)
        return (0);
    else
    {
        return (j);
    }
}

bool InsertLNode(LNode*& L, int i, ElemType e)
{
    LNode* p = L, * s;
    int j = 0;

    if (i <= 0)
        return false;

    while (j < i - 1 && p != NULL)
    {
        j++;
        p = p->next;
    }

    if (p == NULL)
        return false;
    else
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = e;
        s->next = p->next;
        p->next = s;
        return true;
    }
}

bool DeleteLNode(LNode*& L, int i, ElemType& e)
{
    LNode* p = L, * q;
    int j = 0;
    if (i <= 0)
        return false;
    while (j < i - 1 && p != NULL)
    {
        j++;
        p = p->next;
    }
    if (p == NULL)
        return false;
    else
    {
        q = p->next;
        if (q == NULL)
            return false;
        e = q->data;
        p->next = p->next->next;
        free(q);
        return true;
    }
}

Obviously, lnode.cpp only contains the implementation process of the function, using namespace std and header files;

Note that lnode.cpp must contain:

#include"lnode.h"

main.cpp example: 

#include<iostream>
#include<vector>
#include"lnode.h"

using namespace std;

int main()
{
    LNode* L = nullptr;
h1:
    cout << "1.CreateLNodeRear;2.InitLNode;3.DestroyLNode;4.IfEmpty;5.DisplayLNode;" << endl;
    cout << "6.GetElem;7.LocateElem;8.InsertLNode;9.DeleteLNode;10.LNodeLen" << endl;

    int flag = 0;
    cin >> flag;

    if (flag == 1)
    {
        ElemType num;
        vector<ElemType> array;
        cout << "请输入待输入数组array,空格间隔,回车结束" << endl;
        while (1)
        {
            cin >> num;
            array.push_back(num);
            if (cin.get() == '\n')
                break;
        }
        int len = array.size();
        int a[MaxSize];
        for (int i = 0; i < len; i++)
            a[i] = array[i];
        CreateLNodeRear(L, a, len);
    }
    else if (flag == 2)
    {
        InitLNode(L);
    }
    else if (flag == 3)
    {
        DestroyLNode(L);
    }
    else if (flag == 4)
    {
        if (IfEmpty(L))
            cout << "Empty" << endl;
        else
            cout << "Not Empty" << endl;
    }
    else if (flag == 5)
    {
        DisplayLNode(L);
    }
    else if (flag == 6)
    {
        int i = 0, e = 0;
        cout << "请输入i的值" << endl;
        cin >> i;
        if (GetElem(L, i, e))
            cout << e << endl;
        else
            cout << "error!" << endl;
    }
    else if (flag == 7)
    {
        int e;
        cout << "请输入待查找的e" << endl;
        cin >> e;
        if (LocateElem(L, e) == 0)
            cout << "error!" << endl;
        else
            cout << LocateElem(L, e) << endl;
    }
    else if (flag == 8)
    {
        int i = 0, e = 0;
        cout << "请输入i的值" << endl;
        cin >> i;
        cout << "请输入e的值" << endl;
        cin >> e;
        InsertLNode(L, i, e);
    }
    else if (flag == 9)
    {
        int i = 0, e = 0;
        cout << "请输入i的值" << endl;
        cin >> i;
        if (!DeleteLNode(L, i, e))
            cout << "error!" << endl;
        else
            cout << "Finished." << endl;
    }
    else if (flag == 10)
    {
        cout << LNodeLen(L) << endl;
    }
    else
    {
        cout << "未识别指令" << endl;
    }

    cout << "是否继续?Y/N" << endl;
    char c;
    cin >> c;
    if (c == 'Y')
        goto h1;
    else
        return 0;
}

Obviously, main.cpp must also contain:

#include"lnode.h"

You can think of "lnode.h" as the part extracted from the main.cpp function, and lnode.cpp as the implementation part of the function.

Guess you like

Origin blog.csdn.net/m0_63478913/article/details/127560087