摘要

实现了一个string类,包括基本的构造、赋值、判断、大小写等。

String API

Constructors

string();
string(const char& ch);
string(const char* str);
string(const string& str);

Informational Methods

unsigned Length() const;
int Index(char c) const;

Case Methods

void UpCase(unsigned first, unsigned last);
void LowCase(unsigned first, unsigned last);

Stream Operators

friend std::ostream& operator<<(std::ostream& os, const string& str);
friend std::istream& operator>>(std::istream& is, string& str);

Accessor Operators

const char& operator[](unsigned i) const;
char& operator[](unsigned i);

Assignment Operators

String&  operator= (const String&)
String& operator+= (const String&)

String Concatenation

friend string operator+(const string& lhs, const string& rhs);
friend string operator+(const string& lhs, const char* rhs);
friend string operator+(const char* lhs, const string& rhs);

Logical Operators

friend bool operator==(const string& lhs, const string& rhs);
friend bool operator!=(const string& lhs, const string& rhs);
friend bool operator<(const string& lhs, const string& rhs);
friend bool operator>(const string& lhs, const string& rhs);
#include "string.h"

using namespace vlyf;

inline
vlyf::string::string()
{
length = 0;
data = new char[0];
} inline
string::string(const char* str)
{
if (str)
{
unsigned n = 0;
while (str[n] != '\0') n++;
data = new char[n];
length = n;
for (unsigned i = 0; i < n; i++)
{
data[i] = str[i];
}
}
else
{
length = 0;
data = new char[0];
}
} inline
string::string(const string& str)
{
unsigned len = str.Length();
length = len;
data = new char[len];
for (unsigned i = 0; i < len; i++)
{
data[i] = str[i];
}
} inline
string::~string()
{
delete[]data;
} inline
unsigned string::Length() const
{
return length;
} int string::Index(char c) const
{
for (unsigned i = 0; i < Length(); i++)
{
if (c == data[i]) return i;
}
return -1;
} inline
void string::UpCase(unsigned first, unsigned last)
{
while (first++ < last)
{
if ('a' <= data[first] && data[first] <= 'z')
data[first] -= ('a' - 'A');
}
} inline
void string::LowCase(unsigned first, unsigned last)
{
while (first++ < last)
{
if ('A' <= data[first] && data[first] <= 'Z')
data[first] += ('a' - 'A');
}
} const char& vlyf::string::operator[](unsigned i) const
{
return data[i];
} char& vlyf::string::operator[](unsigned i)
{
return data[i];
} string& vlyf::string::operator=(const char* s)
{
char* temp = data; //保存原本数据,确保异常安全性
unsigned n = 0;
while (s[n] != '\0')
n++;
length = n;
data = new char[n];
for (unsigned i = 0; i < n; i++)
data[i] = s[i];
delete[] temp;
return *this;
} inline
string& vlyf::string::operator=(const string& str)
{
if (this == &str) return *this;
char* temp = str.data; //保存原本数据,确保异常安全性 unsigned len = str.Length();
data = new char[len];
for (unsigned i = 0; i < len; i++)
data[i] = str[i];
length = len;
delete[] temp;
return *this;
} inline
string& string::operator+=(const string& str)
{
unsigned len = length + str.Length();
char* ss = new char[len];
for (unsigned i = 0; i < Length(); i++)
ss[i] = data[i];
for (unsigned i = 0; i < str.Length(); i++)
ss[i + length] = str[i];
delete[]data;
data = ss;
length = len;
return *this;
} std::ostream& vlyf::operator<<(std::ostream& os, const string& str)
{
if (str.Length() > 0)
{
for (unsigned i = 0; i < str.Length(); i++)
os << str[i];
}
else
{
os << "";
}
return os;
} std::istream& vlyf::operator>>(std::istream& is,string& str)
{
char* ss = new char[1000];
is >> ss;
str = string(ss);
return is;
} string vlyf::operator+(const string& lhs, const string& rhs)
{
return string(lhs) += rhs;
} string vlyf::operator+(const string& lhs, const char* rhs)
{
return string(lhs) += string(rhs);
} string vlyf::operator+(const char* lhs, const string& rhs)
{
return string(lhs) += string(rhs);
} bool vlyf::operator==(const string& lhs, const string& rhs)
{
if (lhs.Length() != rhs.Length())
return false;
unsigned n = 0;
unsigned len = lhs.Length();
while (lhs[n] == rhs[n] && len != n)
n++;
return n == len;
} bool vlyf::operator!=(const string& lhs, const string& rhs)
{
if (lhs.Length() != rhs.Length())
return true;
unsigned n = 0;
unsigned len = lhs.Length();
while (lhs[n] == rhs[n] && len != n)
n++;
return n != len;
} bool vlyf::operator<(const string& lhs, const string& rhs)
{
unsigned min = (lhs.Length() < rhs.Length()) ? lhs.Length() : rhs.Length();
unsigned n = 0;
while (lhs[n] == rhs[n] && n != min)
n++;
if (n == min) return lhs.Length() < rhs.Length();
else
{
if (lhs[n] < rhs[n])
return lhs.Length() < rhs.Length();
else
return lhs.Length() > rhs.Length();
}
} bool vlyf::operator>(const string& lhs, const string& rhs)
{
return lhs < rhs;
} int main()
{
string s1 = "123";
std::cout << "s1:" << s1 << " length: " << s1.Length() << std::endl; string s2("456");
std::cout << "s2:" << s2 << " length: " << s2.Length() << std::endl; string s3(s1);
std::cout << "s3:" << s3 << " length: " << s3.Length() << std::endl; string s4 = s3;
std::cout << "s4:" << s4 << " length: " << s4.Length() << std::endl; string s5(s1);
s5 += s2;
std::cout << "s5:" << s5 << " length: " << s5.Length() << std::endl; string s6(s5);
std::cout << "s6:" << s6 << " length: " << s6.Length() << std::endl; if (s5 == s6) std::cout << "s5 == s6: true" << std::endl; string s7 = s1 + "456";
std::cout << "s7:" << s7 << " length: " << s7.Length() << std::endl; string s8 = "456" + s1;
std::cout << "s8:" << s8 << " length: " << s8.Length() << std::endl; string s9 = s7 + s8;
std::cout << "s9:" << s9 << " length: " << s9.Length() << std::endl; return 0;
}

