Split, high efficiency

Source: Internet
Author: User

Bring your own strtok

paste a C-implemented function:
Invocation Example:
#define Max_cols
Char src[256];
Char *cols[max_cols];
int count;
int i;
strcpy (src, "AA aa BBC CCCD");
count = Splitex (src, ", cols, max_cols);
For (i=0; i<count; i++)
printf ("%d.%s\n", I+1, Cols[i]);

int Splitex (char *src, const char seperator, char **pcols, const int nmaxcols)
Char *p;
Char **pcurrent;
int count = 0;

if (nmaxcols <= 0)
return 0;

p = src;
pcurrent = pcols;

While (true)
*pcurrent = p;

if (Count >= nmaxcols)
Break ;

p = STRCHR (P, seperator);
if (p = = NULL)
Break ;

*p = ' + ';

return count;


#include <string>
#include <iostream>
#include <vector>

using namespace Std;

int main ()
String test = "AA aa BBC CCCD";
Vector<string> Strvec;
String strtemp;

String::size_type pos1, Pos2;
Pos2 = Test.find (");
POS1 = 0;
while (string::npos! = Pos2)
Strvec.push_back (Test.substr (POS1, pos2-pos1));

POS1 = Pos2 + 1;
Pos2 = Test.find (", POS1);
Strvec.push_back (Test.substr (POS1));

Vector<string>::iterator iter1 = Strvec.begin (), iter2 = Strvec.end ();
while (iter1! = iter2)
cout << *iter1 << Endl;

return 0;

Split, high efficiency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.