您的位置:首页 > 编程语言 > C语言/C++

C++ 05 string类的实现

2014-01-02 17:19 543 查看
1.atoi()函数的实现

const char* convert(char buf[], int val) // buf 用来保存字符串
{
static char digits[19] =
{
'9', '8', '7', '6', '5', '4', '3', '2', '1',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
static const char* zero = digits + 9; // zero point to '0'

int i = val;
char* p = buf;
do
{
int lsd = i % 10; // 余数 可能为负数
i = i / 10;       // 除10取整 即去掉最后一位
*p++ = zero[lsd]; // p指向zero+偏移量(正负都可能) 之后,p++指向下一个位置
} while (i != 0);     // 直到i为0时

if(val < 0)
{
*p++ = '-';
}
*p = '\0';

std::cout<<buf<<std::endl;

std::reverse(buf, p); // length = p - buf

return buf;
}


2.string类的封装

my_string.h

#ifndef STRING_H
#define STRING_H

#include <iostream>
using namespace std;

class String
{
public:
String(const char* str="");
String(const String& other);
~String();
String& operator=(const String& other);
String& operator=(const char* str);
char& operator[](unsigned int index);
char& operator[](unsigned int index) const;
bool operator!() const;
friend String operator+(const String& s1, const String& s2);
String& operator+=(const String& other);
friend ostream& operator<<(ostream& os, const String& str);
friend istream& operator>>(istream& is, String& str);

private:
char* str_;
char* AllocAndCopy(const char* str);/*实施深拷贝*/
};

#endif // STRING_H

my_string.cpp
#include <iostream>
#include "string.h"

using namespace std;

String::String(const char *str)
{
str_ = AllocAndCopy(str);
}
String::String(const String &other)
{
str_ = AllocAndCopy(other.str_);
}
String::~String()
{
delete[] str_;
}
/*深拷贝函数*/
char* String::AllocAndCopy(const char* str)
{
int len = strlen(str)+1;/*构造函数*/
str_ = new char(len);
memset(str_, 0, len);
strcpy(str_, str);

return str_;
}
String& String::operator =(const String& other)
{
if(this == &other)
{
return *this;
}
delete[] str_;
str_ = AllocAndCopy(str_);
return *this;
}
String& String::operator=(const char* str)
{
delete[] str_;
str_ = AllocAndCopy(str);
return *this;
}
bool String::operator !()const
{
return strlen(str_) != 0;
}
/* []运算符的重载 相同的代码让non const版本调用 const版本 */
char& String::operator [](unsigned int index)
{
return const_cast<char&>(static_cast<const String>(*this)[index]);
}
/* []运算符的重载 操作const对象*/
char& String::operator [](unsigned int index)const
{
return str_[index];
}
String operator+(const String& s1, const String& s2)
{
String str = s1;
str += s2;
return str;
}
String& String::operator +=(const String& other)
{
int len = strlen(str_) + strlen(other.str_)+1;
char* new_str = new char[len];/*分配空间 记得释放*/
memset(new_str, 0, len);
strcpy(new_str, str_);
strcat(new_str, other.str_);

delete[] str_;
str_ = new_str;

return *this;
}
ostream& operator<<(ostream& os, const String& str)
{
os<<str.str_;
return os;
}
istream& operator>>(istream& is, String& str)
{
is>>str.str_;
return is;
}


#include "str.h"
#include "common.h"

void str_trim_crlf(char *str)
{
char *p = &str[strlen(str)-1];
while (*p == '\r' || *p == '\n')
*p-- = '\0';

}

void str_split(const char *str , char *left, char *right, char c)
{
char *p = strchr(str, c);
if (p == NULL)
strcpy(left, str);
else
{
strncpy(left, str, p-str);
strcpy(right, p+1);
}
}

int str_all_space(const char *str)
{
while (*str)
{
if (!isspace(*str))
return 0;
str++;
}
return 1;
}

void str_upper(char *str)
{
while (*str)
{
*str = toupper(*str);
str++;
}
}

long long str_to_longlong(const char *str)
{
long long result = 0;
long long mult = 1;
unsigned int len = strlen(str);
int i;

if (len > 15)
return 0;

for (i=len-1; i>=0; i--)
{
char ch = str[i];
long long val;
if (ch < '0' || ch > '9')
return 0;

val = ch - '0';
val *= mult;
result += val;
mult *= 10;
}

return result;
}

unsigned int str_octal_to_uint(const char *str)
{
unsigned int result = 0;
int seen_non_zero_digit = 0;

while (*str)
{
int digit = *str;
if (!isdigit(digit) || digit > '7')
break;

if (digit != '0')
seen_non_zero_digit = 1;

if (seen_non_zero_digit)
{
result <<= 3;
result += (digit - '0');
}
str++;
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: