自定义string类
//自定义String类 注意分别编译///String.h
#ifndef MY_String_H_
#define MY_String_H_
#include<iostream>
using std::istream;
using std::ostream;
class String
{
public:
String( const char *cString = "" ); // Constructor
String( char ch ); // Constructor
String( const String & str ); // Copy constructor
~String( ) // Destructor
{ delete [ ] buffer; }
const String & operator= ( const String & rhs ); // Copy
const String & operator+=( const String & rhs ); // Append
const char *c_str( ) const // Return C-style String
{ return buffer; }
int length( ) const // Return String length
{ return strLength; }
char operator[]( int k ) const; // Accessor operator[]
char & operator[]( int k ); // Mutator operator[]
friend istream & operator>>( istream & in, String & str ); // Input
private:
char *buffer; // storage for characters
int strLength; // length of String (# of characters)
int bufferLength; // capacity of buffer
};
ostream & operator<<( ostream & out, const String & str ); // Output
istream & getline( istream & in, String & str ); // Read line
istream & getline( istream & in, String & str, char delim ); // Read line
String operator+( const String & lhs, const String & rhs ); // Concatenation
bool operator==( const String & lhs, const String & rhs ); // Compare ==
bool operator!=( const String & lhs, const String & rhs ); // Compare !=
bool operator< ( const String & lhs, const String & rhs ); // Compare <
bool operator<=( const String & lhs, const String & rhs ); // Compare <=
bool operator> ( const String & lhs, const String & rhs ); // Compare >
bool operator>=( const String & lhs, const String & rhs ); // Compare >=
#endif
///String.cpp
#include <cstring>
#include <cctype>
#include "String.h"
using std::cerr;
String::String( const char * cString )
{
if( cString == NULL )
cString = "";
strLength = strlen( cString );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cString );
}
String::String( const String & str )
{
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, str.buffer );
}
String::String( char ch )
{
strLength = 1;
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
buffer[ 0 ] = ch;
buffer[ 1 ] = '\0';
}/*
const String & String::operator=( const String & rhs )
{
if( this != &rhs )
{
if( bufferLength < rhs.length( ) + 1 )
{
delete [ ] buffer;
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
}
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
}
return *this;
}*/
const String& String::operator=(const String& rhs)
{
if(this==&rhs)
return *this;
delete []buffer;
strLength=rhs.length();
bufferLength=strLength+1;
buffer=new char[bufferLength];
strcpy(buffer,rhs.buffer);
return *this;
}
/*
const String & String::operator+=( const String & rhs )
{
if( this == &rhs )
{
String copy( rhs );
return *this += copy;
}
int newLength = length( ) + rhs.length( );
if( newLength >= bufferLength )
{
bufferLength = 2 * ( newLength + 1 );
char *oldBuffer = buffer;
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldBuffer;
}
strcpy( buffer + length( ), rhs.buffer );
strLength = newLength;
return *this;d
}*/
const String& String::operator+=(const String& rhs)
{
strLength+=rhs.length();
bufferLength=strLength+1;
char* oldBuffer=buffer;
buffer=new char[bufferLength];
strcpy(buffer,oldBuffer);
delete []oldBuffer;
strcat(buffer,rhs.buffer);
return *this;
}
char & String::operator[ ]( int k )
{
if( k < 0 || k >= strLength )
cerr<<"StringIndexOutOfBoundsException( k, length( ) )";
return buffer[ k ];
}
char String::operator[ ]( int k ) const
{
if( k < 0 || k >= strLength )
cerr<<"StringIndexOutOfBoundsException( k, length( ) )";
return buffer[ k ];
}
String operator+( const String & lhs, const String & rhs )
{
String result = lhs;
result += rhs;
return result;
}
ostream & operator<<( ostream & out, const String & str )
{
return out << str.c_str( );
}/*
istream & operator>>( istream & in, String & str )
{
char ch[256];
in>>ch;
str.strLength=strlen(ch);
str.bufferLength=str.strLength+1;
delete []str.buffer;
str.buffer=new char[str.bufferLength];
strcpy(str.buffer,ch);
return in;
}*/
istream & operator>>( istream & in, String & str )
{
char ch;
str = "";
in >> ch;
if( !in.fail( ) )
{
do
{
str += ch;
in.get( ch );
} while( !in.fail( ) && !isspace( ch ) );
if( isspace( ch ) ) // put whitespace back on the stream
in.putback( ch );
}
return in;
}
istream & getline( istream & in, String & str, char delim )
{
char ch;
str = ""; // empty String, will build one char at-a-time
while( in.get( ch ) && ch != delim )
str += ch;
return in;
}
istream & getline( istream & in, String & str )
{
return getline( in, str, '\n' );
}
bool operator==( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;
}
bool operator!=( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;
}
bool operator<( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) < 0;
}
bool operator<=( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) <= 0;
}
bool operator>( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) > 0;
}
bool operator>=( const String & lhs, const String & rhs )
{
return strcmp( lhs.c_str( ), rhs.c_str( ) ) >= 0;
}
///Main.cpp
#include "String.h"
using std::cin;
using std::cout;
using std::endl;
int main( )
{
String d;
cout<<"enter a string:\n";
cin>>d;
cout<<d<<endl;
String a = "hello";
String b = "world";
String c;
c = a + " "+b;
cout << "c is: " << c << endl;
cout << "c is: " << c.c_str( ) << endl;
cout << "c is: ";
for( int i = 0; i < c.length( ); i++ )
cout << c[ i ];
cout << endl;
return 0;
}