From 1b0063f0905470956755ba9a20071a63544ebf99 Mon Sep 17 00:00:00 2001 From: guishenking Date: Thu, 21 Aug 2025 22:21:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E5=87=BD=E6=95=B0=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=20=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 5 +- src/lib/vector/vector.c | 4 +- src/lib/vector/vector.h | 8 ++ src/main.c | 164 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 176 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index eaaac91..9fc7386 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "C_Cpp.default.compilerPath": "/usr/bin/g++-14" + "C_Cpp.default.compilerPath": "/usr/bin/g++-14", + "files.associations": { + "stdio.h": "c" + } } \ No newline at end of file diff --git a/src/lib/vector/vector.c b/src/lib/vector/vector.c index e38f97d..a05c9e9 100755 --- a/src/lib/vector/vector.c +++ b/src/lib/vector/vector.c @@ -141,7 +141,7 @@ bool vector_remove_at(vector_t *vec, size_t index){ * @param vec * @return size_t */ -size_t vector_used(const vector_t *vec){ +size_t vector_size(const vector_t *vec){ if (!vec) return 0; // 检查vec是否为NULL return vec->size; } @@ -309,6 +309,7 @@ bool vector_reserve(vector_t *vec, size_t new_capacity){ } return true; // 成功保留数据并扩容 } + /** * @brief 排序容器中数据 * @@ -364,7 +365,6 @@ bool vector_for_each(const vector_t *vec, void (*func)(void *,size_t index)){ } } - /** * @brief 删除与 elem 匹配的元素(通过 compare 函数判断)。 * diff --git a/src/lib/vector/vector.h b/src/lib/vector/vector.h index ee3ad43..00a1781 100755 --- a/src/lib/vector/vector.h +++ b/src/lib/vector/vector.h @@ -28,6 +28,14 @@ vector_t *vector_create(size_t elem_size, size_t initial_capacity); bool vector_destroy(vector_t *vec); +bool vector_add_at(vector_t *vec, size_t index, const void *elem); + +bool vector_swap(vector_t *vec, size_t index1, size_t index2); + +bool vector_remove_at(vector_t *vec, size_t index); + +size_t vector_size(const vector_t *vec); + bool vector_push_back(vector_t *vec, const void *elem); bool vector_pop_back(vector_t *vec); diff --git a/src/main.c b/src/main.c index 89f523d..8016b8a 100755 --- a/src/main.c +++ b/src/main.c @@ -8,10 +8,170 @@ * @copyright Copyright (c) 2025 * */ -#include #include "lib/vector/vector.h" +#include +#include + +// 测试用的结构体 +typedef struct { + int id; + char name[20]; + float score; +} Student; + +// 比较函数 +int compare_students(const void *a, const void *b) { + const Student *sa = (const Student *)a; + const Student *sb = (const Student *)b; + return sa->id - sb->id; +} + +// 谓词函数 +int is_high_score(const void *a) { + const Student *s = (const Student *)a; + return s->score >= 90.0f; +} + +// 遍历打印函数 +void print_student(void *elem, size_t index) { + Student *s = (Student *)elem; + printf("[%zu] ID: %d, Name: %s, Score: %.1f\n", + index, s->id, s->name, s->score); +} int main() { - vector_t *vec = vector_create(sizeof(int), 10); + printf("=== 开始测试 vector 实现 ===\n\n"); + + // 测试 vector_create + printf("1. 测试 vector_create...\n"); + vector_t *students = vector_create(sizeof(Student), 2); + if (!students) { + printf("创建 vector 失败!\n"); + return -1; + } + printf("创建成功, 初始容量: %zu\n", vector_get_capacity(students)); + + // 测试 vector_push_back + printf("\n2. 测试 vector_push_back...\n"); + Student s1 = {101, "Alice", 85.5f}; + Student s2 = {102, "Bob", 92.0f}; + Student s3 = {103, "Charlie", 78.5f}; + Student s4 = {104, "David", 95.0f}; + + vector_push_back(students, &s1); + vector_push_back(students, &s2); + vector_push_back(students, &s3); // 这里应该触发扩容 + printf("添加3个学生后, 容量: %zu, 大小: %zu\n", + vector_get_capacity(students), vector_size(students)); + + // 测试 vector_get + printf("\n3. 测试 vector_get...\n"); + Student *ps = (Student *)vector_get(students, 1); + printf("索引1的学生: ID=%d, Name=%s\n", ps->id, ps->name); + + // 测试 vector_set + printf("\n4. 测试 vector_set...\n"); + strcpy(s2.name, "Robert"); + vector_set(students, 1, &s2); + ps = (Student *)vector_get(students, 1); + printf("修改后索引1的学生: Name=%s\n", ps->name); + + // 测试 vector_add_at + printf("\n5. 测试 vector_add_at...\n"); + vector_add_at(students, 1, &s4); + printf("在索引1插入新学生后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_remove_at + printf("\n6. 测试 vector_remove_at...\n"); + vector_remove_at(students, 0); + printf("删除索引0后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_swap + printf("\n7. 测试 vector_swap...\n"); + vector_swap(students, 0, 1); + printf("交换索引0和1后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_pop_back + printf("\n8. 测试 vector_pop_back...\n"); + vector_pop_back(students); + printf("弹出最后一个元素后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_sort + printf("\n9. 测试 vector_sort...\n"); + vector_push_back(students, &s1); + vector_push_back(students, &s3); + printf("排序前:\n"); + vector_for_each(students, print_student); + vector_sort(students, compare_students); + printf("按ID排序后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_reverse + printf("\n10. 测试 vector_reverse...\n"); + vector_reverse(students); + printf("反转后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_find + printf("\n11. 测试 vector_find...\n"); + size_t index; + if (vector_find(students, &s1, compare_students, &index)) { + printf("找到ID为%d的学生在索引%zu\n", s1.id, index); + } else { + printf("未找到ID为%d的学生\n", s1.id); + } + + // 测试 vector_find_if + printf("\n12. 测试 vector_find_if...\n"); + if (vector_find_if(students, is_high_score, &index)) { + Student *high_score = (Student *)vector_get(students, index); + printf("找到高分学生: %s (%.1f)\n", high_score->name, high_score->score); + } else { + printf("未找到高分学生\n"); + } + + // 测试 vector_remove + printf("\n13. 测试 vector_remove...\n"); + vector_remove(students, &s1, compare_students); + printf("删除ID为%d的学生后:\n", s1.id); + vector_for_each(students, print_student); + + // 测试 vector_resize + printf("\n14. 测试 vector_resize...\n"); + vector_resize(students, 5); + printf("调整大小为5后:\n"); + vector_for_each(students, print_student); + + // 测试 vector_clear + printf("\n15. 测试 vector_clear...\n"); + vector_clear(students); + printf("清空后大小: %zu\n", vector_size(students)); + + // 测试 vector_shrink_to_fit + printf("\n16. 测试 vector_shrink_to_fit...\n"); + vector_push_back(students, &s1); + vector_push_back(students, &s2); + printf("添加2个学生后, 容量: %zu\n", vector_get_capacity(students)); + vector_shrink_to_fit(students); + printf("shrink_to_fit后, 容量: %zu\n", vector_get_capacity(students)); + + // 测试 vector_reserve + printf("\n17. 测试 vector_reserve...\n"); + vector_reserve(students, 10); + printf("预留10个容量后, 容量: %zu\n", vector_get_capacity(students)); + + // 测试 vector_destroy + printf("\n18. 测试 vector_destroy...\n"); + if (vector_destroy(students)) { + printf("销毁成功\n"); + } else { + printf("销毁失败\n"); + } + + printf("\n=== 测试完成 ===\n"); return 0; } \ No newline at end of file