Re: Need Container for Large Binary String
From: Maciej Sobczak <no.spam_at_no.spam.com>
Date: 14 Nov 2004 11:58:19 -0500 Hi, floobles wrote:
> I need to manipulate large binary strings.
> The STL string class would be ideal, except that all its functions This is not true. There *are* functions that use 0 as a terminator (they are useful for interfacing with the "rest of the world"), but not all of them have this property. There are many string member functions that use iterators or have explicit length/size parameter. Look for them, learn them, use them.
> my binary string will have
With just a bit of care, having a std::string with embedded zeros is not a problem.
> Next ideal would be the STL vector, but it doesn't seem like there is But it takes iterators: char buf[1000]; // just for presentation std::vector<char> v(&buf[0], &buf[1000]); // no problem or
char *begin = &buf[0];
> Is it possible to do a memcpy-type initialization? But this would Yes, but this very internal property of std::vector (the fact that it stores its data in a contiguous block) is part of its interface:
std::vector<char> v(1000);
However, the memcpy approach has absolutely no advantage over the iterator-style (see above) initialization. Interestingly, iterator-style initialization may be faster, because it is a single-phase action instaed of two-phase (create a vector of given size and then memcpy).
> The MFC arrays Forget them. ;)
> I would appreciate some suggestions on which class to use. At this Use iterator-style initialization. The vector will do the looping for you. You may also have to think a bit before you choose between string and vector<char>. Both are powerful, but only vector guarantees the contiguous memory layout, so if you need to interface with some external API that expects this, then vector<char> may be the only option. On the other hand, string has a big family of find* and replace*-like functions, which means you may prefer string for this kind of processing. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/ [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]Received on Sun Nov 14 2004 - 08:54:39 PST |
Click to report inappropriate content