Comparison between string and char * -- why use string

Source: Internet
Author: User

IStringAndChar *Comparison

1 string is a class, and char * is a pointer to the char type.

String encapsulates char * and manages this string to encapsulate char *. It is a char * type container, which is flexible and easy to expand functions.

2. Do not consider memory release and out-of-bounds

String encapsulates char * and manages char * strings and the memory allocated to char.

For each string copy, the value is maintained by the string class. Do not worry about copying out-of-bounds or out-of-range values.

3 string operations

Overload OPERATOR:

String a; string B; string C;

Addition operation: c = A + B;

Comparison: A> B,! = B;

4. Series string operation functions

Find, copy, and delete

Replace and insert

......

5. support expansion

Use static or dynamic memory

Use resource ID to load string

Operation Security lock

The essential difference is: StringIs a class, charIs the basic type, stringEncapsulated char.

IIStringImplementation and Application

The C ++ standard library STL provides string implementation.

Here, let's take a look at the implementation method of string in my work platform. I feel that it is well written and worth learning.

 Class String
{
Public :
// Default constructor
String (): m_buftype (buf_type_none), m_buf (null)
{
}

// Initialized with resource ID
String ( Const U32 ID );

// Copy constructor
String (Const String & other ):
M_buftype (buf_type_none ),
M_buf (null)
{
Assignwith (other );
}

// Destructor
~ String ()
{
// Ensure that the allocated memory can be released
Clear ();
}

// Operator basic operation [], =, *, =, + = ,......
Public :
// Returns: The character at given Position Index
Wchar Operator [] (S32 index) Const
{
Return Getitem (INDEX );
}

// Returns: the buffer of the string
String & Operator = ( Const String & other );

// Returns: the buffer of the string.
Operator Const Wchar *() Const
{
Return M_buf;
}

// Returns: Return vfx_true the string is equal to the other.
Bool Operator = ( Const String & other) Const
{
Return Comparewith (other) = 0 ;
}

// Returns: Return vfx_true the string is ** not * equal to the other.
BoolOperator ! = ( Const String & other) Const
{
Return Comparewith (other )! = 0 ;
}

// Returns: Reference to the string
String & Operator + = ( Const String & other)
{
Appendwith (other );
Return * This ;
}

// Method
Public :
// Returns: The character at given Position Index
Wchar getitem (
S32 Index // [In] the Position Index to get
) Const
{
Return M_buf [Index];
}

// See also: unlockbuf
Wchar * lockbuf (
Vfxu32 initiallength // [In] the initial buffer length, included zero terminal.
);

// See also: lockbuf
Void Unlockbuf ();

// Returns: the buffer of the string
Const Wchar * getbuf () Const
{
Return M_buf;
}

// Returns: the charactor numbers of the string.
U32 getlength () Const ;

// Returns: Return vfx_true if the string is empty or null
Bool isempty () Const
{
Return M_buf? (M_buf [ 0 ] = 0 ): Vfx_true;
}

// Returns: Reference to the string
String & setempty ();

// Returns: Return vfx_true if the string is null
Vfxbool isnull () Const
{
Return M_buf = NULL;
}

// Returns: Reference to the string
String & setnull ()
{
Clear ();
Return * This ;
}

// Returns: Return vfx_true if the string buffer is dynamic
Bool isdynamic () Const
{
Return Vfx_flag_has (m_buftype, buf_type_dynamic );
}

// Returns: Reference to the string
String & loadfromres (resid res_id );

// Returns: Reference to the string
String & loadfrommem ( Const Wchar * MEm );

// Returns: Reference to the string
String & format ( Const Wchar * format ,...);

// Returns: Reference to the string
String & format ( Const Wchar * format ,...);

Protected :
Const Wchar * clonebuf ( Const Wchar * BUF );
// Implementation
Private :
Buftypeenum m_buftype;
Const Wchar * m_buf;

// Helper Methods
Void Clear ();
Void Assignwith ( Const String & ohter );
Void Appendwith ( Const String & ohter );
S32 comparewith ( Const String & Str) Const ;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.