 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Aaron Walker Guest
|
Posted: Fri Feb 25, 2005 2:20 pm Post subject: trouble splitting strings |
|
|
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
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
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Fri Feb 25, 2005 2:25 pm Post subject: Re: trouble splitting strings |
|
|
* Aaron Walker:
| Quote: |
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?
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Fri Feb 25, 2005 5:46 pm Post subject: Re: trouble splitting strings |
|
|
On Fri, 25 Feb 2005 14:20:03 GMT in comp.lang.c++, Aaron Walker <ka0ttic (AT) cfl (DOT) rr.com> wrote,
| Quote: | I'm trying to code a function, that given a string and a delimiter char,
returns a vector of the sub-strings.
|
See also: [url]http://groups.google.com/groups?selm=4087fa25.97261737 (AT) news (DOT) west.earthlink.net[/url]
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Fri Feb 25, 2005 6:18 pm Post subject: Re: trouble splitting strings |
|
|
Aaron Walker wrote:
| Quote: | 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
#include
std::vector
splitstr(const std::string &str, const char delim)
{
std::vector
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
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
#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
|
|
| Back to top |
|
 |
Amit Guest
|
Posted: Sat Feb 26, 2005 1:10 am Post subject: Re: trouble splitting strings |
|
|
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.
"Aaron Walker" <ka0ttic (AT) cfl (DOT) rr.com> wrote
| Quote: | 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
#include
std::vector
splitstr(const std::string &str, const char delim)
{
std::vector
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
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
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|