首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++兑现C#中split函数

2013-09-05 
C++实现C#中split函数请问如何实现split函数,即对字符串aa##bb##cc用分隔符“##”分割后,得到3个字符串,分别

C++实现C#中split函数
请问如何实现split函数,即对字符串aa##bb##cc用分隔符“##”分割后,得到3个字符串,分别为"aa","bb","cc",对aa##bb##用分隔符“##”分割后,得到三个字符串,分别为"aa","bb",""
问题的关键是在情况2中能得到空字符串。而不是只得到"aa","bb"!
[解决办法]
http://blog.csdn.net/puttytree/article/details/7106957
除了以上方法,我常用boost里面的。


#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
 
using namespace std;
 
int main( int argc, char** argv )
{
  string s = "Hello, the beautiful world!";
  vector<string> rs;
  boost::split( rs, s, boost::is_any_of( " ,!" ), boost::token_compress_on );
  for( vector<string>::iterator it = rs.begin(); it != rs.end(); ++ it )
    cout << *it << endl;
 
  return 0;
}

[解决办法]
写个小例子,重定义 whitespace 即可。
末尾存在空字符串儿的问题,可以通过单独的逻辑处理。

#include <cctype>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
#include <vector>

const std::ctype_base::mask* whitespace ()
{
 using namespace std;
 using ctype = std::ctype<char>;
 static std::vector<ctype::mask> table (ctype::classic_table(),ctype::classic_table()+ctype::table_size);
 table['#'] = ctype::space;
 return &table[0];
}

int main (int,char**)
{
 std::string const line("aa##bb##");

 std::istringstream buffer(line);

 std::locale const locale(buffer.getloc(),new std::ctype<char>(whitespace()));
 buffer.imbue(locale);

 std::vector<std::string> split;


 std::copy(std::istream_iterator<std::string>(buffer),
           std::istream_iterator<std::string>{},
           std::back_inserter(split));

 if (std::isspace(line.back(),locale))
 {
  split.emplace_back("");
 }

 for (auto const& s : split)
 {
  std::cout << s << std::endl;
 }
}


[解决办法]
得到第三个字符串为"",这只是需要另外判断,"##"是否是字符串的末尾,是的话,就加一个""。如此而已啊。
[解决办法]
仅供参考
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
    char *string,
    const char *control1,//连续出现时视为中间夹空token
    const char *control2 //连续出现时视为中间无空token
    )
{
    unsigned char *str;
    const unsigned char *ctrl1 = control1;
    const unsigned char *ctrl2 = control2;
    unsigned char map1[32],map2[32];
    static char *nextoken;
    static char flag=0;
    unsigned char c;
    int L;

    memset(map1,0,32);
    memset(map2,0,32);
    do {
        map1[*ctrl1 >> 3] 
[解决办法]
= (1 << (*ctrl1 & 7));
    } while (*ctrl1++);
    do {
        map2[*ctrl2 >> 3] 
[解决办法]
= (1 << (*ctrl2 & 7));
    } while (*ctrl2++);

    if (string) {
        if (control2[0]) {
            L=strlen(string);
            while (1) {
                c=string[L-1];


                if (map2[c >> 3] & (1 << (c & 7))) {
                    L--;
                    string[L]=0;
                } else break;
            }
        }
        if (control1[0]) {
            L=strlen(string);
            c=string[L-1];
            if (map1[c >> 3] & (1 << (c & 7))) {
                string[L]=control1[0];
                string[L+1]=0;
            }
        }
        str=string;
    }
    else        str=nextoken;

    string=str;
    while (1) {
        if (0==flag) {
            if (!*str) break;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                break;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                string++;
                str++;
            } else {


                flag=1;
                str++;
            }
        } else if (1==flag) {
            if (!*str) break;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                flag=0;
                break;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                *str=0;
                str++;
                flag=2;
                break;
            } else str++;
        } else {//2==flag
            if (!*str) return NULL;
            if (map1[*str >> 3] & (1 << (*str & 7))) {
                str++;
                string=str;
                flag=0;
            } else if (map2[*str >> 3] & (1 << (*str & 7))) {
                str++;
                string=str;
            } else {


                string=str;
                str++;
                flag=1;
            }
        }
    }
    nextoken=str;

    if (string==str) return NULL;
    else             return string;
}
void main()
{
   strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some  more tokens, ");
   strcpy(seps1,",\n");strcpy(seps2," \t");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234
[解决办法]
 LIYI
[解决办法]
China 
[解决办法]
 010 
[解决办法]
201110260000
[解决办法]
OK");
   strcpy(seps1,"
[解决办法]
");strcpy(seps2," ");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK");
   strcpy(seps1,"");strcpy(seps2,"
------解决方案--------------------


");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK");
   strcpy(seps1,"
[解决办法]
");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",a");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);


      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,"a,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",a,,b");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,a,,b,,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,");
   strcpy(seps1,",");strcpy(seps2,"");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }

   strcpy(string,",,,");
   strcpy(seps1,",");strcpy(seps2," ");
   printf("\n[%s]\nTokens:\n",string);
   token=zzstrtok(string,seps1,seps2);
   while (token!=NULL) {
      printf(" <%s>",token);
      token=zzstrtok(NULL,seps1,seps2);
   }
}
//
//[A      string          of ,,tokens
//
//and some  more tokens,]


//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234
[解决办法]
 LIYI
[解决办法]
China 
[解决办法]
 010 
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234
[解决办法]
LIYI
[解决办法]
010
[解决办法]
201110260000
[解决办法]
OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,


[解决办法]
这个不错


http://mapreduce-lite.googlecode.com/svn/trunk/src/strutil/split_string.h

热点排行