C++基础项目结构

以排序算法讲解C++基础项目结构

目录

项目结构建议:

[1. SortAlgorithms.h​ (头文件 - 函数声明)](#1. SortAlgorithms.h (头文件 - 函数声明))

[2. SortAlgorithms.cpp​ (源文件 - 函数定义)](#2. SortAlgorithms.cpp (源文件 - 函数定义))

[3. main.cpp​ (主程序文件)](#3. main.cpp (主程序文件))

[Visual Studio中的文件组织:](#Visual Studio中的文件组织:)

文件结构:

[在Visual Studio中创建步骤:](#在Visual Studio中创建步骤:)

文件类型的说明:

头文件保护(#ifndef/#define/#endif))


典型的C++项目结构,按照Visual Studio的习惯,可以将代码分为头文件(.h)和源文件(.cpp)。

项目结构建议:

1. SortAlgorithms.h​ (头文件 - 函数声明)

复制代码
#ifndef SORTALGORITHMS_H
#define SORTALGORITHMS_H

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

// 函数声明
void insertionSortWithSteps(vector<int>& arr);

#endif // SORTALGORITHMS_H

2. SortAlgorithms.cpp​ (源文件 - 函数定义)

复制代码
#include "SortAlgorithms.h"

// 直接插入排序函数定义(带步骤输出)
void insertionSortWithSteps(vector<int>& arr) {
    int n = arr.size();
    
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        
        cout << "\n第" << i << "轮: 插入 " << key << endl;
        cout << "  当前状态: ";
        for (int k = 0; k < n; k++) {
            if (k == i) cout << "| ";
            cout << arr[k] << " ";
        }
        cout << endl;
        
        // 移动元素
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        
        // 插入元素
        arr[j + 1] = key;
        
        cout << "  插入后: ";
        for (int k = 0; k < n; k++) {
            cout << arr[k] << " ";
        }
        cout << endl;
    }
}

3. main.cpp​ (主程序文件)

复制代码
#include "SortAlgorithms.h"

int main() {
    vector<int> arr = {5, 2, 4, 6, 1, 3};
    
    cout << "直接插入排序过程:" << endl;
    cout << "初始数组: ";
    for (int num : arr) {
        cout << num << " ";
    }
    cout << endl;
    
    insertionSortWithSteps(arr);
    
    cout << "\n最终排序结果: ";
    for (int num : arr) {
        cout << num << " ";
    }
    cout << endl;
    
    return 0;
}

Visual Studio中的文件组织:

文件结构:

复制代码
你的项目文件夹/
│
├── SortAlgorithms.h      (头文件 - 包含函数声明)
├── SortAlgorithms.cpp    (源文件 - 包含函数定义)
└── main.cpp              (主文件 - 包含main函数)

在Visual Studio中创建步骤:

  1. 创建新项目:选择"控制台应用"

  2. 添加文件:

    • 在"解决方案资源管理器"中右键"头文件" → 添加 → 新建项 → 选择"头文件(.h)" → 命名为SortAlgorithms.h

    • 在"源文件"上右键 → 添加 → 新建项 → 选择"C++文件(.cpp)" → 命名为SortAlgorithms.cpp

    • 修改现有的main.cpp(或添加一个新的)

  3. 编译运行:按F5或Ctrl+F5编译运行

文件类型的说明:

  • .h 文件​ (头文件):包含函数声明、类声明、宏定义、类型定义等

  • .cpp 文件​ (源文件):包含函数定义、类方法的实现

  • main.cpp​ (主源文件):包含main()函数,程序的入口点

头文件保护(#ifndef/#define/#endif)

没有头文件保护:

复制代码
// 假设在main.cpp中:
#include "myheader.h"
#include "myheader.h"  // 重复包含,会导致编译错误!

有头文件保护:

复制代码
// myheader.h
#ifndef MYHEADER_H      // 如果MYHEADER_H没有被定义
#define MYHEADER_H      // 定义MYHEADER_H

// 头文件的实际内容
void myFunction();
int myVariable = 10;

#endif                 // 结束
相关推荐
dialect_7 小时前
KMP算法
开发语言·c++·算法
reikocao7 小时前
卡尔曼滤波的动态机制:P如何改变融合权重
算法
漂流瓶jz8 小时前
UVA-1612 猜名次 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·题解·排序·aoapc·算法竞赛入门经典
吞下星星的少年·-·8 小时前
Div.2 CF 1130B题(构造,贪心)
算法·贪心·构造
ShineWinsu9 小时前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
傲世仙尊9 小时前
多态-虚表才活到运行期符号表早已消失
c++
chenbingjie_c9 小时前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
是main不是漫9 小时前
【双指针——移动零】数组划分类题目的入门基石
c++·算法
·present·9 小时前
CSAPP(Bomb lab)
c语言·c++
王老师青少年编程10 小时前
2026年信奥赛c++普及组csp-j第二轮复赛真题
c++·真题·csp·信奥赛