【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.判断输入的用户名和密 ...
随机推荐
- datatables使用
//4.多列排序 //示例:http://www.guoxk.com/html/DataTables/Multi-column-sorting.html //5.隐藏某些列 $(document).r ...
- 使用Python获取Linux系统的各种信息
哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...
- CSS三种定位机制
标准文档流 块级元素撑满整个页面div,ul,li,dl,dt,p 行级元素可以一行显示多个span,strong,img,input大部分 一般不设置盒子的高度,另其自动调整 margin属性的au ...
- j2ee四大作用域pagecontext,request,session,ServletContext(转)
转自:(http://www.5ycode.com/63) 在JSP页面中的对象,包括用户创建的对象(例如,JavaBean对象)和JSP的隐含对象,都有一个范围属性.范围定义了在什么时间内,在哪一个 ...
- Android FloatingActionButton(FAB) 悬浮按钮
FloatingActionButton 悬浮按钮 ...
- node下新建工程
打开控制台 输入express --view ejs 成功后有 输入cnpm install 成功后有 查看下现在的json文件,如图 用数据库的话可以键入 cnpm install --save m ...
- 任性,新建对象不用new
先看最简单的一个例子: window.meng = window.meng || {}; (function () { /** * * @param {Number}width * @param {N ...
- angular-ui-router状态不变刷新页面
需求: 当前在A页面状态,要求在点击A状态时,可以刷新A状态. 解决方法:在ui-sref状态切换的标签中添加属性 ui-sref-opts="{reload: true}" ...
- Java基础:多线程
基本概念 程序&线程&进程 程序是一个段可以提供业务功能的代码,它可以包含1个或多个进程.程序在OS上运行时表现为进程对各种资源(CPU,内存,Disk..)的消耗和处理. 进程是OS ...
- (22)odoo 安装旧模块报错处理
一些老版本的模块没有得到升级,所以经常碰到模块无法安装的问题. No module name osv 将模块的 from osv import osv,fields 改为 from openerp.o ...