马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 andalousie 于 2014-2-23 09:45 编辑
我的iostream(ios.h)下面是我的string(str.h)#ifndef STD_STRING
#define STD_STRING
#include "ios.h"
#include <string.h>
void memmove(char *dest, char *src, size_t n)
{
/* No need to do that thing. */
if (dest == src)
return;
/* Check for destructive overlap. */
if (src < dest && dest < src + n) {
/* Destructive overlap ... have to copy backwards. */
src += n;
dest += n;
while (n-- > 0)
*--dest = *--src;
} else {
/* Do an ascending copy. */
while (n-- > 0)
*dest++ = *src++;
}
}
class out_of_range{
public:
out_of_range()
{
std::cerr << "Error: Invalid access of element. " << std::endl;
}
};
namespace std{
class string{
public:
typedef size_t size_type;
typedef char value_type;
typedef const string& const_reference;
typedef const char * const_pointer;
string(const_pointer str = ""); //default constructor(value_type)
string(value_type);
string(const_reference); //assign constructor(string)
string& operator= (const_reference); //operator=
string operator+ (const_reference) const; //operator+
string& operator+=(const_reference); //operator+=
//bool operators
bool operator==(const_reference) const; //operator==
bool operator==(const_pointer) const;
bool operator!=(const_reference) const; //operator!=
bool operator!=(const_pointer) const;
bool operator< (const_reference) const; //operator<
bool operator< (const_pointer) const;
bool operator> (const_reference) const; //operator>
bool operator> (const_pointer) const;
bool operator<=(const_reference) const; //operator<=
bool operator<=(const_pointer) const;
bool operator>=(const_reference) const; //operator>=
bool operator>=(const_pointer) const;
value_type& operator[](const size_type) const; //operator[]
value_type& at(const size_type n) const;
size_type size() const { return _len; }
size_type length() const { return _len; }
string& erase(size_type pos, size_type len);
void swap(string& str);
void clear();
bool empty() { return !_len; }
const_pointer c_str() const { return const_cast<char*>(m_data); }
~string(void);
private:
value_type *m_data;
size_type _len;
mutable bool local;
};
inline string::string(const_pointer str)
{
_len = strlen(str);
m_data = new value_type[_len+1];
strcpy(m_data, str);
local = true;
}
inline string::string(value_type ch)
{
m_data = new value_type[1];
m_data[0] = ch;
_len = 1;
local = true;
}
inline string::string(const_reference other)
{
local = false;
m_data = other.m_data;
_len = other._len;
}
inline string::~string(void)
{
if(local) delete [] m_data;
}
inline string& string::operator=(const_reference other)
{
if (this!=&other)
{
string tmp(other);
swap(tmp);
}
return *this;
}
inline string string::operator+(const_reference other)const
{
string newstring;
newstring.local = false;
newstring.m_data = new value_type[strlen(m_data)+strlen(other.m_data)+1];
strcpy(newstring.m_data,m_data);
strcat(newstring.m_data,other.m_data);
newstring._len = _len + other._len;
return newstring;
}
inline string& string::operator+=(const_reference other)
{
*this = *this + other;
return *this;
}
inline bool string::operator==(const_reference s) const
{
return s._len == _len
&& !strcmp(m_data,s.m_data);
}
inline bool string::operator==(const_pointer s) const
{
return strlen(s) == _len
&& !strcmp(m_data,s);
}
inline bool string::operator!=(const_reference s) const
{
return !(*this == s);
}
inline bool string::operator!=(const_pointer s) const
{
return !(*this == s);
}
inline bool string::operator< (const_reference s) const
{
return strcmp(m_data,s.m_data) < 0;
}
inline bool string::operator< (const_pointer s) const
{
return strcmp(m_data,s) < 0;
}
inline bool string::operator> (const_reference s) const
{
return strcmp(m_data,s.m_data) > 0;
}
inline bool string::operator> (const_pointer s) const
{
return strcmp(m_data,s) > 0;
}
inline bool string::operator<=(const_reference s) const
{
return !(*this > s);
}
inline bool string::operator<=(const_pointer s) const
{
return !(*this > s);
}
inline bool string::operator>=(const_reference s) const
{
return !(*this < s);
}
inline bool string::operator>=(const_pointer s) const
{
return !(*this < s);
}
inline char& string::operator[](const size_type n) const
{
if (n<0 || n>=_len) throw out_of_range();
return m_data[n];
}
inline char& string::at(const size_type n) const
{
if (n<0 || n>=_len) throw out_of_range();
return m_data[n];
}
inline string& string::erase(size_type pos, size_type len)
{
if (pos >= _len || 0 == len) return *this;
const size_type remainder = pos + len;
if (remainder >= _len || remainder < pos)
{
*(m_data + pos) = '\0';
_len = pos;
return *this;
}
size_type left = _len - remainder + 1;
value_type *d = m_data + pos;
value_type *s = m_data + remainder;
memmove(d, s, left);
_len -= len;
return *this;
}
inline void string::swap(string& str)
{
string tmp(str);
str.local = local;
str.m_data = m_data;
str._len = _len;
local = tmp.local;
m_data = tmp.m_data;
_len = tmp._len;
}
inline void string::clear()
{
m_data = new value_type[1];
strcpy(m_data, "");
_len = 0;
}
istream& operator>>(istream& is, string& str)
{
char buffer[1000];
is >> buffer;
str = buffer;
return is;
}
istream & getline(istream & is, string & str)
{
char buffer[1000];
is.getline(buffer, 1000);
str = buffer;
return is;
}
ostream& operator<<(ostream& os, string& str)
{
os << str.c_str();
return os;
}
} //namespace std
#endif
测试文件test_string.cpp#include "str.h"
using namespace std;
int main()
{
string s1;
string s2("23345sghehst");
cout << s1.size();
cout << s2.size() << endl;
s2.erase(4, 3);
string s3 = s1 + s2;
cout << s3 << endl;
if(s1 >= s2) cout << 1;
}
|