Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jibsen
Last active December 25, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jibsen/6899042 to your computer and use it in GitHub Desktop.
Save jibsen/6899042 to your computer and use it in GitHub Desktop.
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