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

專(zhuān)注于c++

  C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
  21 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

常用鏈接

留言簿(15)

我參與的團(tuán)隊(duì)

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

實(shí)現(xiàn)stl string類(lèi)——轉(zhuǎn)自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>>
***********************************測(cè)試的代碼****************************************************
// 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 閱讀(5001) 評(píng)論(0)  編輯 收藏 引用

只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲小说春色综合另类电影| 欧美日本精品在线| 免费看亚洲片| 久久婷婷国产综合精品青草| 久久精品国产久精国产一老狼| 欧美亚洲一级片| 久久精品国产亚洲一区二区三区| 性做久久久久久久免费看| 欧美一级在线亚洲天堂| 久久视频免费观看| 亚洲日本在线视频观看| 亚洲激情精品| 亚洲一区久久久| 久久久久久久国产| 欧美精品在线视频| 国产精品女主播| 狠狠色丁香婷婷综合久久片| 欧美精品久久99久久在免费线| 日韩视频精品| 亚洲第一区在线| aa级大片欧美三级| 午夜视频一区在线观看| 牛人盗摄一区二区三区视频| 亚洲精品在线免费观看视频| 午夜久久久久| 欧美日韩国产美女| 国内精品久久久| 一区二区久久| 欧美v国产在线一区二区三区| 日韩亚洲欧美综合| 久久视频国产精品免费视频在线 | 亚洲欧美在线aaa| 久久久久国产精品午夜一区| 欧美激情一区二区久久久| 亚洲影院在线观看| 欧美激情1区2区| 一色屋精品视频在线看| 亚洲欧美卡通另类91av| 最新亚洲电影| 久久久久国产精品一区二区| 国产精品一区二区三区四区五区 | 欧美激情久久久| 国产欧美日韩在线播放| 这里只有精品丝袜| 亚洲电影观看| 欧美一区二区三区日韩视频| 欧美日韩精品久久久| 激情综合自拍| 久久黄金**| 亚洲欧美日本伦理| 国产精品一区二区男女羞羞无遮挡| 亚洲免费大片| 亚洲国产精品传媒在线观看| 久久综合导航| 在线日本成人| 欧美成在线视频| 久久蜜桃av一区精品变态类天堂| 国产视频精品xxxx| 久久精品99国产精品| 小黄鸭精品密入口导航| 国产精品每日更新| 午夜一区二区三区不卡视频| 亚洲香蕉网站| 国产欧美一区二区精品仙草咪| 午夜精品久久99蜜桃的功能介绍| 一区二区日韩| 国产精品一区二区三区久久久| 午夜综合激情| 久久久噜噜噜久久狠狠50岁| 在线日韩av片| 亚洲精品久久久久久下一站| 欧美激情综合五月色丁香| 91久久在线| 国产日韩欧美视频在线| 亚洲毛片在线看| 欧美高清不卡| 欧美成人免费在线视频| 亚洲美女精品一区| 99视频一区| 国产欧美大片| 美日韩精品免费观看视频| 久久综合九色99| 99视频在线精品国自产拍免费观看| 亚洲精品无人区| 国产精品推荐精品| 美女诱惑一区| 欧美日韩999| 久久国产精品一区二区三区| 久久久免费精品视频| 日韩视频在线观看免费| 一区二区三区视频在线| 国外成人网址| 日韩亚洲精品在线| 国产午夜久久久久| 亚洲国产日韩欧美在线图片| 国产乱码精品一区二区三区不卡| 久久综合九色欧美综合狠狠| 欧美激情网友自拍| 久久精品日产第一区二区三区| 麻豆精品视频在线| 亚洲欧美在线aaa| 看片网站欧美日韩| 亚洲天堂男人| 久久亚洲国产精品日日av夜夜| 99热在这里有精品免费| 亚洲欧美成人一区二区三区| 亚洲国产日韩在线| 这里是久久伊人| 国产在线精品二区| 一区二区久久| 好看的av在线不卡观看| 日韩视频一区二区三区在线播放免费观看 | 狠狠色丁香婷婷综合久久片| 亚洲精品一区二区三区蜜桃久| 国产精品尤物| 夜夜嗨av一区二区三区四区| 亚洲国产精品第一区二区| 午夜精品久久久久久99热软件 | 欧美精品在线视频观看| 老司机成人在线视频| 国产精品福利在线| 亚洲日本一区二区三区| 亚洲电影观看| 中文av一区特黄| 亚洲高清av| 国产精品久久久久久久久久久久久| 六月婷婷久久| 国产精品激情av在线播放| 欧美黄色aaaa| 亚洲国产成人精品久久| 久久精品国产免费| 亚洲自拍偷拍一区| 欧美黄色视屏| 亚洲激情婷婷| 亚洲七七久久综合桃花剧情介绍| 久久精品国产一区二区三区免费看| 午夜精品久久久| 国产伪娘ts一区| 欧美亚洲在线视频| 久久免费黄色| 亚洲国产小视频在线观看| 久久一二三国产| 欧美激情欧美狂野欧美精品| 亚洲精品123区| 欧美国产丝袜视频| 亚洲毛片播放| 欧美一级片一区| 黄色小说综合网站| 免费亚洲一区| 亚洲美女黄色片| 亚洲欧美精品在线| 国产视频在线观看一区二区| 欧美专区亚洲专区| 美女脱光内衣内裤视频久久影院 | 亚洲电影中文字幕| 欧美成人精品一区二区| 亚洲精品美女在线观看| 一区二区三区四区在线| 国产精品99免视看9| 亚洲永久免费| 久久婷婷影院| 亚洲国产一区二区三区高清 | 尹人成人综合网| 女生裸体视频一区二区三区| 亚洲激情av在线| 99精品久久免费看蜜臀剧情介绍| 欧美日本一区二区高清播放视频| 99av国产精品欲麻豆| 久久成人免费日本黄色| 亚洲夫妻自拍| 欧美三级网页| 久久精品盗摄| 99精品视频一区| 久久综合图片| 亚洲在线日韩| 亚洲黄色免费| 国产欧美日韩免费看aⅴ视频| 麻豆freexxxx性91精品| 亚洲一区二区av电影| 欧美国产日产韩国视频| 午夜精品久久久久久久久久久久久| 国产一区二区毛片| 欧美成人免费全部观看天天性色| 宅男66日本亚洲欧美视频| 国产亚洲一区在线播放| 欧美日本精品| 久久久一二三| 久久婷婷国产综合精品青草| 亚洲女同精品视频| 免费短视频成人日韩| 亚洲午夜激情| 亚洲国产精品久久久久婷婷884| 欧美视频在线观看免费网址| 久久综合久久88| 宅男噜噜噜66一区二区66| 欧美aaaaaaaa牛牛影院| 亚洲欧美日韩综合国产aⅴ| 亚洲美女中出| 曰韩精品一区二区| 国产精品高潮呻吟|