#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstl/cvector.h>
/**
*bref 比较容器中两个节点值的大小
*/
void value_greater(const void *cpv_first, const void *cpv_second, void *pv_output)
{
if (*(int *)cpv_first > *(int *)cpv_second){
*(bool_t *)pv_output = true;
}else{
*(bool_t *)pv_output = false;
}
}
/**
*bref 用迭代器遍历容器
*/
void vector_travel_by_iter(vector_t *pt_vec)
{
iterator_t iter;
if (pt_vec == NULL){
fprintf(stderr, "[travel_inter]:vector is null.\n");
return;
}
printf("------------使用迭代器遍历 ------------\n");
for (iter = vector_begin(pt_vec); !iterator_equal(iter, vector_end(pt_vec)); iter = iterator_next(iter)){
printf("%d ", *(int *)iterator_get_pointer(iter));
}
printf("\n");
}
/**
*bref 用下标遍历容器
*/
void vector_travel_by_index(vector_t *pt_vec)
{
size_t t_index = 0;
if (pt_vec == NULL){
fprintf(stderr, "[travel_index]:vector is null.\n");
return;
}
printf("------------使用下标遍历------------\n");
for (t_index = 0; t_index < 10; t_index++){
printf("%d ", *(int *)vector_at(pt_vec, t_index));
}
printf("\n");
}
int main(int argc, char *argv[])
{
vector_t *pt_vec = create_vector(int);
size_t t_index = 0;
iterator_t iter;
if (pt_vec == NULL){
fprintf(stderr, "create vector is failed.\n");
return -1;
}
vector_init(pt_vec);
srand((unsigned int)time(NULL));
///循环赋值
for (t_index = 0; t_index < 10; t_index++){
vector_push_back(pt_vec, rand()%64);
}
printf("begin sorting:\n");
vector_travel_by_iter(pt_vec);
vector_travel_by_index(pt_vec);
printf("\nafter order sorting:\n");
///有条件排序,排序条件以回调函数的形式给出,如果回调函数为空,使用默认条件(递增)排序
algo_sort_if(vector_begin(pt_vec), vector_end(pt_vec), value_greater);
vector_travel_by_iter(pt_vec);
printf("\nafter reverse sorting:\n");
///默认条件(递增)排序
algo_sort(vector_begin(pt_vec), vector_end(pt_vec));
vector_travel_by_index(pt_vec);
vector_destroy(pt_vec);
return 0;
}
编译:
gcc -o test_vector test_vector.c -I/root/libcstl/lib/include/ -L/root/libcstl/lib/lib/ -lcstl
begin sorting:
------------使用迭代器遍历 ------------
27 7 16 58 49 24 53 14 34 22
------------使用下标遍历------------
27 7 16 58 49 24 53 14 34 22
after order sorting:
------------使用迭代器遍历 ------------
58 53 49 34 27 24 22 16 14 7
after reverse sorting:
------------使用下标遍历------------
7 14 16 22 24 27 34 49 53 58
#include <stdlib.h>
#include <stdio.h>
#include <cstl/clist.h>
#define N_MAX 29
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef struct{
uint8_t id;
uint8_t age;
uint8_t sex;
char name[N_MAX];
}user_t;
static void _user_init(const void *cpv_input, void *pv_output)
{
user_t * user_input = (user_t *)cpv_input;
memset(user_input, 0, sizeof(user_t));
*(bool_t *)pv_output = true;
}
static void _user_destroy(const void *cpv_input, void *pv_output)
{
user_t * user_input = (user_t *)cpv_input;
memset(user_input, 0, sizeof(user_t));
*(bool_t *)pv_output = true;
}
static void _user_copy(const void *cpv_first, const void *cpv_second, void *pv_output)
{
user_t *user_1 = (user_t *)cpv_first;
user_t *user_2 = (user_t *)cpv_second;
user_1->id = user_2->id;
user_1->age = user_2->age;
user_1->sex = user_2->sex;
strncpy(user_1->name, user_2->name, N_MAX);
*(bool_t *)pv_output = true;
}
static void _user_less(const void *cpv_first, const void *cpv_second, void *pv_output)
{
if (((user_t *)cpv_first)->id < ((user_t *)cpv_second)->id){
*(bool_t *)pv_output = true;
}else{
*(bool_t *)pv_output = false;
}
}
void list_travel_by_iter(list_t *pt_list)
{
iterator_t iter;
user_t user;
if (pt_list == NULL){
fprintf(stderr, "[travel_inter]:list is null.\n");
return;
}
printf("------------使用迭代器遍历 ------------\n");
printf("Id\t Name \tAge\tSex\t\n");
for (iter = list_begin(pt_list); !iterator_equal(iter, list_end(pt_list)); iter = iterator_next(iter)){
iterator_get_value(iter, &user);
printf("%u\t %s \t% u\t %c\n", user.id, user.name, user.age, user.sex?'m':'f');
}
printf("\n");
}
int main(int argc, char *argv[])
{
list_t *pt_list = NULL;
size_t index;
user_t user_obj[] = {{134, 57, 1, "steven jobs"},
{110, 50, 1, "bill gates"},
{85, 52, 0, "alex martelli"},
{220, 30, 0, "michel wood"},
{24, 72, 1, "bob green"},
{102, 29, 1, "devil cash"}};
type_register(user_t, _user_init, _user_copy, _user_less, _user_destroy);
pt_list = create_list(user_t);
if (pt_list == NULL){
fprintf(stderr, "create list is failed.\n");
return -1;
}
list_init(pt_list);
for (index = 0; index < 6; index++){
list_push_back(pt_list, &user_obj[index]);
}
printf("before sorting\n");
list_travel_by_iter(pt_list);
list_sort(pt_list);
printf("after sorting\n");
list_travel_by_iter(pt_list);
list_destroy(pt_list);
return 0;
}
gcc -o test_list test_list.c -I/root/libcstl/lib/include/ -L/root/libcstl/lib/lib/ -lcstl
before sorting
------------使用迭代器遍历 ------------
Id Name Age Sex
134 steven jobs 57 m
110 bill gates 50 m
85 alex martelli 52 f
220 michel wood 30 f
24 bob green 72 m
102 devil cash 29 m
after sorting
------------使用迭代器遍历 ------------
Id Name Age Sex
24 bob green 72 m
85 alex martelli 52 f
102 devil cash 29 m
110 bill gates 50 m
134 steven jobs 57 m
220 michel wood 30 f