Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the string class in c++ std work?

Tags:

c++

std

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).

  1. Why is it coded as a template?
  2. How does the template work?
  3. How does it do mem allocation?
  4. Why is (is not) better than mere null terminated char arrays?
like image 228
Dervin Thunk Avatar asked Oct 19 '10 22:10

Dervin Thunk


People also ask

How do C strings work programming?

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.

How does std::string work in C++?

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.

Is there a string class in C?

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.


1 Answers

  1. 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.

  2. Unanswerable as asked. If you're confused about something, please try to narrow it down a bit.

  3. 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.

  4. 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.

like image 137
John Dibling Avatar answered Sep 28 '22 00:09

John Dibling