迭代器:
miterator.h
#ifndef MITERATOR_h
#define MITERATOR_h
//#include"mstring.h"
class Mstring;
class Miterator
{
private:
Mstring* _p_mstr;
int _sit;
public:
Miterator(Mstring* p_mstr,const int sit)
{
_p_mstr = p_mstr;
_sit = sit;
}
Miterator(const Miterator& src)
{
_p_mstr = src._p_mstr;
_sit = src._sit;
}
Miterator& operator++()
{
++_sit;
return *this;
}
Miterator operator++(int)
{
return Miterator(_p_mstr, _sit++);
}
Miterator& operator--()
{
--_sit;
return *this;
}
Miterator operator--(int)
{
return Miterator(_p_mstr, _sit--);
}
bool operator==(const Miterator& src)
{
if (_p_mstr == src._p_mstr && _sit == src._sit)
{
return true;
}
return false;
}
bool operator!=(const Miterator& src)
{
if (_p_mstr == src._p_mstr && _sit == src._sit)
{
return false;
}
return true;
}
char& operator*();
/*
{
return (*_p_mstr)[_sit];
}
*/
};
#endif
main.cpp
#include<iostream>
#include<string>
//#include"miterator.h"
#include"mstring.h"
using namespace std;
/*
namespace gg
{
typedef int Int;
}
*/
int main()
{
#if 0
/*
++ -- != == *
end();
begin();
*/
string s1 = "abcdefg";
string::iterator it1 = s1.begin();
for (; it1 != s1.end(); it1++)
{
cout << *it1 << endl;
}
it1--;
string::iterator it3 = it1;
it1--;
if (it1 == it3)
{
cout << "it1 == it3" << endl;
}
else
{
cout << "it1 != it3" << endl;
}
string s2 = "poiutyrr";
string::iterator it2 = s2.begin();
cout << s2 << endl;
cout << *it2 << endl;
*it2 = 'm';
cout << *it2 << endl;
/*
if (it1 == it2)
{
cout << "it1 = it 2" << endl;;
}
else
{
cout << "it1 != it2" << endl;
}*/
#endif
Mstring str1 = "1234567";
Mstring::miterator its1 = str1.begin();
for (; its1 != str1.end(); its1++)
{
(*its1)++;
cout << *its1 << endl;
}
return 0;
}
mstring.cpp
//miterator.h展开
//mstring.h展开
#include"mstring.h"
Mstring::Mstring(int len)
{
_len = len;
_str = new char[_len];
memset(_str, 0, _len);
init();
}
Mstring::Mstring(const Mstring& src)
{
_len = src._len;
_str = src._str;
up_count();
}
Mstring::Mstring(const char* str)
{
//开辟空间
_len = strlen(str)+1+ get_front_len();
_str = new char[_len];
memset(_str, 0, _len);
//拷贝数据
for (int i = 0; i < strlen(str)+1; i++)
{
get_str_begin()[i] = str[i];
}
//初始化引用技术
init();
}
Mstring::~Mstring()
{
if(0 == down_count())
{
delete get_lock();
delete []_str;
}
_len = 0;
_str = NULL;
}
Mstring& Mstring::operator=(const Mstring& src)
{
if (&src == this)
{
return *this;
}
if (0 == down_count())
{
delete _str;
}
_len = src._len;
_str = src._str;
up_count();
return *this;
}
Mstring& Mstring::operator=(const char* str)
{
if (0 == down_count())
{
delete get_lock();
delete[]_str;
}
if (NULL == str)
{
_len = 14;
_str = new char[_len];
memset(_str, 0, _len);
init_count();
}
_len = strlen(str) + 1 + get_front_len();
_str = new char[_len];
memset(_str, 0, _len);
for (int i = 0; i < strlen(str) + 1; i++)
{
get_str_begin()[i] = str[i];
}
init();
return *this;
}
Mstring Mstring::operator+(const Mstring& src)const
{
int len = src.size() + size()+1;
char* tmp = new char[len];
memset(tmp, 0, len);
int i = 0;
for (; i < size(); i++)
{
tmp[i] = get_str_begin()[i];
}
for (int j = 0; j < src.size()+1; i++,j++)
{
tmp[i] = src.get_str_begin()[j];
}
return tmp;
}
Mstring Mstring::operator+(const char* str)const
{
if (NULL == str)
{
return *this;
}
int len = size() + strlen(str) + 1;
char* tmp = new char[len];
memset(tmp, 0, len);
int i = 0;
for (; i < size(); i++)
{
tmp[i] = get_str_begin()[i];
}
for (int j = 0; j < strlen(str) + 1; i++, j++)
{
tmp[i] = str[j];
}
return tmp;
}
bool Mstring::operator==(const Mstring& src)const
{
return strcmp(get_str_begin(), src.get_str_begin()) == 0;
}
bool Mstring::operator==(const char* str)const
{
return strcmp(get_str_begin(), str) == 0;
}
bool Mstring::operator>(const Mstring& src)const
{
return strcmp(get_str_begin(), src.get_str_begin()) > 0;
}
bool Mstring::operator>(const char* str)const
{
return strcmp(get_str_begin(), str) > 0;
}
ostream& Mstring::operator<<(ostream& out)const
{
out << get_str_begin() << endl;
return out;
}
istream& Mstring::operator>>(istream& in)
{
char str[1024] = {0};
in >> str;
if(0 == down_count())
{
delete _str;
}
_len = strlen(str)+1+sizeof(int);
_str = new char[_len];
memset(_str, 0, _len);
for (int i = 0; i < strlen(str) + 1; i++)
{
get_str_begin()[i] = str[i];
}
init_count();
return in;
}
Mstring operator+(const char* str, const Mstring& src)
{
return src + str;
}
bool operator==(const char* str, const Mstring& src)
{
return strcmp(str, src.get_str_begin()) == 0;
}
bool operator>(const char* str, const Mstring& src)
{
return strcmp(str, src.get_str_begin()) > 0;
}
ostream& operator<<(ostream& out, const Mstring& src)
{
out << "count=" << src.get_count() << endl;
out << src.get_str_begin() << endl;
return out;
}
istream& operator>>(istream& in, Mstring& src)
{
return src.operator>>(in);
}
Mstring::miterator Mstring::begin()
{
return Miterator(this, 0);
}
Mstring::miterator Mstring::end()
{
return Miterator(this, size());
}
char& Miterator::operator*()
{
return (*_p_mstr)[_sit];
}
mstring.h
#ifndef MSTRING_H
#define MSTRING_H
#include<iostream>
#include"miterator.h"
using namespace std;
#include<mutex>
class Mstring
{
private:
int _len;
char* _str;
void init_count()
{
*((int*)_str) = 1;
}
int& get_count()
{
return *((int*)_str);
}
const int& get_count()const
{
return *((int*)_str);
}
void up_count()
{
get_lock()->lock();
get_count()++;
get_lock()->unlock();
}
int down_count()
{
get_lock()->lock();
--get_count();
get_lock()->unlock();
return get_count();
}
char* get_str_begin()const
{
return _str + get_front_len();
}
mutex*& get_lock()
{
return *((mutex**)(_str + 4));
}
void init()
{
init_count();
get_lock() = new mutex();
}
int get_front_len()const
{
return sizeof(int) + sizeof(mutex*);
}
public:
typedef Miterator miterator;
Mstring(int len = 18);
Mstring(const Mstring& src);
Mstring(const char* str);
~Mstring();
Mstring& operator=(const Mstring& src);
Mstring& operator=(const char* str);
Mstring operator+(const Mstring& src)const;
Mstring operator+(const char* str)const;
bool operator==(const Mstring& src)const;
bool operator==(const char* str)const;
bool operator>(const Mstring& src)const;
bool operator>(const char* str)const;
ostream& operator<<(ostream& out)const;
istream& operator>>(istream& in);
char& operator*()
{
if(1 != get_count())
{
char* tmp = new char[_len];
memset(tmp, 0, _len);
for (int i = 0; i < size() + 1; i++)
{
tmp[sizeof(int) + i] = get_str_begin()[i];
}
down_count();
_str = tmp;
init_count();
}
return *_str;
}
const char& operator*()const
{
return *_str;
}
char& operator[](int sit)
{
if (1 != get_count())
{
char* tmp = new char[_len];
memset(tmp, 0, _len);
for (int i = 0; i < size() + 1; i++)
{
tmp[sizeof(int) + i] = get_str_begin()[i];
}
down_count();
_str = tmp;
init_count();
}
return get_str_begin()[sit];
}
const char& operator[](int sit)const
{
return get_str_begin()[sit];
}
int size()const
{
return strlen(get_str_begin());
}
miterator begin();
miterator end();
friend Mstring operator+(const char* str, const Mstring& src);
friend bool operator==(const char* str, const Mstring& src);
friend bool operator>(const char* str, const Mstring& src);
friend ostream& operator<<(ostream& out, const Mstring& src);
friend istream& operator>>(istream& in, Mstring& src);
};
Mstring operator+(const char* str, const Mstring& src);
bool operator==(const char* str, const Mstring& src);
bool operator>(const char* str, const Mstring& src);
ostream& operator<<(ostream& out,const Mstring& src);
istream& operator>>(istream& in, Mstring& src);
#endif