实现简单的string类的更多相关文章

  1. 自己实现简单的string类

    1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...

  2. 一个简单的string类,读书看报系列(一)

    对于这个类,写过程序的都知道应该含有的方法是 初始化.销毁.拼接.求长度.清除.判断是否为空等.还有一些操作符重载 一.先看初始化: 可以想到应该有默认构造的的.带有字符串的.带有默认字符的.还有一个 ...

  3. 【c++】简单的string类的几个基本函数

    // string的几个基本函数的实现 #include <iostream> #include <assert.h> #include <string.h> us ...

  4. 【Java】整理关于java的String类,equals函数和比较操作符的区别

    初学 Java 有段时间了,感觉似乎开始入了门,有了点儿感觉但是发现很多困惑和疑问而且均来自于最基础的知识折腾了一阵子又查了查书,终于对 String 这个特殊的对象有了点感悟大家先来看看一段奇怪的程 ...

  5. c++在string类源

    一:回想 (1)c++中的string类是在面试中和笔试中常常考的题目: project代码免费下载 string类的自行实现 (2)c++中的string类和fstream类合起来是处理外部数据的利 ...

  6. 全面深入介绍C++字符串:string类

    http://blog.csdn.net/liuliming3000/article/details/1809385 1 从C到C++ string类 2 string类的构造函数 3 string类 ...

  7. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  8. C++ char数组和string类简单使用总结

    使用char数组,进行字符串的操作,是c风格的操作方式. string是C++的风格,感觉string本质上就是一个vector<char> 以下代码详细展示了字符串的常见操作 #incl ...

  9. c++string类的简单介绍

    #include "iostream" #include "string" using namespace std; /*@author:浅滩 *family: ...

随机推荐

  1. 无向图边双联通分量 tarjan 模板

    #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int MAXM = 500005 ...

  2. Numpy | 10 广播(Broadcast)

    广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 下面的图片展示了数组 b 如何通过广播来与数组 a 兼容. 4x ...

  3. Hibernate的事务

    1.数据库的封锁(https://www.cnblogs.com/zhai1997/p/11710082.html): 封锁是实现并发控制的重要技术. read uncommitted : 读取尚未提 ...

  4. datetime.now()提示没有now方法

    py3.6 导入方法是 from datetime import datetime 在使用datetime.now()的时候报错,说没有now 在保存module的create_time字段的时候,提 ...

  5. 洛谷P4170 [CQOI2007]涂色题解

    废话: 这个题我第一眼看就是贪心呐, 可能是我之前那做过一道类似的题这俩题都是关于染色的 现在由于我帅气无比的学长的指导, 我已经豁然开朗, 这题贪心不对啊, 当时感觉自己好厉害贪心都能想出来 差点就 ...

  6. vs2017使用OpenGL的方法

    第一步:将相应的.lib和.h文件放到相应的文件夹中,具体如下: 将GLAUX.LIB,GLU32.LIB,OPENGL32.LIB 放到....\VC\Tools\MSVC\14.10.25017\ ...

  7. 洛谷2051 [AHOI2009]中国象棋

    题目链接 题意概述:n行m列棋盘放若干个棋子每行每列最多两个求方案总数,答案对9999973取模. 可以比较容易看出这是个dp,设f[i][j][k]表示前i行j列放1个棋子k列放2个棋子的方案总数. ...

  8. DDD 全称 “Domain-Driven Design”,领域驱动设计

    大型软件项目的最复杂之处不是实现,而是软件所服务的真正的领域. 领域驱动设计就是用来处理这些高度复杂领域的理想和途径,使得领域本身成为项目关注的焦点,从而达到维护能深刻反映领域的软件模型的目的. 通过 ...

  9. Linux:搭建GitLab

    0.写在前面 GitLab官方明确要求最低配置2核4G,如果配置过低,会出现502错误. 1.安装SSH #安装ssh sudo yum install -y curl policycoreutils ...

  10. 【Gamma】设计与计划

    目录 需求分析 已实现 功能 用户使用动机分析 当前阶段推广困难 当前阶段任务优先级 主要功能解析 社团活动场地申请 - 实现成本较高,正在调研社长的需求 完善入社流程的信息提示 通知功能 通知管理 ...