本文共 4742 字,大约阅读时间需要 15 分钟。
串第一次听说,实际上就和C++封装好的string对象类似。本例题中,利用C++自实现了string类的一些功能函数,一方面回顾c++的一些知识,另一方面也加深对string对象的了解。编程过程中,为了省事,也或多或少利用了string的函数,例如size(),clear()等。
主要函数功能介绍:Stringv1() { length = 0; } //构造函数Stringv1(const Stringv1 & s); //复制构造函数Stringv1(const char *c); //将c风格字符串转string对象Stringv1 &operator=(const Stringv1 & s); //赋值函数void Show() const; //展示对象信息bool ClearString(); //清除对象bool isEmpty() const { return (length == 0); } //判断串是否为空int Length() const { return length; } //返回串的长度string SubString(int pos, int len); //返回从pos开始的后len长度的串int Index(const string & s, const int pos); //查找pos之后的第一个与s相同的子串int Replace(const string & s1, const string & s2); //用s2替换掉串中的s1string StrInsert(const string & s1, int pos); //在串的pos之后插入子串s1//这里用到了两个友元函数,目的是为了加强练习,也可以用类的公有函数代替friend int StrCompare(const Stringv1 & s1, const Stringv1 & s2);//比较两个串是否相同friend Stringv1 Concat(const Stringv1 & s1, const Stringv1 & s2);//将两个串连接在一起
示例输出结果:
This is a char array!has 21 charater.This is a char array!has 21 charater.String is cleared!The two string is equal!Substring:is a charFind the first substring "is" at 2Using "at" replace isAfter 2 time replace, the string is: That at a char array!has 21 charater.After insert a string:That insert at a char array!
串的基本功能实现头文件:
#pragma once#ifndef STRINGV1_H_#define STRINGV1_H_#include#include using namespace std;class Stringv1{ private: string data; int length;public: Stringv1() { length = 0; } Stringv1(const Stringv1 & s); Stringv1(const char *c); Stringv1 &operator=(const Stringv1 & s); void Show() const; bool ClearString(); bool isEmpty() const { return (length == 0); } int Length() const { return length; } string SubString(int pos, int len); int Index(const string & s, const int pos); int Replace(const string & s1, const string & s2); string StrInsert(const string & s1, int pos); friend int StrCompare(const Stringv1 & s1, const Stringv1 & s2); friend Stringv1 Concat(const Stringv1 & s1, const Stringv1 & s2);};Stringv1::Stringv1(const Stringv1 & s){ data = s.data; length = s.length;}Stringv1::Stringv1(const char *c){ length = strlen(c); for (int i = 0; i < length; i++) data.push_back(c[i]);}Stringv1 &Stringv1::operator=(const Stringv1 & s){ data = s.data; length = s.length; return *this;}void Stringv1::Show() const{ cout << data << endl; cout << "has " << length << " charater.\n";}bool Stringv1::ClearString(){ length = 0; data.clear(); return true;}int StrCompare(const Stringv1 & s1, const Stringv1 & s2){ if (s1.data.size() == 0 || s2.data.size() == 0) { cout << "Least an object is null.\n"; exit(EXIT_FAILURE); } else { int i = 0; while (i length) { cout << "The substring is too long!\n"; exit(EXIT_FAILURE); } else for (int i = 0; i < len; i++) out.push_back(data[pos + i]); return out;}int Stringv1::Index(const string & s, const int pos){ if (s.empty()) { cout << "is empty!\n"; return 0; } for (int i = pos; i < length; i++) { int j = 0; while (j < s.size() && i < length&&s[j] != data[i]) i++; int temp = i; while (j < s.size() && i < length&&s[j] == data[i]) { i++; j++; } if (j == s.size()) return temp; else i = temp + 1; } return 0;}int Stringv1::Replace(const string & s1, const string & s2){ if (s1.size() != s2.size()) { cout << "Must have same length!\n"; return 0; } int time = 0; for (int i = 0; i < length; ) { int index = Index(s1, i); if (index) { for (char temp : s2) { data[index] = temp; index++; } time++; i = index + 1; } else if (index == 0) return time; }}string Stringv1::StrInsert(const string & s1, int pos){ if (s1.size() == 0) cout << "The string is empty!\n"; if (pos > length) cout << "The index is out of range!\n"; return this->SubString(0, pos - 1) + s1 + this->SubString(pos - 1, length - pos + 1);}#endif // !STRINGV1_H_
串的基本功能示例:
#include#include #include"stringv1.h"using namespace std;void main(){ char test1[] = "This is a char array!"; Stringv1 test2(test1); test2.Show(); Stringv1 test3; test3 = test2; test3.Show(); if (test3.ClearString()) cout << "String is cleared!\n"; if (!StrCompare(test1, test2)) cout << "The two string is equal!\n"; cout << "Substring: \n"; cout << test2.SubString(5, 10) << endl; string s1 = "is"; cout << "Find the first substring \"" << s1 << "\" at "; cout << test2.Index(s1, 1) << endl; string s2 = "at"; cout << "Using \"" << s2 << "\" replace " << s1 << endl; int time = test2.Replace(s1, s2); cout << "After " << time << " time replace, the string is: "; test2.Show(); string s3 = "insert "; cout << "After insert a string: \n"; cout << test2.StrInsert(s3, 6) << endl;}
转载地址:http://zpyci.baihongyu.com/