// c_str chop method
#include "stdafx.h"
#include <stdio.h>
#include <vector>
using namespace std;
vector<char *> chopMat(char *strSrc, char *delim)
{
if(strSrc == NULL)
return vector<char *>(0);
size_t delim_count = 0;
vector<int> poslist;
size_t i;
for(i = 0; i < strlen(strSrc); i ++)
{
if(strSrc[i] == *delim)
{
delim_count +=1;
poslist.push_back(i + 1);
}
}
char *str = strSrc;
vector<char *> rtlist;
rtlist.push_back(strtok(str, delim));
for(i = 0; i < delim_count - 1; i ++)
{
char *tmp = NULL;
tmp = strtok(str + poslist[i], delim);
rtlist.push_back(tmp);
}
char *last = NULL;
last = strtok(str + poslist[poslist.size()-1], "\0");
rtlist.push_back(last);
return rtlist;
}
int main()
{
unsigned long t = 0xC0A8016B;
printf("%l", t);
int nf1,nf2,nf3,nf4;
nf1 = t>>24;
nf2 = (t & 0xff0000)>>16;
nf3 = (t & 0xff00)>>8;
nf4 = t & 0xff;
unsigned long t2 = 0;
t2 = (nf1<<24) + (nf2<<16) + (nf3<<8) + nf4;
char *str1 = new char[16];
strcpy(str1, "192.168.1.107\0");
// int n1,n2,n3,n4;
// char *c1,*c2,*c3,*c4;
// c1 = strtok(str, ".");
// n1 = atoi(c1);
// c2 = strtok(str+strlen(c1)+1, ".");
// n2 = atoi(c2);
// c3 = strtok(str+strlen(c1)+strlen(c2)+2, ".");
// n3 = atoi(c3);
// c4 = strtok(str+strlen(c1)+strlen(c2)+strlen(c3)+3, "\0");
// n4 = atoi(c4);
vector<char *> tv = chopMat(str1, ".");
for(size_t i = 0; i < tv.size(); i++)
{
puts(tv[i]);
}
delete[] str1;
return 0;
}
C串分隔子串到vector容器内
最新推荐文章于 2022-10-24 21:50:41 发布