I'm afraid I don't know templates (or C++, really), but I know algorithms and data structures (even some OOP! :). Anyway, to make the question a bit more precise, consider what I would like to be part of the answer (among others I don't know in advance).
In C programming, a string is a sequence of characters terminated with a null character \0 . For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
There is no string type in C . You have to use char arrays. By the way your code will not work ,because the size of the array should allow for the whole array to fit in plus one additional zero terminating character.
std::string
is actually a typedef
to a std::basic_string<char>
, and therein lies the answer to your #1 above. Its a template in order to make basic_string
work with pretty much anything. char
, unsigned char
, wchar_t
, pizza
, whatever... string
itself is just a programmer convenience that uses char
as the datatype, since that's what's often wanted.
Unanswerable as asked. If you're confused about something, please try to narrow it down a bit.
There are two answers. One, from the application-layer point of view, all basic_string
objects use an allocator
object to do the actual allocation. Allocation methods may vary from one implementation to the next, and for different template parameters, but in practice they will use new
at the lower levels to allocate & manage the contained resource.
Its better than mere char arrays for a wide variety of reasons.
string
managers the memory for you. You do not have to ever allocate buffer space when you add or remove data to the string. If you add more than will fit in the currently-allocated buffer, string
will reallocate it for you behind the scenes.
In this regard, string
can be thought of as a kind of smart pointer. For the same reasons why smart pointers are better than raw pointers, string
s are better than raw char arrays.
Type safety. This may seem a little convoluted, but string
used properly has better type safety than char buffers. Consider a common scenario:
#include <string>
#include <sstream>
using namespace std;
int main()
{
const char* jamorkee_raw = "jamorkee";
char raw_buf[0x1000] = {};
sprintf( raw_buf, "This is my string. Hello, %f", jamorkee_raw);
const string jamorkee_str = "jamorkee";
stringstream ss;
ss << "This is my string. Hello " << jamorkee_str;
string s = ss.str();
}
the type safety issue raised in the above by using a raw char buffer isn't even possible when using string
along with streams.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With