include “stdafx.h”
include
include
include
include
int Token(const char* pSep, char* pStr, std::set& refset)
{
for(char* outer = strtok(pStr, pSep) ; NULL != outer; outer = strtok(NULL, pSep))
{
printf( “oken: %s\n”, outer );
refset.insert(outer);
}
return 0;
}
int Token(int pSep, char* pStr, std::set& refset)
{
char* str=pStr;
char* pSub=strchr(str,pSep);
char subStrBuf[128]={'\0'};
while(NULL!=pSub)
{
strncpy(subStrBuf, str, pSub-str);
refset.insert(subStrBuf);
printf( "oken: %s\n", subStrBuf );
str = pSub+1;
pSub=strchr(str,pSep);
}
if(NULL==pSub && strlen(str)>0)
{
strncpy(subStrBuf, str, strlen(str));
refset.insert(subStrBuf);
printf( "oken: %s\n", subStrBuf );
}
return 0;
}
int Token(char ch, char* pStr, std::set& refset)
{
std::string str=pStr;
int pos=str.find(ch);
std::string subStr;
while(-1!=pos)
{
subStr=str.substr(0,pos);
refset.insert(subStr);
printf( “oken: %s\n”, subStr.c_str());
str=str.substr(pos+1, str.size());
pos=str.find(ch);
}
if(pos == -1 && str.size()>0)
{
refset.insert(str);
printf( "oken: %s\n", str.c_str());
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf( “==================方式一==================\n”);
char str1[]=”127.0.0.1:10;127.0.0.2:20;127.0.0.3:30”;
std::set tempset;
Token(“;”, str1, tempset);
printf( "==================方式二==================\n");
char str2[]="127.0.0.1:10;127.0.0.2:20;127.0.0.3:30";
std::set tempset2;
Token(';', str2, tempset2);
printf( "==================方式三==================\n");
char str3[]="127.0.0.1:10;127.0.0.2:20;127.0.0.3:30";
std::set tempset3;
Token(';', str3, tempset3);
system("pause");
return 0;
}