#include
#include
using namespace std;
namespace jxh {
class string {
public:
/*string(const char* str="")
:_str(new char[strlen(str)+1])
,_size(strlen(str))
,_capacity(strlen(str))
{
strcpy(_str, str);
}*/
string(const char* str = ""){
_size = strlen(str);
_capacity = _size;
_str = new char[_size + 1];
strcpy(_str, str);
}
string(const string& s) {
string tmp(s._str);
swap(tmp);
}
string& operator=(string s) {
swap(s);
return *this;
}
typedef char* iterator;
typedef const char* const_iterator;
iterator begin() {
return _str;
}
iterator end() {
return _str + _size;
}
const_iterator begin()const {
return _str;
}
const_iterator end()const {
return _str + _size;
}
const char* c_str()const{
return _str;
}
size_t size()const {
return _size;
}
char& operator[](size_t pos) {
assert(pos <= _size);
return _str[pos];
}
const char& operator[](size_t pos) const {
assert(pos <= _size);
return _str[pos];
}
void reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch) {
if (_size == _capacity) {
int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
void append(const char* str) {
size_t len = strlen(str);
if (len + _size > _capacity) {
reserve(len + _size);
}
strcpy(_str + _size, str);
_size = len + _size;
}
string& operator+=(char ch) {
push_back(ch);
return *this;
}
string& operator+=(const char* str) {
append(str);
return *this;
}
void insert(size_t pos, char ch) {
assert(pos <= _size);
if (_size == _capacity) {
int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
for (size_t i = _size+1; i > pos; i--) {
_str[i] = _str[i-1];
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* str) {
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity) {
reserve(_size + len);
}
for (size_t i = _size + len; i >= pos + len; i--) {
_str[i] = _str[i - len];
}
strncpy(_str + pos, str, len);
_size += len;
}
void erase(size_t pos, size_t len=npos) {
assert(pos < _size);
if (len == npos || pos + len - 1 >= _size - 1) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void swap(string& s) {
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
size_t find(char ch, size_t pos = 0) {
for (size_t i = 0; i < _size; i++) {
if (ch == _str[i])
return i;
}
return npos;
}
size_t find(const char* str, size_t pos = 0) {
const char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)return npos;
else return ptr - _str;
}
string substr(size_t pos, size_t len = npos) {
assert(pos < _size);
size_t end = len + pos;
if (len == npos || len + pos >= _size) {
end = _size;
}
string str;
str.reserve(end - pos);
for (size_t i = pos; i < end; i++) {
str += _str[i];
}
return str;
}
void clear() {
_size = 0;
_str[0] = '\0';
}
void print_str() {
for (size_t i = 0; i < _size; i++) {
cout << _str[i] << ' ';
}
cout << endl;
}
~string() {
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
size_t string::npos = -1;
ostream& operator<<(ostream& out, const string& s) {
for (auto ch : s) {
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s) {
s.clear();
char buff[128];
char ch = in.get();
int i = 0;
while (ch != ' ' && ch != '\n') {
buff[i++] = ch;
if (i == 127) {
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0) {
buff[i] = '\0';
s += buff;
}
return in;
}
}
这些都放在了头文件中,用的时候只需要包含头文件,使用上命名空间就行
如果想把这些代码的声明和定义分离,可以:
在string.h中放
#include
#include
using namespace std;
namespace jxh {
class string {
public:
string(const char* str = "");
string(const string& s);
string& operator=(string s);
typedef char* iterator;
typedef const char* const_iterator;
iterator begin() {
return _str;
}
iterator end() {
return _str + _size;
}
const_iterator begin()const {
return _str;
}
const_iterator end()const {
return _str + _size;
}
const char* c_str()const{
return _str;
}
size_t size()const {
return _size;
}
char& operator[](size_t pos);
const char& operator[](size_t pos)const;
void reserve(size_t n);
void push_back(char ch);
void append(const char* str);
string& operator+=(char ch);
string& operator+=(const char* str);
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
void erase(size_t pos, size_t len = npos);
void swap(string& s);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
string substr(size_t pos, size_t len = npos);
void clear();
void print_str();
~string();
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos=-1;
};
ostream& operator<<(ostream& out, const string& s);
istream& operator>>(istream& in, string& s);
}
在string.cpp中放
#include"string.h"
namespace jxh {
string::string(const char* str) {
_size = strlen(str);
_capacity = _size;
_str = new char[_size + 1];
strcpy(_str, str);
}
string::string(const string& s) {
string tmp(s._str);
swap(tmp);
}
string& string::operator=(string s) {
swap(s);
return *this;
}
char& string::operator[](size_t pos) {
assert(pos <= _size);
return _str[pos];
}
const char& string::operator[](size_t pos) const {
assert(pos <= _size);
return _str[pos];
}
void string::reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void string::push_back(char ch) {
if (_size == _capacity) {
int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
void string::append(const char* str) {
size_t len = strlen(str);
if (len + _size > _capacity) {
reserve(len + _size);
}
strcpy(_str + _size, str);
_size = len + _size;
}
string& string::operator+=(char ch) {
push_back(ch);
return *this;
}
string& string::operator+=(const char* str) {
append(str);
return *this;
}
void string::insert(size_t pos, char ch) {
assert(pos <= _size);
if (_size == _capacity) {
int newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
for (size_t i = _size + 1; i > pos; i--) {
_str[i] = _str[i - 1];
}
_str[pos] = ch;
_size++;
}
void string::insert(size_t pos, const char* str) {
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity) {
reserve(_size + len);
}
for (size_t i = _size + len; i >= pos + len; i--) {
_str[i] = _str[i - len];
}
strncpy(_str + pos, str, len);
_size += len;
}
void string::erase(size_t pos, size_t len) {
assert(pos < _size);
if (len == npos || pos + len - 1 >= _size - 1) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
void string::swap(string& s) {
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
size_t string::find(char ch, size_t pos) {
for (size_t i = 0; i < _size; i++) {
if (ch == _str[i])
return i;
}
return npos;
}
size_t string::find(const char* str, size_t pos) {
const char* ptr = strstr(_str + pos, str);
if (ptr == nullptr)return npos;
else return ptr - _str;
}
string string::substr(size_t pos, size_t len) {
assert(pos < _size);
size_t end = len + pos;
if (len == npos || len + pos >= _size) {
end = _size;
}
string str;
str.reserve(end - pos);
for (size_t i = pos; i < end; i++) {
str += _str[i];
}
return str;
}
void string::clear() {
_size = 0;
_str[0] = '\0';
}
void string::print_str() {
for (size_t i = 0; i < _size; i++) {
cout << _str[i] << ' ';
}
cout << endl;
}
string::~string() {
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
ostream& operator<<(ostream& out, const string& s) {
for (auto ch : s) {
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s) {
s.clear();
char buff[128];
char ch = in.get();
int i = 0;
while (ch != ' ' && ch != '\n') {
buff[i++] = ch;
if (i == 127) {
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0) {
buff[i] = '\0';
s += buff;
}
return in;
}
}
我们一般把短小的函数声明定义都放在类里面,这样就默认是内联,提高效率,另外缺省值只能声明给