【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.判断输入的用户名和密 ...
随机推荐
- Delphi 过程与函数
注:该内容整理自以下链接. http://chanlei001.blog.163.com/blog/static/340306642011111615445266/ delphi 过程以保留字proc ...
- linux 试题
1. 在Linux系统中,以 文件 方式访问设备 . 2. 某文件的权限为:drw-r--r--,用数值形式表示该权限,则该八进制数为: 644 ,该文件属性是 目录 . 3. 前台起动的进程使用 C ...
- 20145218《Java程序设计》第一周学习总结
20145218 <Java程序设计>第一周学习总结 教材学习内容总结 今天下午看了Java学习的视频,感觉很是新奇,之前觉得Java学起来是艰难枯燥的,但通过第一章的学习觉得如果自己可以 ...
- 5.4.2 使用配置启动firefox
1.使用firefox的本地配置加载浏览器 使用本地配置加载浏览器,代码如下. 这样运行后,可以看到firebug等插件都已启动. 练习:假设做性能测试时,需要获取某个状况下的页面网络运行参数.要求完 ...
- HDUoj-------(1128)Self Numbers
Self Numbers Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 221. Maximal Square -- 矩阵中1组成的最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 51nod 1021 石子归并(dp)
51nod 1021 石子归并 题解:从i到j合并的最小值:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]); 最 ...
- MATLAB随机森林回归模型
MATLAB随机森林回归模型: 调用matlab自带的TreeBagger.m T=textread('E:\datasets-orreview\discretized-regression\10bi ...
- 设置浏览器使用的dom模式为IE7
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>设置浏览器使用的dom模式为 ...
- ES mlockall作用——preventing that memory from being paged to the swap area
elasticsearch还有一个重要的参数bootstrap.mlockall,这个参数的目的是当你无法关闭系统的swap的时候,建议把这个参数设为true.防止在内存不够用的时候,elastics ...