Note: writing this code has no other purpose. I wrote a little bit for the trainer and thought it was necessary to share it.
Use the generic idea to compile the Code. If you feel uncomfortable about the code, please leave it empty.
String class referencs: http://www.cplusplus.com/reference/string/string/
Welcome to learn and exchange! Reprinted do not forget to indicate the source (http://blog.csdn.net/zhanxinhang) ^_^
Header file my_string.hpp
/*
* Copy right by ZhanHang, 2011.7
* E-mail: huaxin.turtle@gmail.com
* QQ: 273711460
* Welcome To My Homepage (http://blog.csdn.net/zhanxinhang )!
*/
# Ifndef MY_STRING_HPP
# Define MY_STRING_HPP
# Include <malloc. h>
# Include <cstring>
# Include <cassert>
# Include <cwchar>
Template <typename charT>
Class myString
{
Public:
Typedef charT * iterator;
Private:
Size_t M_length;
Size_t M_storage_size;
Iterator M_begin;
Iterator M_end;
Private: // allocator
Iterator alloc (size_t n );
Public: // constructor and destructor
MyString (const charT *);
MyString ();
~ MyString ();
Public: // operator =
MyString & operator = (const myString &);
MyString & operator = (const charT *);
MyString & operator = (charT );
Public: // iterator
Const iterator begin () const;
Const iterator end () const;
Public: // capacity
Const size_t size () const;
Const size_t max_size () const;
Void resize (size_t, charT );
Void resize (size_t n );
Public: // element access
CharT & operator [] (size_t );
Public: // String operations
Const charT * c_str () const;
Size_t copy (charT * s, size_t n, size_t pos = 0) const;
Size_t find (const myString & str, size_t pos = 0) const;
}; // Class myString
/////////////////
// Constructor //
/////////////////
Template <typename charT>
Inline myString <charT >:: myString (const charT * str)
: M_length (strlen (char *) str)/sizeof (charT )),
M_storage_size (M_length + M_length/5 + 1 ),
M_begin (alloc (M_storage_size )),
M_end (M_begin + M_length)
{
Assert (M_length <max_size ());
Strcpy (char *) M_begin, (char *) str );
* M_end = '\ 0 ';
}
Template <typename charT>
Inline myString <charT >:: myString ()
: M_length (0 ),
M_begin (alloc (1 )),
M_end (0)
{
* M_begin = '\ 0 ';
}
////////////////
// Destructor //
////////////////
Template <typename charT>
Inline myString <charT> ::~ MyString ()
{
Free (M_begin );
}
///////////////
// Operator = //
///////////////
Template <typename charT>
Inline myString <charT> & myString <charT>: operator = (const myString & str)
{
M_length = str. size ();
Assert (M_length <max_size ());
If (M_storage_size <= M_length) // dynamically allocate the bucket
{
Free (M_begin );
M_begin = alloc (M_length + M_length/5 + 1 );
}
Strcpy (char *) M_begin, (char *) str. c_str ());
M_end = M_begin + M_length;
Return * this;
}
Template <typename charT>
Inline myString <charT> & myString <charT>: operator = (const charT * pstr)
{
M_length = strlen (char *) pstr)/sizeof (charT );
Assert (M_length <max_size ());
If (M_storage_size <= M_length)
{
Free (M_begin );
M_begin = alloc (M_length + M_length/5 + 1 );
}
Strcpy (char *) M_begin, (char *) pstr );
M_end = M_begin + M_length;
Return * this;
}
Template <typename charT>
Inline myString <charT> & myString <charT>: operator = (charT c)
{
M_length = 1;
M_end = M_begin + M_length;
If (M_storage_size <= 1)
{
Free (M_begin );
M_begin = alloc (2 );
}
* M_begin = c;
Return * this;
}
//////////////
// Iterator //
//////////////
Template <typename charT>
Inline const typename myString <charT >:: iterator myString <charT >:: begin () const
{
Return M_begin;
}
Template <typename charT>
Inline const typename myString <charT >:: iterator myString <charT >:: end () const
{
Return M_end;
}
//////////////
// Capacity //
//////////////
Template <typename charT>
Inline const size_t myString <charT>: max_size () const
{
Return size_t (-1)/sizeof (charT *)-1;
}
Template <typename charT>
Inline const size_t myString <charT>: size () const
{
Return M_length;
}
Template <typename charT>
Void myString <charT >:: resize (size_t n, charT c)
{
CharT * tmp = M_begin;
Int I;
M_begin = alloc (n + 1 );
M_end = M_begin + n;
Memcpy (char *) M_begin, (char *) tmp, (n <M_length? N: M_length) * sizeof (charT ));
M_length = strlen (char *) M_begin)/sizeof (charT );
For (I = M_length; I <n; I ++)
{
M_begin [I] = c;
}
* M_end = '\ 0 ';
Free (tmp );
}
Template <typename charT>
Void myString <charT >:: resize (size_t n)
{
CharT * tmp = M_begin;
M_begin = alloc (n + 1 );
M_end = M_begin + n;
Memcpy (char *) M_begin, (char *) tmp, (n <M_length? N: M_length) * sizeof (charT ));
M_length = strlen (char *) M_begin)/sizeof (charT );
Free (tmp );
}
////////////////////
// Element access //
////////////////////
Template <typename charT>
CharT & myString <charT>: operator [] (size_t pos)
{
Return M_begin [pos];
}
///////////////
// Allocator //
///////////////
Template <typename charT>
Inline typename myString <charT >:: iterator myString <charT >:: alloc (size_t n)
{
M_storage_size = n;
Return (charT *) calloc (n, sizeof (charT ));
}
///////////////////////
// String operations //
///////////////////////
Template <typename charT>
Inline const charT * myString <charT>: c_str () const
{
Return M_begin;
}
Template <typename charT>
Size_t myString <charT>: copy (charT * s, size_t n, size_t pos) const
{
Size_t I, j;
Assert (pos <M_length );
For (I = 0, j = pos; I <n & j <M_length; I ++, j ++)
{
S [I] = M_begin [j];
}
Return I;
}
Template <typename charT>
Inline size_t myString <charT>: find (const myString <charT> & str, size_t pos) const
{
Char * p = strstr (char *) M_begin, (char *) str. c_str ());
Return (p-(char *) M_begin)/sizeof (charT) + 1;
}
Typedef myString <char> String;
Typedef myString <wchar_t> WString;
# Endif
// Test in vs Environment
# Include <stdio. h>
# Include <tchar. h>
# Include <conio. h>
# Include <iostream>
# Include <locale. h>
# Include "my_string.hpp"
Int _ tmain (int argc, _ TCHAR * argv [])
{
Setlocale (LC_ALL, "chs ");
String st = "zh ";
String str = st;
Printf ("str = % s \ n", str. c_str (); // zxh
Wchar_t * w = L "Chinese ";
WString wstr (w );
Wprintf (L "% ls \ n", wstr. c_str (); // Chinese
Str = "zhanhang ";
Wstr = L "Chinese ";
Printf ("% s", str. c_str ());
Wprintf (L "% ls \ n", wstr. c_str (); // zhanhang is Chinese
// Operator [] method test
Printf ("% c \ n", str [2]); //
Wprintf (L "% ls \ n", & wstr [3]); // person
// Iterator method test
String: iterator it;
For (it = str. begin (); it! = Str. end (); it ++)
{
Printf ("% c", * it );
} // Zhanhang
Printf ("\ n ");
WString: iterator wit = wstr. begin ();
Wit ++;
Wprintf (L "% ls \ n", wit); // Chinese
// Resize method test
Wstr. resize (6, wstr [0]);
Wprintf (L "% ls \ n", wstr. c_str (); // yes for Chinese
Str. resize (2 );
Printf ("% s \ n", str. c_str (); // zh
Str. resize (7 ,'*');
Printf ("% s \ n", str. c_str (); // zh *****
// Find method test
Str = "zhan xin hang ";
Wstr = L "He is a Chinese ";
String key = "xin ";
WString wkey = L "Chinese ";
Printf ("% d \ n", str. find (key); // 6
Printf ("% d \ n", wstr. find (wkey); // 3
// Copy method test
Wchar_t wbuff [20] = {0 };
Char buff [20] = {0 };
Str. copy (buff, 2 );
Wstr. copy (wbuff, 5 );
Printf ("% s", buff );
Wprintf (L "% ls \ n", wbuff); // zh: Chinese
Getch ();
Return 0;
}
The result is as follows:
// Test in g ++ Environment
# Include <iostream>
# Include <cstdio>
# Include "my_string.hpp"
Int main ()
{
String st = "zh ";
String str = st;
Printf ("str = % s \ n", str. c_str (); // zxh
Wchar_t * w = (wchar_t *) "Chinese ";
WString wstr (w );
Printf ("% s \ n", wstr. c_str (); // Chinese
Str = "zhanhang ";
Wstr = (wchar_t *) "Chinese ";
Printf ("% s", str. c_str ());
Printf ("% s \ n", wstr. c_str (); // zhanhang is Chinese
// Operator [] method test
Printf ("% c \ n", str [2]); //
Printf ("% s \ n", & wstr [3]); // person
// Iterator method test
String: iterator it;
For (it = str. begin (); it! = Str. end (); it ++)
{
Printf ("% c", * it );
} // Zhanhang
Printf ("\ n ");
WString: iterator wit = wstr. begin ();
Wit ++;
Printf ("% s \ n", wit); // Chinese
// Resize method test
Wstr. resize (6, wstr [0]);
Printf ("% s \ n", wstr. c_str (); // yes for Chinese
Str. resize (2 );
Printf ("% s \ n", str. c_str (); // zh
Str. resize (7 ,'*');
Printf ("% s \ n", str. c_str (); // zh *****
// Find method test
Str = "zhan xin hang ";
Wstr = (wchar_t *) "He is Chinese ";
String key = "xin ";
WString wkey = (wchar_t *) "Chinese ";
Std: cout <str. find (key) <std: endl; // 6
Std: cout <wstr. find (wkey) <std: endl; // 3
// Copy method test
Wchar_t wbuff [20] = {0 };
Char buff [20] = {0 };
Str. copy (buff, 2 );
Wstr. copy (wbuff, 5 );
Printf ("% s, % s \ n", buff, wbuff); // zh, he is Chinese
}
The result is as follows: