explode equivalent in c++

D

DJH

I am trying to find the most efficient way to code a php explode in C++.

What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?

example:

~!||1||1||foobar

I would like a string array as such

stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

Thanks in advance!
 
G

Gianni Mariani

DJH said:
I am trying to find the most efficient way to code a php explode in C++.

What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?

example:

~!||1||1||foobar

I would like a string array as such

stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

Thanks in advance!

Exactly what kind of efficiency are you talking about ?

#include <string>

const std::string arrayss[] = { "~!", "1", "1", "foobar" };

It's debatable what is more efficient than what - however this below is
about as efficient as you can get:

const char * const arrayccs[] = { "~!", "1", "1", "foobar" };

Since there is no run-time cost for arrayccs.

But the real question is then how it's used. If you're taking
std::string s of strings from arrayccs all the time it may be more
efficient to use arrayss.

Having said all this, I fail to see why it matters so much since unless
you can measure the difference, it's irrelevant.
 
D

DJH

Gianni said:
DJH said:
I am trying to find the most efficient way to code a php explode in C++.

What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?

example:

~!||1||1||foobar

I would like a string array as such

stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

Thanks in advance!

Exactly what kind of efficiency are you talking about ?


I just need to parse the "~!||1||1||foobar" string into parts delimited
by the "||" delimiter, thats all. The parts should be a std::string array.

#include <string>

const std::string arrayss[] = { "~!", "1", "1", "foobar" };

It's debatable what is more efficient than what - however this below is
about as efficient as you can get:

const char * const arrayccs[] = { "~!", "1", "1", "foobar" };

Since there is no run-time cost for arrayccs.

But the real question is then how it's used. If you're taking
std::string s of strings from arrayccs all the time it may be more
efficient to use arrayss.

Having said all this, I fail to see why it matters so much since unless
you can measure the difference, it's irrelevant.
 
A

Alan Johnson

DJH said:
I am trying to find the most efficient way to code a php explode in C++.

What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?

example:

~!||1||1||foobar

I would like a string array as such

stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

Thanks in advance!

Writing an explode function should not be too difficult. E.g.:
#include <vector>
#include <string>

std::vector<std::string> explode(
const std::string & in,
const std::string & delim)
{
typedef std::string::size_type size_type ;

const size_type delim_len = delim.length() ;
std::vector<std::string> result ;

size_type i = 0, j ;
for (;;)
{
j = in.find(delim, i) ;
result.push_back(in.substr(i, j-i)) ;
if (j == std::string::npos)
break ;
i = j + delim_len ;
}

return result ;
}

#include <iterator>
#include <algorithm>
#include <iostream>

int main()
{
std::vector<std::string> parts = explode("~!||1||1||foobar", "||") ;
std::copy(parts.begin(), parts.end(),
std::eek:stream_iterator<std::string>(std::cout, "\n")) ;
}
 
A

Alan Johnson

I am trying to find the most efficient way to code a php explode in C++.

What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?

example:

~!||1||1||foobar

I would like a string array as such

stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

Thanks in advance!


It shouldn't be hard to cook up an explode function. Here is what I
come up with after a few minutes of tinkering:
#include <vector>
#include <string>

std::vector<std::string> explode(
const std::string & in,
const std::string & delim)
{
typedef std::string::size_type size_type ;

const size_type delim_len = delim.length() ;
std::vector<std::string> result ;

size_type i = 0, j ;
for (;;)
{
j = in.find(delim, i) ;
result.push_back(in.substr(i, j-i)) ;
if (j == std::string::npos)
break ;
i = j + delim_len ;
}

return result ;
}

#include <iterator>
#include <algorithm>
#include <iostream>

int main()
{
std::vector<std::string> parts = explode("~!||1||1||foobar",
"||") ;
std::copy(parts.begin(), parts.end(),
std::eek:stream_iterator<std::string>(std::cout, "\n")) ;
}
 
J

James Kanze

I am trying to find the most efficient way to code a php explode in C++.
What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?


I would like a string array as such
stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"

You might want to look at the code for FieldArray and its
subclasses in the Text subsystem at my site
(http://kanze.james.neuf.fr/code-en.html). You'll probably have
to adapt it some; it's old code, and for example,
RegularExpressionSeparatedFields uses my own RegularExpression
class, rather than that of Boost. But it's simple to use:

RegularExpressionSeparatedFields
fields( "\\|\\|" ) ;
std::string line ;
while ( std::getline( in, line ) ) {
fields = line ;
// fields[0] contains the entire line, fields[1] the
// first field, fields[2] the second, etc.
}

The base class FieldArray uses the strategy pattern to decide
how to cut up the string, so it would be easy to add additional
critera. (All RegularExpressionSeparatedFields does in addition
is to provide the delegate.) It should be simple, for example,
to provide a delegate which uses a fixed string as a separator;
just replace std::find in CharacterSeparatedFieldSplitter with
std::search.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top