【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.判断输入的用户名和密 ...
随机推荐
- html5 和css3的小知识!
阿里web字体的使用 (1)进入官网 点击webfont (2)输入对应的文字 然后选择添加字体 (3)可以直接引用线上地址或者本地下载,引用线上地址需要添加http,(在服务器环境下可以不用),如 ...
- [css] vertical-align和line-height
原文链接:http://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/ ...
- Hbase之尝试使用错误列族获取数据
import com.google.common.base.Strings; import org.apache.hadoop.conf.Configuration; import org.apach ...
- this和call
function foo(x){ console.log(x);} foo.call(this,'abc');console.log(this); ---- abc
- VC++全局变量初始化
目录 第1章说明 2 1.1 程序启动 2 1.2 强符号.弱符号 2 1.3 动态初始化顺序 3 1.4 exe调用dll 4 1.5 禁用动态初始化 4 1.6 ...
- 让页面滑动流畅得飞起的新特性:Passive Event Listeners
版权声明:本文由陈志兴原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/153 来源:腾云阁 https://www.qclo ...
- table合并单元格colspan和rowspan .
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...
- SAP MM事务代码清单
- js文字上下滚动代码
<div id="dome"> <div id="dome1"> <ul class="express"> ...
- jmeter 建立一个JMS主题测试计划
创建两个线程组和组 每一个到10迭代. 总消息线程(6)x(1消息) (重复10次)= 60消息. 建立测试计划,您将使用 以下元素: 线程组 , JMS的出版商 , JMS用户 , 图结果 . 一般 ...