Re: Character Vector vs. String (in STL)
Al <two@haik.us> wrote:
Hi all,
1) Are there major conceptual differences between:
std::vector<char> and std::string?
(or std::vector<wchar_t> / std::wstring)
std::vector<T,A> has been gauranteed for quite a while to be
contiguous and there were no knowo version that was not alrready
contiguous when the change was adopted. Std::basic_string<C,T,A> has
recently forced to be contiguous but there do exist versions of
std::string that are not contiguous and are C++98 compliant. So
if you need a contigyous, so if you need a oontiguaus array of char then
use vector and algorithms of <algorithm> if you need to use 'string
operations' use std::string. Further std::string requires the
char_type to be POD and have am std::char_traits<char_type> available
and char_traits<char_type> define another POD int_type capable of
storing char_type therein and also and eof() sentinel.
2) Is it possible/likely that these are implemented equally behind the
scenes?
It is poosible, but since the char_type is pod it probably is not
implemented that way because std::string can use bitwise copy where
std::vector can only use bitwise copy if certain conditions on the
value_type are satisified.
3) Is there any performance data available using one versus the other in
different scenarios (e.g. quick short strings and long text files)?
std::string provides an extractor to extract whitespace separated
sequences of char's into a string. and a free function getline() to
read a 'line' into a string. These do not exist for vector<char>.
Performance if either is reasonable to use is an implementation issue,
and varies.
4) Is either optimized for specific situations?
If I want a buffer of chars, I use vector<char> if I want a string
I use std::string. If the program is going to use a lot of <cstring>
functions, I use a vector<char> and push_back a '\0' to be safe.
Although it has been approved that std::basic_string is contiguous,
there is no guarantee the implementation is that up to date.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]