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

trouble splitting strings

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
On Fri, 25 Feb 2005 14:20:03 GMT in comp.lang.c++, Aaron Walker <ka*****@cfl.rr.com> wrote,
I'm trying to code a function, that given a string and a delimiter char,
returns a vector of the sub-strings.


See also: http://groups.google.com/gr*********....earthlink.net

Jul 23 '05 #3

P: n/a
Aaron Walker wrote:
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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.