【C++】String类实现

//string.h

#include <iostream>

using namespace std;

class String;

istream& operator>>( istream&, String& );
ostream& operator<<( ostream&, String& );

class String {
    public:
        //String str1;
        //String str2( "literal" );
        //String str3( str2 );
        String();
        String( const char* );
        String( const String& );

        ~String();
        
        bool operator==( const String& ) const;
        bool operator==( const char* ) const;
        bool operator!=( const String& ) const;

        String& operator=( const String& );
        String& operator=( const char* );
        char& operator[]( const int );

        int size() { return _size; }
        char* c_str() { return _string; }

    private:
        char *_string;
        int _size;
};

  

//string.cpp

#include "string.h"

// to include strcmp
// in the C standard library
#include <cstring>
#include <iostream>
#include <cassert>
#include <iomanip>


using namespace std;

bool
String::operator==
( const String &rhs ) const {
    if ( _size != rhs._size ) 
        return false;
    else
        return strcmp( _string, rhs._string) ? false : true;
}

bool
String::operator==( const char *c ) const {
    return strcmp( _string, c ) ? false : true;
}


String::String() {
    _size = 0;
    _string = 0;
}


String::String( const String &rhs ) {
    _size = rhs._size;
    if ( !rhs._string ) {
        _string = 0; }
    else {
        _string = new char[_size + 1];
        strcpy( _string, rhs._string );
    }
}


String::String( const char *s ) {
    if ( !s ) {
        _size = 0; _string = 0; }
    else {
        _size = strlen( s );
        _string = new char[_size + 1];
        strcpy( _string, s );
    }
}


String::~String() {
    delete [] _string;
}

String&
String::operator=
( const String &rhs ) {
    if ( this != &rhs ) {
        _size = rhs._size;
        delete [] _string;
        if ( !rhs._string )
            _string = 0;
        else {
            _string = new char[_size + 1];
            strcpy( _string, rhs._string );
        }
    }
    else
        return *this;
}

String&
String::operator=
( const char *c ) {
    if ( !c ) {
        _size = 0; 
        delete [] _string;
        _string = 0;
    }
    else {
        //cout << "operator= invoked for char: " << *c << endl;
        _size = strlen( c );
        delete [] _string;
        _string = new char[_size + 1];
        strcpy( _string, c );
    }    
}

char&
String::operator[](const int index) {
    assert( index >= 0 && index < _size);
    return _string[ index ];
}

istream&
operator>>( istream& io, String &s) {
    const int limit_string_size =   4096;
    char inBuf[ limit_string_size ];

    io >> setw( limit_string_size ) >> inBuf; 
    s = inBuf; //String::operator=( const char* )

    return io;
}

ostream& 
operator<<( ostream& os, String &s) {
    os << s.c_str();
}

 

//stringtest.cpp
#include "string.h"
#include <iostream>

using namespace std;


int main() 
{
    int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
        theCnt = 0, itCnt =0, wdCnt = 0, notVowel = 0;
    String buf, the( "the" ), it( "it" );

    // invoke operator>> 
    while ( cin >> buf ) {
        ++wdCnt;

        //invoke operator<< 
        cout << buf << ' '; 
        if ( wdCnt%12 == 0 ) 
            cout << endl;

        if ( buf == the || buf == "The" ) 
            ++theCnt;
        else
            if ( buf == it || buf == "It" )
                ++itCnt;
        for ( int ix = 0; ix < buf.size(); ++ix )
        {
            switch ( buf[ ix ] )
            {
                case 'a': case 'A': ++aCnt; break;
                case 'e': case 'E': ++eCnt; break;
                case 'i': case 'I': ++iCnt; break;
                case 'o': case 'O': ++oCnt; break;
                case 'u': case 'U': ++uCnt; break;
                default: ++notVowel; break;
            }
        }
    }

    cout << "\n\n"
        << "Words read: " << wdCnt << "\n\n"
        << "the/The: "    << theCnt << '\n'
        << "it/It: "      << itCnt << "\n\n"
        << "non-vowel read: " << notVowel << "\n\n"
        << "a: " << aCnt << '\n'
        << "e: " << eCnt << '\n'
        << "i: " << iCnt << '\n'
        << "o: " << oCnt << '\n'
        << "u: " << uCnt << endl;

}

  

原文链接: https://www.cnblogs.com/dracohan/p/3145519.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    【C++】String类实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/92758

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午1:50
下一篇 2023年2月10日 上午1:50

相关推荐