trouble splitting strings

A

Aaron Walker

I have a feeling this going to end up being something so stupid, but
right now I'm confused as hell.

I'm trying to code a function, that given a string and a delimiter char,
returns a vector of the sub-strings.

Here's what I have (I've thrown a main() in there for this mail).
---
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string>
splitstr(const std::string &str, const char delim)
{
std::vector<std::string> vec;

std::string::size_type pos, lpos = 0;

while (true)
{
pos = str.find(delim, lpos);
if (pos == std::string::npos)
{
vec.push_back(str.substr(lpos));
break;
}

vec.push_back(str.substr(lpos, pos));
lpos = ++pos;
}
return vec;
}

int
main(int argc, char **argv)
{
if (argc != 2)
return 1;

std::vector<std::string> testvec = splitstr(argv[1], ' ');

std::cout << "testvec1 = [";

std::vector<std::string>::iterator i;
for (i = testvec.begin() ; i != testvec.end() ; ++i)
std::cout << "'" << *i << "', ";
std::cout << "]" << std::endl;
}
---

For some reason I cannot figure out, it's doing this:

$ ./splitstr "this is a test"
testvec1 = [ 'this', 'is a te', 'a test', 'test', ]

It looks like lpos is correct for every one, but pos is only correct for
the first and last elements.

When printing the value of pos and lpos on each iteration, it looks
correct. For example, the 2nd element in the vector above is lpos=5,
pos=7. How on earth can that equal a 7char string?

Anyone able to give me a kind shove in the right direction?

Thanks,
Aaron
 
A

Alf P. Steinbach

* Aaron Walker:
Anyone able to give me a kind shove in the right direction?

The second argument of std::string::substr is the subtstring length,
not the end position.
 
D

Default User

Aaron said:
I have a feeling this going to end up being something so stupid, but
right now I'm confused as hell.

I'm trying to code a function, that given a string and a delimiter
char, returns a vector of the sub-strings.

Here's what I have (I've thrown a main() in there for this mail).
---
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string>
splitstr(const std::string &str, const char delim)
{
std::vector<std::string> vec;

std::string::size_type pos, lpos = 0;

while (true)
{
pos = str.find(delim, lpos);
if (pos == std::string::npos)
{
vec.push_back(str.substr(lpos));
break;
}

vec.push_back(str.substr(lpos, pos));
lpos = ++pos;
}
return vec;
}

int
main(int argc, char **argv)
{
if (argc != 2)
return 1;

std::vector<std::string> testvec = splitstr(argv[1], ' ');

std::cout << "testvec1 = [";

std::vector<std::string>::iterator i;
for (i = testvec.begin() ; i != testvec.end() ; ++i)
std::cout << "'" << *i << "', ";
std::cout << "]" << std::endl;
}
---

For some reason I cannot figure out, it's doing this:

$ ./splitstr "this is a test"
testvec1 = [ 'this', 'is a te', 'a test', 'test', ]

It looks like lpos is correct for every one, but pos is only correct
for the first and last elements.

When printing the value of pos and lpos on each iteration, it looks
correct. For example, the 2nd element in the vector above is lpos=5,
pos=7. How on earth can that equal a 7char string?

Anyone able to give me a kind shove in the right direction?


Here's a version that I wrote some time back. I've seen other
variations posted here that use a stringstream and read from it.


#include <vector>
#include <string>

// breaks apart a string into substrings separated by a character string
// does not use a strtok() style list of separator characters
// returns a vector of std::strings

std::vector<std::string> Explode (const std::string &inString,
const std::string &separator)
{
std::vector<std::string> returnVector;
std::string::size_type start = 0;
std::string::size_type end = 0;

while ((end=inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end+separator.size();
}

returnVector.push_back (inString.substr (start));

return returnVector;

}



Brian
 
A

Amit

Try

vec.push_back(str.substr(lpos, pos-lpos));

You need to get only the difference of characters between the two positions,
not the total char's as present in pos.
 

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

Staff online

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top