hi guys
i have a little question
i don't understand this line
typedef basic_string <char> string ;
i have searched but can't get it any one can help
ur help will be appreciated

Dani AI

Generated

— the line you asked about is simply giving a short name to a particular instantiation of the library string template. C++ uses angle brackets to supply template type parameters; parentheses are used for function calls or constructors, so you would not write the form you guessed. As and noted, this is the same idea as creating an alias for a built-in type, and ’s comparison to raw character buffers is a helpful intuition but not exact: the standard string type is an object with behavior, not a raw pointer.

A bit more detail without repeating prior snippets: the standard string facility is built on a class template that looks roughly like this:

template<class CharT, class Traits = std::char_traits<CharT>,
         class Allocator = std::allocator<CharT>>
class basic_string;

The common string typedef in practice is just the template instantiated for char with the default traits and allocator. That instantiation provides member functions (length queries, concatenation, substring, c_str() for a null-terminated view, etc.) and manages its own memory — so copies, assignments and lifetime are handled by the class.

Practical notes: always include the string header, prefer using the standard std::string name in code, and in modern C++ prefer using aliases if you make your own. Be aware that implementations often use small-string-optimization (SSO) so short text may not allocate on the heap. For the authoritative template declaration and member list, see the library reference: basic_string — cppreference.

Recommended Answers

All 4 Replies

>i don't understand this line typedef basic_string <char> string;

basic_string class is parameterized by character type. There is no need to use the basic_string template directly. The types string and wstring are typedefs for, respectively, basic_string<char> and basic_string<wchar_t>.

See article.

That line is basically creating a new variable type for you called string, the basic_string<char> is something you dont need to worry about, I will just guess though that you included <string> into your program. string is just another way of saying char*, but it is less of an array and more of a vector, at least in my opinion ;). With that line, now you can make something like this:

string mystring = "HelloWorld!\n";
cout << mystring;

Hope that was helpful

- Pranav Sathyanarayanan

thanks adatapost and WargRider so much
but the question is about basic string<char>
i am not professional in c++
but all my knowledge
was that we can write typedef int integer
something like this
i know basic string is a class but why we didn't write it in this way

basic_string (char)
and thanks again ur posts were so helpful guys : )

Do you know what this means :

typedef unsigned int uInt //?

Do you know what a class is?

Then all this

typedef basic_string <char> string

is, is a typedef for a template class called basic_string;

Its the same as this :

typedef int INT

in concept, but instead of int they substitute it for a template class.

commented: Helpful! +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.