C++ String类的实现
2016-07-20 12:15
423 查看
string头文件:
#ifndef MAIN_WTF_STRING_H
#define MAIN_WTF_STRING_H
#include<iostream>
using namespace std;
namespace main_wtf_5
{
class string
{
private:
char* characters; //存储字符串数据
size_t alloacted; //字符串容量
size_t current_length; //字符串长度
public:
string(const char str[] = "");
string(const string& source);
void operator = (const string& source);
size_t length()const { return current_length; } ;
char* getChar() const { return characters; };
char operator[](size_t position) const;
void operator += (const string& addend);
void operator += (const char addend);
void operator += (const char addend[]);
void reserve(size_t n);
~string();
friend istream& operator >>(istream& ins,string& target);
friend ostream& operator <<(ostream& outs,const string& source);
};
string operator +(const string& s1,const string& s2);
istream& operator >>(istream& ins,string& target);
ostream& operator <<(ostream& outs,const string& source);
bool operator == (const string& s1,const string& s2);
bool operator > (const string& s1,const string& s2);
bool operator < (const string& s1,const string& s2);
bool operator >= (const string& s1,const string& s2);
bool operator <= (const string& s1,const string& s2);
bool operator != (const string& s1,const string& s2);
}
#endif
实现文件:
#include "mystring.h"
#include <cassert>
namespace main_wtf_5
{
string::string(const char str[])
{
current_length = strlen(str);
alloacted = current_length+1;
characters = new char[alloacted];
strcpy(characters,str);
}
string::string(const string& source)
{
current_length = source.current_length;
alloacted = source.alloacted;
characters = new char[alloacted];
strcpy(characters,source.characters);
}
void string::operator= (const string& source)
{
char* new_str;
if(this == &source)
return;
if(alloacted != source.alloacted)
{
new_str = new char[alloacted];
delete characters;
characters = new_str;
alloacted = source.alloacted;
}
current_length = source.current_length;
strcpy(characters,source.characters);
}
char string::operator[](size_t position) const
{
assert(position <= current_length);
return characters[position];
}
void string::operator+=(const string& addend)
{
// current_length += addend.current_length;
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted = current_length + addend.current_length + 1;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend.characters,addend.characters+addend.current_length,characters+current_length);
current_length += addend.current_length;
characters[current_length] = '\0';
}
void string::operator+=(const char addend)
{
// reserve(alloacted+1);
char* new_str = new char[alloacted+1];
strcpy(new_str,characters);
new_str[current_length] = addend;
characters = new_str;
alloacted += 1;
current_length += 1;
characters[current_length] = '\0';
}
void string::operator+=(const char addend[])
{
int length = strlen(addend);
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted += length;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend,addend+length,characters+current_length);
current_length += length;
characters[alloacted] = '\0';
}
void string::reserve(size_t n)
{
char* new_str;
if(alloacted == n)
return;
if(n < current_length)
alloacted = current_length+1;
new_str = new char
;
strcpy(new_str,characters);
delete[] characters;
characters = new_str;
alloacted = n;
}
string::~string()
{
delete[] characters;
}
string operator +(const string& s1,const string& s2)
{
string str;
str += s1;
str += s2;
return str;
}
istream& operator >>(istream& ins,string& target)
{
while(ins && isspace(ins.peek()))
ins.ignore();
char ch;
target.characters = "";
while(ins>>ch)
{
target += ch;
if(isspace(ins.peek()))
break;
}
target.current_length = strlen(target.characters);
target.alloacted = target.current_length + 1;
return ins;
}
ostream& operator <<(ostream& outs,const string& source)
{
outs<<source.characters;
return outs;
}
bool operator == (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) == 0);
}
bool operator > (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) > 0);
}
bool operator < (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) < 0);
}
bool operator >= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) >= 0);
}
bool operator <= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) <= 0);
}
bool operator != (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) != 0);
}
}
#ifndef MAIN_WTF_STRING_H
#define MAIN_WTF_STRING_H
#include<iostream>
using namespace std;
namespace main_wtf_5
{
class string
{
private:
char* characters; //存储字符串数据
size_t alloacted; //字符串容量
size_t current_length; //字符串长度
public:
string(const char str[] = "");
string(const string& source);
void operator = (const string& source);
size_t length()const { return current_length; } ;
char* getChar() const { return characters; };
char operator[](size_t position) const;
void operator += (const string& addend);
void operator += (const char addend);
void operator += (const char addend[]);
void reserve(size_t n);
~string();
friend istream& operator >>(istream& ins,string& target);
friend ostream& operator <<(ostream& outs,const string& source);
};
string operator +(const string& s1,const string& s2);
istream& operator >>(istream& ins,string& target);
ostream& operator <<(ostream& outs,const string& source);
bool operator == (const string& s1,const string& s2);
bool operator > (const string& s1,const string& s2);
bool operator < (const string& s1,const string& s2);
bool operator >= (const string& s1,const string& s2);
bool operator <= (const string& s1,const string& s2);
bool operator != (const string& s1,const string& s2);
}
#endif
实现文件:
#include "mystring.h"
#include <cassert>
namespace main_wtf_5
{
string::string(const char str[])
{
current_length = strlen(str);
alloacted = current_length+1;
characters = new char[alloacted];
strcpy(characters,str);
}
string::string(const string& source)
{
current_length = source.current_length;
alloacted = source.alloacted;
characters = new char[alloacted];
strcpy(characters,source.characters);
}
void string::operator= (const string& source)
{
char* new_str;
if(this == &source)
return;
if(alloacted != source.alloacted)
{
new_str = new char[alloacted];
delete characters;
characters = new_str;
alloacted = source.alloacted;
}
current_length = source.current_length;
strcpy(characters,source.characters);
}
char string::operator[](size_t position) const
{
assert(position <= current_length);
return characters[position];
}
void string::operator+=(const string& addend)
{
// current_length += addend.current_length;
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted = current_length + addend.current_length + 1;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend.characters,addend.characters+addend.current_length,characters+current_length);
current_length += addend.current_length;
characters[current_length] = '\0';
}
void string::operator+=(const char addend)
{
// reserve(alloacted+1);
char* new_str = new char[alloacted+1];
strcpy(new_str,characters);
new_str[current_length] = addend;
characters = new_str;
alloacted += 1;
current_length += 1;
characters[current_length] = '\0';
}
void string::operator+=(const char addend[])
{
int length = strlen(addend);
char* new_str = new char[alloacted];
strcpy(new_str,characters);
delete[] characters;
alloacted += length;
characters = new char[alloacted];
copy(new_str,new_str+current_length,characters);
copy(addend,addend+length,characters+current_length);
current_length += length;
characters[alloacted] = '\0';
}
void string::reserve(size_t n)
{
char* new_str;
if(alloacted == n)
return;
if(n < current_length)
alloacted = current_length+1;
new_str = new char
;
strcpy(new_str,characters);
delete[] characters;
characters = new_str;
alloacted = n;
}
string::~string()
{
delete[] characters;
}
string operator +(const string& s1,const string& s2)
{
string str;
str += s1;
str += s2;
return str;
}
istream& operator >>(istream& ins,string& target)
{
while(ins && isspace(ins.peek()))
ins.ignore();
char ch;
target.characters = "";
while(ins>>ch)
{
target += ch;
if(isspace(ins.peek()))
break;
}
target.current_length = strlen(target.characters);
target.alloacted = target.current_length + 1;
return ins;
}
ostream& operator <<(ostream& outs,const string& source)
{
outs<<source.characters;
return outs;
}
bool operator == (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) == 0);
}
bool operator > (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) > 0);
}
bool operator < (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) < 0);
}
bool operator >= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) >= 0);
}
bool operator <= (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) <= 0);
}
bool operator != (const string& s1,const string& s2)
{
return (strcmp(s1.getChar(),s2.getChar()) != 0);
}
}
相关文章推荐
- TG(提高篇)第15讲 实践项目 简单银行系统(函数篇)
- vector中函数的使用
- 121. Best Time to Buy and Sell Stock
- 函数atof,atoi,atol,strtod,strtol,strtoul
- TinyXML经典入门
- C/C++中无条件花括号的妙用
- c语言格式化输出控制长度
- C++中的string类的用法
- C语言中do...while(0)的妙用(转载)
- C++中的static关键字的总结
- UVA 579-Clock Hands
- UVA 10879-Code Refactoring
- UVA 10392-Factoring Large Numbers
- UVA 10250-The Other Two Trees
- UVA 350-Pseudo-Random Numbers
- UVA 10061-How many zero's and how many digits
- 浅谈C/C++ 语言中的表达式求值
- delete和delete[]的区别
- UVA 10161 Ant on a Chessboard
- UVA 621-Secret Research