• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            專注于c++

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              21 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(15)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            實現stl string類——轉自C++ how to program。

            // String class definition with operator overloading.
            #ifndef STRING_H
            #define STRING_H

            #include 
            <iostream>
            using std::ostream;
            using std::istream;

            class String
            {
               friend ostream 
            &operator<<( ostream &const String & );
               friend istream 
            &operator>>( istream &, String & );
            public:
               String( 
            const char * = "" ); // conversion/default constructor
               String( const String & ); // copy constructor
               ~String(); // destructor

               
            const String &operator=const String & ); // assignment operator
               const String &operator+=const String & ); // concatenation operator

               
            bool operator!() const// is String empty?
               bool operator==const String & ) const// test s1 == s2
               bool operator<const String & ) const// test s1 < s2

               
            // test s1 != s2
               bool operator!=const String &right ) const
               

                  
            return !*this == right ); 
               }
             // end function operator!=

               
            // test s1 > s2
               bool operator>const String &right ) const
               

                  
            return right < *this
               }
             // end function operator>
             
               
            // test s1 <= s2
               bool operator<=const String &right ) const
               

                  
            return !( right < *this ); 
               }
             // end function operator <=

               
            // test s1 >= s2
               bool operator>=const String &right ) const
               

                  
            return !*this < right ); 
               }
             // end function operator>=

               
            char &operator[]( int ); // subscript operator (modifiable lvalue)
               char operator[]( int ) const// subscript operator (rvalue)
               String operator()( intint = 0 ) const// return a substring
               int getLength() const// return string length
            private:
               
            int length; // string length (not counting null terminator)
               char *sPtr; // pointer to start of pointer-based string

               
            void setString( const char * ); // utility function
            }
            // end class String

            #endif
            源文件
            // String class member-function and friend-function definitions.
            #include <iostream>
            using std::cerr;
            using std::cout;
            using std::endl;

            #include 
            <iomanip>
            using std::setw;

            #include 
            <cstring> // strcpy and strcat prototypes
            using std::strcmp;
            using std::strcpy;
            using std::strcat;

            #include 
            <cstdlib> // exit prototype
            using std::exit;

            #include 
            "String.h" // String class definition

            // conversion (and default) constructor converts char * to String
            String::String( const char *s ) 
               : length( ( s 
            != 0 ) ? strlen( s ) : 0 )
            {
               cout 
            << "Conversion (and default) constructor: " << s << endl;
               setString( s ); 
            // call utility function
            }
             // end String conversion constructor

            // copy constructor
            String::String( const String &copy ) 
               : length( copy.length )
            {
               cout 
            << "Copy constructor: " << copy.sPtr << endl;
               setString( copy.sPtr ); 
            // call utility function
            }
             // end String copy constructor

            // Destructor
            String::~String()
            {
               cout 
            << "Destructor: " << sPtr << endl;
               delete [] sPtr; 
            // release pointer-based string memory
            }
             // end ~String destructor

            // overloaded = operator; avoids self assignment
            const String &String::operator=const String &right )
            {
               cout 
            << "operator= called" << endl;

               
            if ( &right != this ) // avoid self assignment
               {         
                  delete [] sPtr; 
            // prevents memory leak
                  length = right.length; // new String length
                  setString( right.sPtr ); // call utility function
               }
             // end if
               else
                  cout 
            << "Attempted assignment of a String to itself" << endl;

               
            return *this// enables cascaded assignments
            }
             // end function operator=

            // concatenate right operand to this object and store in this object
            const String &String::operator+=const String &right )
            {
               size_t newLength 
            = length + right.length; // new length
               char *tempPtr = new char[ newLength + 1 ]; // create memory

               strcpy( tempPtr, sPtr ); 
            // copy sPtr
               strcpy( tempPtr + length, right.sPtr ); // copy right.sPtr

               delete [] sPtr; 
            // reclaim old space
               sPtr = tempPtr; // assign new array to sPtr
               length = newLength; // assign new length to length
               return *this// enables cascaded calls
            }
             // end function operator+=

            // is this String empty?
            bool String::operator!() const

               
            return length == 0
            }
             // end function operator! 

            // Is this String equal to right String?
            bool String::operator==const String &right ) const

               
            return strcmp( sPtr, right.sPtr ) == 0
            }
             // end function operator==

            // Is this String less than right String?
            bool String::operator<const String &right ) const

               
            return strcmp( sPtr, right.sPtr ) < 0
            }
             // end function operator<

            // return reference to character in String as a modifiable lvalue
            char &String::operator[]( int subscript )
            {
               
            // test for subscript out of range
               if ( subscript < 0 || subscript >= length )
               
            {
                  cerr 
            << "Error: Subscript " << subscript 
                     
            << " out of range" << endl;
                  exit( 
            1 ); // terminate program
               }
             // end if

               
            return sPtr[ subscript ]; // non-const return; modifiable lvalue
            }
             // end function operator[]

            // return reference to character in String as rvalue
            char String::operator[]( int subscript ) const
            {
               
            // test for subscript out of range
               if ( subscript < 0 || subscript >= length )
               
            {
                  cerr 
            << "Error: Subscript " << subscript 
                       
            << " out of range" << endl;
                  exit( 
            1 ); // terminate program
               }
             // end if

               
            return sPtr[ subscript ]; // returns copy of this element
            }
             // end function operator[]

            // return a substring beginning at index and of length subLength
            String String::operator()( int index, int subLength ) const
            {
               
            // if index is out of range or substring length < 0, 
               
            // return an empty String object
               if ( index < 0 || index >= length || subLength < 0 )  
                  
            return ""// converted to a String object automatically

               
            // determine length of substring
               int len;

               
            if ( ( subLength == 0 ) || ( index + subLength > length ) )
                  len 
            = length - index;
               
            else
                  len 
            = subLength;

               
            // allocate temporary array for substring and 
               
            // terminating null character
               char *tempPtr = new char[ len + 1 ];

               
            // copy substring into char array and terminate string
               strncpy( tempPtr, &sPtr[ index ], len );
               tempPtr[ len ] 
            = '\0';

               
            // create temporary String object containing the substring
               String tempString( tempPtr );
               delete [] tempPtr; 
            // delete temporary array
               return tempString; // return copy of the temporary String
            }
             // end function operator()

            // return string length
            int String::getLength() const 

               
            return length; 
            }
             // end function getLength

            // utility function called by constructors and operator=
            void String::setString( const char *string2 )
            {
               sPtr 
            = new char[ length + 1 ]; // allocate memory

               
            if ( string2 != 0 ) // if string2 is not null pointer, copy contents
                  strcpy( sPtr, string2 ); // copy literal to object
               else // if string2 is a null pointer, make this an empty string
                  sPtr[ 0 ] = '\0'// empty string
            }
             // end function setString 

            // overloaded output operator
            ostream &operator<<( ostream &output, const String &s )
            {
               output 
            << s.sPtr;
               
            return output; // enables cascading
            }
             // end function operator<<

            // overloaded input operator
            istream &operator>>( istream &input, String &s )
            {
               
            char temp[ 100 ]; // buffer to store input
               input >> setw( 100 ) >> temp;
               s 
            = temp; // use String class assignment operator
               return input; // enables cascading
            }
             // end function operator>>
            ***********************************測試的代碼****************************************************
            // String class test program.
            #include <iostream>
            using std::cout;
            using std::endl;
            using std::boolalpha;

            #include 
            "String.h"

            int main()
            {
               String s1( 
            "happy" );
               String s2( 
            " birthday" );
               String s3;

               
            // test overloaded equality and relational operators
               cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2
                  << "\"; s3 is \"" << s3 << '\"' 
                  
            << boolalpha << "\n\nThe results of comparing s2 and s1:"
                  
            << "\ns2 == s1 yields " << ( s2 == s1 )
                  
            << "\ns2 != s1 yields " << ( s2 != s1 )
                  
            << "\ns2 >  s1 yields " << ( s2 > s1 )
                  
            << "\ns2 <  s1 yields " << ( s2 < s1 )
                  
            << "\ns2 >= s1 yields " << ( s2 >= s1 )
                  
            << "\ns2 <= s1 yields " << ( s2 <= s1 );
                  

               
            // test overloaded String empty (!) operator
               cout << "\n\nTesting !s3:" << endl;

               
            if ( !s3 )
               
            {
                  cout 
            << "s3 is empty; assigning s1 to s3;" << endl;
                  s3 
            = s1; // test overloaded assignment
                  cout << "s3 is \"" << s3 << "\"";
               }
             // end if

               
            // test overloaded String concatenation operator
               cout << "\n\ns1 += s2 yields s1 = ";
               s1 
            += s2; // test overloaded concatenation
               cout << s1;

               
            // test conversion constructor
               cout << "\n\ns1 += \" to you\" yields" << endl;
               s1 
            += " to you"// test conversion constructor
               cout << "s1 = " << s1 << "\n\n";

               
            // test overloaded function call operator () for substring
               cout << "The substring of s1 starting at\n"
                  
            << "location 0 for 14 characters, s1(0, 14), is:\n"
                  
            << s1( 014 ) << "\n\n";

               
            // test substring "to-end-of-String" option
               cout << "The substring of s1 starting at\n"
                  
            << "location 15, s1(15), is: "
                  
            << s1( 15 ) << "\n\n"

               
            // test copy constructor
               String *s4Ptr = new String( s1 );  
               cout 
            << "\n*s4Ptr = " << *s4Ptr << "\n\n";

               
            // test assignment (=) operator with self-assignment
               cout << "assigning *s4Ptr to *s4Ptr" << endl;
               
            *s4Ptr = *s4Ptr; // test overloaded assignment
               cout << "*s4Ptr = " << *s4Ptr << endl;

               
            // test destructor
               delete s4Ptr;     

               
            // test using subscript operator to create a modifiable lvalue
               s1[ 0 ] = 'H';      
               s1[ 
            6 ] = 'B';
               cout 
            << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: "
                  
            << s1 << "\n\n";

               
            // test subscript out of range
               cout << "Attempt to assign 'd' to s1[30] yields:" << endl;
               s1[ 
            30 ] = 'd'// ERROR: subscript out of range
               return 0;
            }
             // end main
            posted on 2009-09-26 22:44 bellgrade 閱讀(4985) 評論(0)  編輯 收藏 引用
            7国产欧美日韩综合天堂中文久久久久 | 亚洲国产综合久久天堂| 久久国产成人精品麻豆| 午夜欧美精品久久久久久久| 久久久国产视频| 亚洲乱码精品久久久久.. | 久久国产精品久久国产精品| 久久精品国产网红主播| 国产精品免费福利久久| 久久99国产精品久久99| 国产成人久久精品二区三区| 精品久久人人做人人爽综合| 国产精品综合久久第一页| 久久99精品久久久久久9蜜桃 | 久久久久综合中文字幕| 日本国产精品久久| 久久亚洲日韩看片无码| 伊人久久大香线蕉综合Av | 国产成人无码久久久精品一| 国产精品久久毛片完整版| 国产精品免费久久久久影院| 亚洲国产精品无码久久青草 | 久久99久久99精品免视看动漫| 久久久久亚洲AV成人网人人网站 | 国内精品伊人久久久久影院对白| 久久一区二区免费播放| 波多野结衣久久一区二区| 久久精品国产亚洲av影院| 国产精品久久久久久久午夜片| 午夜精品久久久久成人| 久久99精品久久久久久久不卡| 亚洲国产成人久久精品动漫| 久久福利资源国产精品999| 精品一区二区久久| 日本WV一本一道久久香蕉| 国产91久久精品一区二区| 精品久久久久久无码不卡| 久久福利青草精品资源站| 伊人久久大香线蕉av不变影院| 国产精品久久久久影视不卡| 久久福利资源国产精品999|