//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; }

  

【C++】String类实现的更多相关文章

  1. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  2. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  3. C++ string类的实现

    c++中string类的实现 今天面试被考到了, 全给忘记了!!!   //string类的实现 #include <iostream> #include <string.h> ...

  4. String类的功能

    String类              标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...

  5. java基础复习:final,static,以及String类

    2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...

  6. String类和StringBuffer类的区别

    首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...

  7. 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

    Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...

  8. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  9. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  10. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

随机推荐

  1. hdu 4414 Finding crosses

    题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...

  2. HTML介绍、文档基本结构、meta标签、HTML标记的语法

    一.HTML的介绍 Hyper Text Mark-up Language 超文本标记语言,是一种描述性标记语言(不是编程语言),主要用于描述网页(可以有图像,文字,声音,等..)但没有交互性 HTM ...

  3. HBase启动和停止命令

    启动HBase集群: bin/start-hbase.sh 单独启动一个HMaster进程: bin/hbase-daemon.sh start master 单独停止一个HMaster进程: bin ...

  4. Python核心编程-基础2

    open() 和 file() 函数会同时存在, 完成相同的功能.一般说来, 我们建议使用 open() 来读写文件, 在您想说明您在处理文件对象时使用 file() , 例如 if instance ...

  5. 推荐两篇Unity与Android交互的文章

    http://www.xuanyusong.com/archives/676 里面18,19介绍

  6. 2016-6-15-de novo文献阅读

    准备读四篇denovo的文献: Nature Biotechnology(2015) - Sequencing of allotetraploid cotton (Gossypium hirsutum ...

  7. 腾讯云从零部署nodejs站点

    版权声明:本文由袁飞翔原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/176 来源:腾云阁 https://www.qclo ...

  8. SQL Server 2012清除连接过的服务器名称历史

    退出客户端后 SQL Server 2012: 删除这两个地方!请提前备份! X:\Users\XXX\AppData\Roaming\Microsoft\SQL Server Management ...

  9. rsa加密--选择padding模式需要注意的问题。。。

    最近在做一个项目中需要,在android对一个密码字段首先进行 一次md5加密后再进行一次rsa加密,然后把加密的结果通过 json协议传输给nginx服务器进行解密.在android中,可以直接 使 ...

  10. 使用XIB实现一个简单view

    技术处女贴 欢迎来探讨 转帖请注明出处 http://www.cnblogs.com/andy-zhou/p/4962135.html 微信: @Andy 1. AppDelegate AppDele ...