【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; }
【C++】String类实现的更多相关文章
- 标准库String类
下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++ string类的实现
c++中string类的实现 今天面试被考到了, 全给忘记了!!! //string类的实现 #include <iostream> #include <string.h> ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- java基础复习:final,static,以及String类
2.final 1)为啥String是final修饰的呢? 自己答: 答案: 主要是为了“效率” 和 “安全性” 的缘故.若 String允许被继承, 由于它的高度被使用率, 可能会降低程序的性能,所 ...
- String类和StringBuffer类的区别
首先,String和StringBuffer主要有2个区别: (1)String类对象为不可变对象,一旦你修改了String对象的值,隐性重新创建了一个新的对象,释放原String对象,StringB ...
- 05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明
Question: 整理String类的Length().charAt(). getChars().replace(). toUpperCase(). toLowerCase().trim().toC ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- String类常用方法
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
随机推荐
- Broadcast Receviewer
使用Braodcast Receiver 一.新建一个空的工程,命名为LearnBroadcastReceiver 二.为主界面添加一个按钮 三.新建一个broadcast receiver,命名为M ...
- 出现 could not open jvm.cfg 的解决办法
出现该的原因很可能是由于卸载 jdk 不干净而引起的(就算是使用工具也一样),在oracle的官方上也是让我们卸载了重装一次.可是重装真的能解决问题吗? 以下方法经过本人的验证,绝对可行! (一定要 ...
- 2010 word 如何新建目录
首先插入一个bullet 填充内容,编好编号,选择文字,右键,然后选择相应的level,然后点击一级菜单reference, 然后点击table of contents, 选择某一个样式,然后插入成功 ...
- listview去掉底部多出的边框黑色
listview去掉底部多出的边框黑色 android:fadingEdge="none" //去掉listview黑色底边 listview.setDivider(null);
- 日期操作类--GregorianCalendar类
GregorianCalendar--API JavaTM Platform Standard Ed. 6 GregorianCalendar类 Calendar类实现了公历日历,GregorianC ...
- 【Problem solved】 error C2665: “loadimage”: 2 个重载中没有一个可以转换所有参数类型
选择“项目”菜单->项目属性->配置属性->常规->字符集,改为“未设置”即可.
- 在Access中批量调用SQL语句
access的SQL视图一次只能执行一条SQL语句,那么在VBA中调用或许可以简便一些 例如: Public Sub 批量()Dim strsql As String strsql = " ...
- 《Java程序设计》实验四 实验报告
实验四 Android开发基础 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 ...
- ubuntu安装SCrapy
依次安装 sudo apt-get install build-essential; sudo apt-get install python-dev; sudo apt-get install lib ...
- GIT命令(急速学习)
用过sourceTree,egit(eclipse中的git插件),最后还是感觉git bash顺手:svn早已经不用: 先上几个原来看过的git 教程--书读百遍,其义自见.多看几篇文章才能总结 ...