Skip to content

Instantly share code, notes, and snippets.

@ryaninhust
Created May 15, 2013 06:59
Show Gist options
  • Save ryaninhust/5582111 to your computer and use it in GitHub Desktop.
Save ryaninhust/5582111 to your computer and use it in GitHub Desktop.
Extract data from LIBSVM format
/* strtok example */
#include <stdio.h>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int main ()
{
char str[] ="1 1:1 2:2 3:3";
char * pch;
vector<string> res;
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
res.push_back(pch);
pch = strtok(NULL, " ");
}
for(int i=0;i < res.size();i++)
{
if(i == 0)
{
printf("label\n");
}
else
{
char *tem = new char[5];
char * subpch;
strcpy(tem, res[i].data());
subpch = strtok(tem, ":");
while(subpch !=NULL)
{
printf("%s\n", subpch);
subpch = strtok(NULL, ":");
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment