开发者

What is the difference between the C string and C++ string?

I 开发者_JAVA技巧mean what is the difference of string in C and C++?


C does not define string: it only has "perfectly ordinary arrays of characters" and pointers to those arrays;

C++ defines it, as a class type, with several properties and methods.


In C there is no such thing/type as "string". It is represented as NULL terminated array of characters like char str[256];. C++ has string class in standard library that internally maintains it as array of characters and has many methods and properties to manipulate it.


I fully agree with @pmg answer. But one need to mention some things. In C programmer must be very careful when he works with C-strings because a) every C-string must be ended with zero code character; b) it is very easy to make buffer overrun if buffer size for string is too small. Also in C all work with strings goes through functions. It may be programmers nightmare. In C++ things are much simpler. Firstly, you don't need to care about memory management. String class allocate additional memory when internal buffer becomes small. Secondly, you don't need to care about zero terminating character. You work with container. Thirdly, there are simple methods for working with string class. For example, overloaded operator + for string concatenation. No more awful strcat() calls. Let the work with strings to be simple!


in C++ String objects are a special type of container, specifically designed to operate with sequences of characters.string class defined in string

or in C string is a character sequence terminated with a null character ('\0'), all functions related to strings defined in string.h

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