By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

explode equivalent in c++

P: n/a
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!
Jun 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
DJH wrote:
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.
Jun 19 '07 #2

P: n/a
DJH
Gianni Mariani wrote:
DJH wrote:
>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.
Jun 19 '07 #3

P: n/a
On Jun 18, 4:35 pm, DJH <nos...@nospam.comwrote:
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::stringexplode(
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::stringresult ;

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::stringparts = explode("~!||1||1||foobar",
"||") ;
std::copy(parts.begin(), parts.end(),
std::ostream_iterator<std::string>(std::cout, "\n")) ;
}

--
Alan Johnson

Jun 19 '07 #4

P: n/a
DJH wrote:
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::stringexplode(
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::stringresult ;

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::stringparts = explode("~!||1||1||foobar", "||") ;
std::copy(parts.begin(), parts.end(),
std::ostream_iterator<std::string>(std::cout, "\n")) ;
}

--
Alan Johnson
Jun 19 '07 #5

P: n/a
On Jun 19, 1:35 am, DJH <nos...@nospam.comwrote:
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"
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.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.