C++ 05 string类的实现
2014-01-02 17:19
543 查看
1.atoi()函数的实现
2.string类的封装
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; }
相关文章推荐
- C++实现带写时拷贝功能的String类
- C++实现数字转为字符串string类对象
- C++面试题(二)——自己实现一个String类
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能。
- String类的字符串操作(C++实现)
- 如何用C++实现String类?
- C++实现带赋值运算符重载函数和迭代器的String类
- String类的实现 三大复制控制函数(C++笔试题 )
- C++【String类】String头插单个字符,头删单个字符的函数实现
- 【重温】C++中String类的实现
- C++中String类的实现
- C++实现String类
- 【C++】浅析浅拷贝,深拷贝及写时拷贝(copy_on_write),模拟实现String类。
- C++ 简易string类实现(三)-抽离引用计数
- C++实现的String类
- C++面试题,自己实现String类的构造函数,拷贝构造函数,赋值操作符,append,replace,+运算符重载等
- String类的C++实现
- C++中String类模拟实现以及深拷贝浅拷贝
- C++的String类的字符串分割实现----(效果等于Java的String类的split函数)
- C++笔试题 String类的实现