C++ string类的实现
2016-07-01 17:23
513 查看
string类的简单实现
/*
* String类的简单实现
* Build 20160701
* Author: LonelyEnvoy
* Copyright (c) 2015-2016
* All Rights Reserved.
*/
#define MAX_LENGTH 50
#define NULL 0
#include <iostream>
using std::ostream;
using std::istream;
using std::cout;
using std::endl;
class String
{
public:
String(const char* s = NULL);
String(const String &obj);
~String();
void set(char* s);
char* get() const;
void print() const;
int size() const;
static int strlen(const char* s);
static char* strcat(char* s1, const char* s2);
static char* strcpy(char* s1, const char* s2);
String operator+(String& obj2);
String& operator=(String& obj2);
String& operator=(const char* s2);
String& operator+=(String& obj2);
char& operator[](unsigned n);
friend ostream & operator << (ostream & os, const String & obj);
friend istream & operator >> (istream & is, String & obj);
private:
char* str;
};
/*
* String类的简单实现
* Build 20160701
* Author: LonelyEnvoy
* Copyright (c) 2015-2016
* All Rights Reserved.
*/
#define MAX_LENGTH 50
#define NULL 0
#include <iostream>
using std::ostream;
using std::istream;
using std::cout;
using std::endl;
class String
{
public:
String(const char* s = NULL);
String(const String &obj);
~String();
void set(char* s);
char* get() const;
void print() const;
int size() const;
static int strlen(const char* s);
static char* strcat(char* s1, const char* s2);
static char* strcpy(char* s1, const char* s2);
String operator+(String& obj2);
String& operator=(String& obj2);
String& operator=(const char* s2);
String& operator+=(String& obj2);
char& operator[](unsigned n);
friend ostream & operator << (ostream & os, const String & obj);
friend istream & operator >> (istream & is, String & obj);
private:
char* str;
};
String::String(const char * s) { if (s == NULL) str = NULL; else { str = new char[strlen(s) + 1]; strcpy(str, s); } } String::String(const String & obj) { if (obj.str == NULL) str = NULL; else { str = new char[strlen(obj.str) + 1]; strcpy(str, obj.str); } } String::~String() { if (str == NULL) return; delete[]str; } void String::set(char * s) { if (s == NULL) return; if (str != NULL) delete[]str; str = new char[strlen(s) + 1]; int i; for (i = 0; i < strlen(s); i++) str[i] = s[i]; s[i] = '\0'; } char * String::get() const { return str; } void String::print() const { for (int i = 0; i < strlen(str); i++) cout << str[i]; cout << endl; } int String::size() const { return strlen(str); } int String::strlen(const char * s) { if (s == NULL) return 0; int i = 0; while (s[++i] != '\0'); return i; } char * String::strcat(char * s1, const char * s2) { // s1 must have enough space for storage if (s1 == NULL || s2 == NULL) return NULL; int i, j; for (i = strlen(s1), j = 0; j < strlen(s2); i++, j++) s1[i] = s2[j]; s1[i] = '\0'; return s1; } char * String::strcpy(char * s1, const char * s2) { // s1 must have enough space for storage if (s1 == NULL || s2 == NULL) return NULL; int i; for (i = 0; i < strlen(s2); i++) s1[i] = s2[i]; s1[i] = '\0'; return s1; } String String::operator+(String & obj2) { String s; if (str == NULL || obj2.str == NULL) return String(); else if (str == NULL) s = obj2; else if (obj2.str == NULL) s = *this; else { s.str = new char[strlen(str) + strlen(obj2.str) + 1]; strcpy(s.str, str); strcat(s.str, obj2.str); } return s; } String & String::operator=(String & obj2) { if (this != &obj2) { delete[]str; if (obj2.str == NULL) str = NULL; else { str = new char[strlen(obj2.str) + 1]; strcpy(str, obj2.str); } } return obj2; } String & String::operator=(const char * s2) { String temp(s2); operator=(temp); return temp; } String & String::operator+=(String & obj2) { operator=(operator+(obj2)); return obj2; } char & String::operator[](unsigned n) { if (n >= 0 && n < (unsigned)size()) return str ; throw 1; // 越界抛出异常 } ostream & operator<<(ostream & os, const String & obj) { os << obj.str; return os; } istream & operator>>(istream & is, String & obj) { char temp[MAX_LENGTH]; is >> temp; obj = temp; return is; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 关于指针的一些事情
- android上改变listView的选中颜色
- String.intern
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- php7 扩展类的写法[2]
- php7 类的方法传参[3]
- 一个简单的asp数据库操作类
- Prototype源码浅析 String部分(二)
- Ruby面向对象编程中类与方法的基础学习
- Ruby中的String对象学习笔记
- C#实现用于操作wav声音文件的类实例
- C#类的多态性详解
- 在线管理数据库 类
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析