青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

專注于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 閱讀(5003) 評論(0)  編輯 收藏 引用
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲第一区在线观看| 亚洲人成人一区二区在线观看| 亚洲一级黄色片| 亚洲美女区一区| 日韩视频免费在线| 亚洲精品午夜精品| 日韩亚洲精品电影| 一区二区三区四区国产精品| 亚洲永久精品大片| 久久久之久亚州精品露出| 麻豆精品网站| 欧美日韩在线直播| 国产精品自在在线| 亚洲国产精品日韩| 亚洲一区二区三区免费视频| 欧美亚洲免费在线| 欧美高清不卡| 亚洲小视频在线观看| 久久亚洲图片| 国产精品久久久| 伊人夜夜躁av伊人久久| 亚洲视频一区二区| 欧美成人精品一区二区| 这里只有精品视频| 久久亚洲欧美| 国产日韩精品在线播放| 99视频精品免费观看| 欧美专区在线播放| 日韩一级在线观看| 久久综合伊人77777| 国产精品久久午夜夜伦鲁鲁| 在线看欧美视频| 亚洲欧美激情精品一区二区| 亚洲成人在线网站| 香港成人在线视频| 欧美日韩另类在线| 亚洲激情电影在线| 久久久一本精品99久久精品66| 亚洲精品在线一区二区| 美女黄毛**国产精品啪啪| 国产女主播一区二区三区| 亚洲精品资源| 另类av一区二区| 午夜亚洲性色视频| 欧美小视频在线观看| 亚洲美女福利视频网站| 欧美91视频| 久久久免费精品视频| 国产精品一区视频| 午夜在线观看免费一区| 99国产精品一区| 欧美精品一区二区三区在线看午夜 | 久久人人97超碰精品888| 99国产精品久久久久久久| 免播放器亚洲一区| 狠狠色狠狠色综合日日五| 欧美一区亚洲| 欧美一级视频免费在线观看| 国产欧美日韩亚洲一区二区三区| 亚洲免费在线播放| av不卡免费看| 欧美视频不卡| 亚洲一区二区三区精品在线| 一本色道久久88精品综合| 欧美全黄视频| 亚洲图中文字幕| 亚洲一区bb| 国产午夜精品全部视频在线播放| 久久国产天堂福利天堂| 性欧美video另类hd性玩具| 国产日韩欧美在线看| 久久久久网址| 欧美电影免费观看高清完整版| 亚洲黄色小视频| 亚洲裸体在线观看| 国产精品夜夜夜| 男女精品视频| 欧美日韩国产天堂| 亚洲你懂的在线视频| 新狼窝色av性久久久久久| 黄色成人在线免费| 亚洲高清在线| 国产精品久久久91| 久久综合九色欧美综合狠狠| 麻豆免费精品视频| 亚洲五月婷婷| 久久激五月天综合精品| 亚洲人成人一区二区三区| 一区二区av在线| 精品av久久久久电影| 亚洲激情在线观看视频免费| 欧美视频一区二区三区| 久久精品国产欧美激情| 另类尿喷潮videofree| 一本久道久久综合狠狠爱| 亚洲欧美日韩在线不卡| 亚洲国产精品一区二区尤物区 | 亚洲精品一区二区三区不| 国产精品揄拍一区二区| 欧美成人dvd在线视频| 欧美日韩综合在线| 噜噜噜噜噜久久久久久91 | 久久人人97超碰国产公开结果| 欧美国产欧美综合| 欧美与黑人午夜性猛交久久久| 欧美电影免费观看高清| 久久爱另类一区二区小说| 免费亚洲电影在线| 欧美一区二区在线| 欧美视频成人| 亚洲国产精品成人久久综合一区| 国产精品大片免费观看| 欧美高清一区| 极品av少妇一区二区| 亚洲一区国产精品| 一本色道久久综合亚洲精品不| 久久久91精品| 欧美在线观看一区二区| 欧美日韩一区在线| 亚洲国产精品激情在线观看 | 欧美特黄一区| 美女诱惑黄网站一区| 国产美女精品视频| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 午夜精品一区二区三区在线播放| 欧美精品www| 欧美国产另类| 尤物在线精品| 久久激五月天综合精品| 久久激情视频免费观看| 欧美三日本三级少妇三2023| 亚洲狠狠丁香婷婷综合久久久| 国模私拍视频一区| 欧美亚洲网站| 欧美在线播放一区| 国产模特精品视频久久久久| 中文无字幕一区二区三区| 一区二区三区免费网站| 欧美日韩亚洲高清一区二区| 亚洲乱亚洲高清| 亚洲午夜视频在线观看| 欧美视频网站| 亚洲综合视频一区| 久久精品国产91精品亚洲| 国产午夜亚洲精品羞羞网站| 先锋资源久久| 免费在线观看日韩欧美| 亚洲第一主播视频| 欧美成人精品一区二区三区| 最新中文字幕亚洲| 亚洲欧美国产精品va在线观看| 国产精品影院在线观看| 久久精品国产第一区二区三区最新章节 | 亚洲自拍都市欧美小说| 欧美综合77777色婷婷| 国产综合婷婷| 欧美成人情趣视频| 日韩视频永久免费| 午夜视频久久久久久| 国语自产精品视频在线看抢先版结局| 久久高清国产| 亚洲区第一页| 香蕉久久夜色精品| 禁久久精品乱码| 欧美精品综合| 翔田千里一区二区| 亚洲国产一区二区三区a毛片| 亚洲一区二区三区四区中文| 国产一区二区高清视频| 欧美粗暴jizz性欧美20| 亚洲一级黄色av| 亚洲春色另类小说| 欧美一级欧美一级在线播放| 亚洲国产精品专区久久| 国产精品chinese| 久久婷婷综合激情| 一区二区三区黄色| 蜜桃精品一区二区三区| 在线午夜精品自拍| 在线观看亚洲视频啊啊啊啊| 欧美视频日韩视频| 模特精品在线| 欧美一区二区高清| 99热免费精品| 亚洲国产婷婷| 久久久亚洲精品一区二区三区| 亚洲一区二区在线播放| 亚洲精品国产视频| 国产真实乱偷精品视频免| 欧美特黄视频| 欧美精品一区二区三区久久久竹菊| 欧美一区二区免费观在线| 99在线精品视频在线观看| 欧美成人综合| 蜜臀av国产精品久久久久| 久久国产66| 久久成人精品电影| 香蕉久久夜色精品| 欧美一区二区三区成人| 亚洲视屏在线播放|