string vs. vector<char>

T

thomas

Hi,
In my mind, string and vector<char> are equivalent in some sense.
But I have found that the vector operation "pop_back()" is missing
for "string". I prefer the "string" to "vector<char>" when playing
strings. But I will need the "pop_back()" operation from time to
time.
I know that "resize(str.size()-1)" is a workaround, but I'm not
sure if there is any efficiency loss compared with "pop_back()" etc.
Thanks.

Thomas.
 
I

Ian Collins

thomas said:
Hi,
In my mind, string and vector<char> are equivalent in some sense.
But I have found that the vector operation "pop_back()" is missing
for "string". I prefer the "string" to "vector<char>" when playing
strings. But I will need the "pop_back()" operation from time to
time.
I know that "resize(str.size()-1)" is a workaround, but I'm not
sure if there is any efficiency loss compared with "pop_back()" etc.

I'd have thought the vector equivalent would be to erase the second from
last item, assuming your "string" is null terminated.
 
T

thomas

I'd have thought the vector equivalent would be to erase the second from
last item, assuming your "string" is null terminated.

so you mean that a "string" is null terminated. If a "pop_back()"
operation exists, there's confusion that whether the last character or
'\0' is poped out.

en.. I think it makes sense.
 
T

thomas

so you mean that a "string" is null terminated. If a "pop_back()"
operation exists, there's confusion that whether the last character or
'\0' is poped out.

en.. I think it makes sense.

By the way, I am not sure how the "string" is designed and
implemented. But I think the iterator operations (string.begin(),
string.end(), etc.) should have been specially handled for strings. Or
how can a "string.end()" tell whether it's after the '\0' or right at
the position of '\0'?
 
R

Richard Herring

Do you need any of the string-specific functions of std::string?

If not, there's no reason not to use vector<char>. That means you also
get member function back() and push_back(), which form a conceptual set
with pop_back().

I wouldn't expect to see any significant difference in any sane
implementation of std::string, but the only way to find out is to
measure it.

The main loss is of clarity: pop_back() spells out exactly what you
mean, without provoking the reader to muse on possible out-by-1 errors:
I'd have thought the vector equivalent would be to erase the second
from last item, assuming your "string" is null terminated.

Assuming the OP is talking about std::string, it isn't.
 
R

Richard Herring

In message

No. In the STL container model, pop_back() doesn't return a value, it
simply decrements the effective position of end().
By the way, I am not sure how the "string" is designed and
implemented. But I think the iterator operations (string.begin(),
string.end(), etc.) should have been specially handled for strings. Or
how can a "string.end()" tell whether it's after the '\0' or right at
the position of '\0'?

It can't and doesn't. end() simply points to size() elements beyond
begin().

In std::string, '\0' is not a special character, and str[str.size()-1]
is not generally '\0' unless you deliberately make it so.

It's only the result of c_str() that is guaranteed to have a terminating
null added at offset size().
 
J

James Kanze

thomas wrote:
I'd have thought the vector equivalent would be to erase the
second from last item, assuming your "string" is null
terminated.

Why would the string be null terminated? A vector knows its
size, just as string does.

In practice, I use std::string for strings, because that's what
readers expect. And because of the implicit conversion of
string literals. Otherwise, std::vector<char> works just as
well, and in certains cases, offers complexity guarantees that
std::string doesn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top