Skip to content

Instantly share code, notes, and snippets.

@jibsen jibsen/coordsort.c
Last active Dec 25, 2015

Embed
What would you like to do?
A simple example that reads integer coordinates in the form x,y into a scv_vector and prints them in lexicographical order.
#include <stdlib.h>
#include <stdio.h>
#include "scv.h"
struct point {
int x;
int y;
};
int point_compare(const struct point *lhs, const struct point *rhs)
{
if (lhs->x == rhs->x) {
return lhs->y - rhs->y;
}
return lhs->x - rhs->x;
}
int main(void)
{
struct scv_vector *v;
struct point p;
size_t i;
/* create a scv_vector of points, reserving space for 10 */
v = scv_new(sizeof p, 10);
/* read coordinates into p and append to v */
while (scanf("%d,%d", &p.x, &p.y) == 2) {
scv_push_back(v, &p);
}
/* sort points in v */
qsort(scv_data(v), scv_size(v), scv_objsize(v), point_compare);
/* print points */
for (i = 0; i < scv_size(v); ++i) {
struct point *pp = scv_at(v, i);
printf("%d,%d\n", pp->x, pp->y);
}
scv_delete(v);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.