Pushing a string on to a vector(and pointer math)
I''m looking for a smart way to push a string in to a vector . Following is some broken code that sort of explains what I''m pushing for :
void StringToVector(ByteArray &thevector, const char *text)
{
unsigned long i;
const char *ch = *text;
while(*ch != ''\0'')
{
thevector.push_back(&ch);
ch++;
}
}
The code is a bit wonky as I''m trying (unsucessfully) to be tricky with pointers. I could write this in a simpler manner but this way seems more efficient as the text array only needs to be parsed once. The idea I had was you you point a pointer at the character you want to push on to the vector then use pointer math to step it along the string. Once you hit the strings null you bail...
Can someone show me hoe I''d do the pointer work to make this work?
Thanks
gimp
Chris Brodie
Maybe this?
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
void StringToVector(ByteArray &thevector, const char *text){ const char *ch = text; while (*ch) { thevector.push_back(*ch); ch++; }} //...or, if you''re crazy enough to understand, this... void StringToVector(ByteArray &thevector, const char *text){ while (*text) thevector.push_back(*text++);}
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
#include <algorithm>#include <vector>void StringToVector(ByteArray &thevector, const char *text){ // get length of string, not including null character const int len = strlen (text); // two ways to do this part. Here's #1: // - extend or shrink vector to len, then overwrite vector thevector.resize (len); // extend or shrink to len copy (text, text + len, thevector.begin ()); // way #2: // delete vector, and use the back_inserter (impress your boss) thevector.clear (); thevector.reserve (len); // avoid reallocation copy (text, text + len, back_inserter<vector <char> > (thevector));}
back_inserter rocks. BTW, make sure there's a space between the > after char and the closing > of back_inserter, or MSVC will think it's a bitshift operator (>>).
Also, I think copy is highly underrated. It's like memcopy with iterators. Very useful.
Anyway, this function actually passes through text twice, once to find strlen, and then once for the copy. With this in mind, mossmoss's implementation is probably fastest. I just wanted to show some different ways of doing things.
I do think it's important that you clear the vector, or if not then you have an "assert (thevector.empty ())" at the top. If your vector is not empty, mossmoss's code will append it.
Edited by - Stoffel on September 14, 2000 1:13:01 PM
I am not sure if append is not the desired behavior, however, I just want to say that the cost of a strlen and resize is likely less then multiple resizes from push_back. New is expensive, read and compare zero to consecutive addresses is cheap. The second time through the string you are either in cache or have a hot page hit.
I agree that inserter templates rock.
I agree that inserter templates rock.
Ah, grib, my embedded-systems history shows through. I almost never take into account cache hits (since I worked without them for so long); I always just think of instructions.
BTW, if append IS the desired behavior, then you would be well-advised to do a resize or reserve (vec.size () + len) (where len is the length of your new string). I'm sure you can figure out where the new target of the copy would be, but make sure you don't store any iterators before the resize/reserve and then use them later; those functions can invalidate iterators.
Edited by - Stoffel on September 14, 2000 6:40:18 PM
BTW, if append IS the desired behavior, then you would be well-advised to do a resize or reserve (vec.size () + len) (where len is the length of your new string). I'm sure you can figure out where the new target of the copy would be, but make sure you don't store any iterators before the resize/reserve and then use them later; those functions can invalidate iterators.
Edited by - Stoffel on September 14, 2000 6:40:18 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement