Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String's data type

67 views
Skip to first unread message

JiiPee

unread,
Sep 18, 2014, 6:59:36 AM9/18/14
to
In all examples of creating a string class they always do it like this:

class String
{
public:
// functions
...
private:
char* str;
int len;
};

But listening experts they always say that we should not use the old C
array but rather vector. So why String is not done like:

class String
{
public:
// functions
...
private:
vector<char> str;
};

?
Is it somehow slower? I would actully prefer the vector as its c++.
Its like this in all C++ tutorials.

Wouter van Ooijen

unread,
Sep 18, 2014, 7:13:47 AM9/18/14
to
JiiPee schreef op 18-Sep-14 12:59 PM:
There is an good std::string, so creating a custom (heap based) string
is for educational purposes only.

Your first version uses only C-level 'first principles'. It shows how
memory can be allocated on the heap, maybe resized, that you must obey
the rule of (at least) 3 to make things work properly, etc. It shows the
*guts* of how a string class can work.

The second version relies on std::vector, which is itself implemented
somewhat like the first version, to do all the dirty work. It might
still be useful to explain the interface of the string, but it hides all
the implementation details.

So which one to choose depends on what you want to explain.

Wouter van Ooijen

JiiPee

unread,
Sep 18, 2014, 7:13:47 AM9/18/14
to
Ah, sending messages before thinking it through.

Obviously needs a character array as C because thats the representation
of strings, we cannot for example "cout<<" a vector.

My mistake...I wish I could delete these messages :)

JiiPee

unread,
Sep 18, 2014, 7:16:42 AM9/18/14
to
Ok, but I just found out myself that vector is not usefull because we
cannot do:

cout<<str;
if str is a vector. So string class must return a char* type pointer,
isnt it? But hmmmm.... can we actually get that char* from vector? I
remember reading that vector can be changed to char*, I remember correctly?

JiiPee

unread,
Sep 18, 2014, 7:19:38 AM9/18/14
to
hmmm, am still actually thinking... as I mentioned, I remember reading
that vector<char> can return a char* pointing to the first character of
the vector... have to study a bit more about it.

JiiPee

unread,
Sep 18, 2014, 7:22:29 AM9/18/14
to
Okey, but I think that is more like a question that do we need C type
arrays anymore at all?

I mean if I am teaching C++ I would like to teach the correct way to do
things. Using a C arrays would teach wrong ways to program C++ , isn't it?

On 18/09/2014 12:13, Wouter van Ooijen wrote:

JiiPee

unread,
Sep 18, 2014, 7:28:32 AM9/18/14
to
|This will return char* :
(|char| *)(&str[0])

But obviously it can only be return for reading, not modifying the vector.
This would work with the String class in all situations?

Wouter van Ooijen

unread,
Sep 18, 2014, 9:01:17 AM9/18/14
to
JiiPee schreef op 18-Sep-14 1:22 PM:
> Okey, but I think that is more like a question that do we need C type
> arrays anymore at all?
>
> I mean if I am teaching C++ I would like to teach the correct way to do
> things. Using a C arrays would teach wrong ways to program C++ , isn't it?

That depends.

If you want to teach how things work at the lowest level (as opposed to
what one should use): when you get down to the implementation it is
still C-style arrays and pointers.

When programming in a resource-rich situation (for instance a PC) using
the STL and other standard libraries is definitely a good idea.

In a resource-constraint situation (for instance real-time work on a
small micro-controller) using the same libraries might be a bad idea
because they often use the heap (which you will likely not have), and
have an unpredictable timing. (The *average* performance is generally
very good, but that does not help at all in a strict real-time situation).

Wouter van Ooijen
(who happens to be teaching C++)

Jorgen Grahn

unread,
Sep 18, 2014, 10:56:29 AM9/18/14
to
On Thu, 2014-09-18, JiiPee wrote:
> Okey, but I think that is more like a question that do we need C type
> arrays anymore at all?

Because of C compatibility, and because people are used to them.
std::vector and std::array takes care of most other uses.

However, that doesn't mean I don't want to write:

int foo(const int* p)
{
return p[42];
}

I see that as pointer arithmetics, not arrays.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

David Harmon

unread,
Sep 18, 2014, 1:22:00 PM9/18/14
to
On Thu, 18 Sep 2014 11:59:11 +0100 in comp.lang.c++, JiiPee
<n...@notvalid.com> wrote,
>But listening experts they always say that we should not use the old C
>array but rather vector. So why String is not done like:

string is older than vector.

Paavo Helde

unread,
Sep 18, 2014, 2:16:02 PM9/18/14
to
JiiPee <n...@notvalid.com> wrote in news:olzSv.276694$177.2...@fx04.am4:

>|This will return char* :
> (|char| *)(&str[0])

&vector[0] can be used for both const and mutable access, depending on
whether the vector itself is accessed as const or not.

In C++11 there is also a new data() member function of std::vector, which
does the exact same thing, except that it also works for empty vectors
(&vector[0] causes undefined behavior if the vector is empty).

Cheers
Paavo

Juha Nieminen

unread,
Sep 19, 2014, 3:14:42 AM9/19/14
to
JiiPee <n...@notvalid.com> wrote:
> But listening experts they always say that we should not use the old C
> array but rather vector. So why String is not done like:

How exactly do you think std::string is internally implemented?
If you say "with a std::vector?" then how exactly do you think
std::vector is internally implemented? Do you see how this would
result in a vicious cycle?

There's nothing wrong in using raw pointers encapsulated within
a class, as long as you know how to use them and follow good
practices to avoid the mistakes and dangers inherent to them.
They allow fine-tuning the behavior of your class.

Note, however, that if std::vector (or other similar ready-made
containers) are enough for your purposes (in terms of functionality
and efficiency), then you should definitely use them because they
make your code simpler and safer.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages