A
Andrew Brampton
Hi,
I'm new to using std classes, but I was wondering how I could do the
following efficiently with the string class:
Say I had a string with delimited values such as:
"field1,field2,field3,field4"
and I wanted to to place those 4 fields into different variables I would do
something like:
/* Note code quickly pulled from my head - may be wrong */
char *tmp_ptr;
char *ptr = input;
while (ptr != null) {
tmp_ptr = strchr(ptr, ',');
strncpy(somewhere, ptr, tmp_ptr - ptr);
ptr = tmp_ptr;
}
Now that starts with a pointer to the beginning of the file, and moves it
along... and each new search done by strchr starts where the last one left
off.
Now I'm faced with the same problem, but I wanted to use C++... so my code
would look like this:
string input = "blah,blah,blah,blah";
int pos = 0;
int tmppos = 0;
while (tmp != npos) {
tmppos = input.find_first_of(",", pos);
//do some kind of copy from pos, to tmppos
pos = tmppos;
}
This again loops through the string, but I'm worried on how C++ handles this
behind the scenes... Everytime I do input.find_first_of(.., pos) will it
moving in its internal array from 0 to pos, and then starts the search?...In
my previous case it didn't have to move from 0 to pos on each iteration
because I stored pos as a pointer..
So I was wondering if there is some way I can make input remember the last
place I started from inside the string, or, should I just use c_str() and
use my own char * to do everything?
Also it should be noted that I'm only asking this so I can learn best
practices, but say for example I wasn't doing this on small data, but data
megabytes big, I imagine using a pointer to remember where to start from
will be faster.
Any feedback is welcome..
Thanks
Andrew
I'm new to using std classes, but I was wondering how I could do the
following efficiently with the string class:
Say I had a string with delimited values such as:
"field1,field2,field3,field4"
and I wanted to to place those 4 fields into different variables I would do
something like:
/* Note code quickly pulled from my head - may be wrong */
char *tmp_ptr;
char *ptr = input;
while (ptr != null) {
tmp_ptr = strchr(ptr, ',');
strncpy(somewhere, ptr, tmp_ptr - ptr);
ptr = tmp_ptr;
}
Now that starts with a pointer to the beginning of the file, and moves it
along... and each new search done by strchr starts where the last one left
off.
Now I'm faced with the same problem, but I wanted to use C++... so my code
would look like this:
string input = "blah,blah,blah,blah";
int pos = 0;
int tmppos = 0;
while (tmp != npos) {
tmppos = input.find_first_of(",", pos);
//do some kind of copy from pos, to tmppos
pos = tmppos;
}
This again loops through the string, but I'm worried on how C++ handles this
behind the scenes... Everytime I do input.find_first_of(.., pos) will it
moving in its internal array from 0 to pos, and then starts the search?...In
my previous case it didn't have to move from 0 to pos on each iteration
because I stored pos as a pointer..
So I was wondering if there is some way I can make input remember the last
place I started from inside the string, or, should I just use c_str() and
use my own char * to do everything?
Also it should be noted that I'm only asking this so I can learn best
practices, but say for example I wasn't doing this on small data, but data
megabytes big, I imagine using a pointer to remember where to start from
will be faster.
Any feedback is welcome..
Thanks
Andrew