Skip to content

Instantly share code, notes, and snippets.

@Siapran
Created March 21, 2015 18:39
Show Gist options
  • Save Siapran/576bcbdb5bd3cbee722e to your computer and use it in GitHub Desktop.
Save Siapran/576bcbdb5bd3cbee722e to your computer and use it in GitHub Desktop.
GCL usage example
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef const char* str;
#define TYPE str
#include "vector.h"
#include "vector.def"
#undef TYPE
#define KEY_TYPE str
#define VALUE_TYPE int
#include "map.h"
#include "map.def"
#undef KEY_TYPE
#undef VALUE_TYPE
int main() {
size_t i;
map_iterator(str, int) itr;
vector_t(str) my_vector;
vector_t(str) my_vector_2;
map_t(str, int) my_map;
char sentence[] = "The Standard Template Library (STL) is a software library for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functional, and iterators.";
const char delimiter[] = " ";
char *token;
token = strtok(sentence, delimiter);
vector_init(str)(&my_vector, 0);
map_init(str, int)(&my_map, strcmp);
while (token != NULL) {
vector_push_back(str)(&my_vector, token);
token = strtok(NULL, delimiter);
}
for (i = 0; i < my_vector.size; ++i) {
++map_at(str, int)(&my_map,
my_vector.data[i]);;
}
printf("mot\t: compte\n");
printf("\n");
vector_init(str)(&my_vector_2, 0);
map_foreach(str, int, &itr, &my_map) {
printf("%s\t: %d\n", itr->key, itr->value);
vector_push_back(str)(&my_vector_2, itr->key);
}
for (i = 0; i < my_vector_2.size; ++i) {
map_erase(str, int)(&my_map, my_vector_2.data[i]);
}
map_foreach(str, int, &itr, &my_map) {
printf("%s\t: %d\n", itr->key, itr->value);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment