hello,
I am a noob in C++ and need help about the bellow line of code:
typedef basic_string<char, char_traits<char>, allocator<char> > string;
what the above line of code means ? what it does?
Printable View
hello,
I am a noob in C++ and need help about the bellow line of code:
typedef basic_string<char, char_traits<char>, allocator<char> > string;
what the above line of code means ? what it does?
It declares a typedef of type basic_string<char, char_traits<char>, allocator<char> > named string. What specific aspect of it do you want to know about?
It defines a type under the name string. That type is an instantiation of the basic_string class template parameterized by types char, chart_traits and allocator (the last two are also class templates parameterized by type char).
Basically, you need knowledge of C++ templates and the typedef keyword in order to understand that. Check them in your favorite C++ book or on the internet.
small point but typedef does not define a type. classes and structs define types. A typedef creates an alias for a type so you can refer to a long-windedly-named-type by its alias.
I want to know what this data type store data .How this string store information and how we can manipulate it?
I want to know why we use this instead of a simple char or TCHAR or CString or other simplest forms.
I what to know why this defines at this such a form : basic_string<char, char_traits<char>, allocator<char> >
I what to know this line of code means that string is really a char or char_traits or allocator or something other?
How compiler deal with it?When compiler goes into the source code and saw a "string" then change it into basic_string<char, char_traits<char>, allocator<char> > then it means that this is a data type? like this?Code:typedef basic_string<char, char_traits<char>, allocator<char> > string;
what are these str1 and str2 exactly ?Code:string str1;
basic_string<char, char_traits<char>, allocator<char> > str2;
Get a C++ book and read about std::string.
A char is a single character. A TCHAR is also a single character and it only works for Windows. A CString only works for MFC. What if I don't work in Windows, and I need a string? Use std::string.Quote:
I want to know why we use this instead of a simple char or TCHAR or CString or other simplest forms.
Because a std::string is really a template.Quote:
I what to know why this defines at this such a form : basic_string<char, char_traits<char>, allocator<char> >
Get a book on C++ and templates. A basic_string<> is a general template class that handles sequences of characters. A character can be an 8 bit character, 16-bit character, or any type that works. A sequence of char types is a basic_string<char>.Quote:
I what to know this line of code means that string is really a char or char_traits or allocator or something other?
The char_traits<char> is another template that tells the basic_string<> template how to handle the "char" in certain places in the basic_string template function.
The allocator template tells how to allocate memory when the basic_string template asks for memory.
Now it's your turn to do the research and learn C++, as all of these questions are answered in depth and in full in C++ books, and by learning the C++ language. Randomly just looking at C++ source code, and then asking questions is not the way to learn the language.
Variables.Quote:
what are these str1 and str2 exactly ?Code:string str1;
basic_string<char, char_traits<char>, allocator<char> > str2;
Regards,
Paul McKenzie
Why you need the underlying of C++ string template ? I think you better grasp basic of C++ first.