// ------String.h------
#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;
class String
{
public:
String();
String(int n, char c);
String(const char* source);
String(const String& s);
// String& operator = (char* s);
String& operator = (const String& s);
~String();
char& operator [] (int i){return a[i];}
const char& operator [] (int i) const {return a[i];} // Indexer to constant.
String& operator += (const String& s);
int length();
friend istream& operator >> (istream& is, String& s); // Try to figure out why set ">>" friend function member.
// friend bool operator < (const String& left,const String& right);
friend bool operator > (const String& left, const String& right); // These three operators is not necessary to set them friend. Here is just for simplification.
friend bool operator == (const String& left, const String& right);
friend bool operator != (const String& left, const String& right);
private:
char* a;
int size;
};
#endif
// ------String.cpp------
#include "String.h"
#include <cstring>
#include <cstdlib>
String::String()
{
a = new char[1];
a[0] = '\0';
size = 0;
}
String::String(int n, char c)
{
a = new char[n + 1];
memset(a, c, n);
a[n] = '\0';
size = n;
}
String::String(const char* source)
{
if(source == NULL)
{
a = new char[1];
a[0] = '\0';
size = 0;
}
else
{
size = strlen(source);
a = new char[size + 1];
strcpy(a, source);
}
}
String::String(const String& s)
{
size = strlen(s.a);
a = new char[size + 1];
strcpy(a,s.a);
}
String& String::operator = (const String& s)
{
if(this == &s)
return *this;
else
{
delete[] a;
size = strlen(s.a);
a = new char[size + 1];
strcpy(a, s.a);
return *this;
}
}
String::~String()
{
delete[] a;
}
String& String::operator += (const String& s)
{
int j = strlen(a);
int size = j + strlen(s.a);
char* tmp = new char[size + 1];
strcpy(tmp, a);
strcpy(tmp + j, s.a);
delete[] a;
a = tmp;
return *this;
}
int String::length()
{
return strlen(a);
}
// ------Main.cpp------
#include <iostream>
#include "String.h"
using namespace std;
bool operator == (const String& left, const String& right)
{
int a = strcmp(left.a, right.a);
if(a == 0)
{
return true;
}
else
{
return false;
}
}
bool operator != (const String& left, const String& right)
{
return !(left == right);
}
ostream& operator << (ostream& os, String& s)
{
int length = s.length();
for(int i = 0; i < length; i++)
{
os << s[i];
}
return os;
}
String operator + (const String& a, const String& b)
{
String temp;
temp = a;
temp += b;
return temp;
}
bool operator < (const String& left, const String& right)
{
int j = 0;
while((left[j] != '\0') && (right[j] != '\0'))
{
if(left[j] < right[j])
{
return true;
}
else
{
if(left[j] == right[j])
{
j++;
continue;
}
else
{
return false;
}
}
}
if((left[j] == '\0') && (right[j] != '\0'))
{
return true;
}
else
{
return false;
}
}
bool operator > (const String& left, const String& right)
{
int a = strcmp(left.a, right.a);
if(a > 0)
{
return true;
}
else
{
return false;
}
}
istream& operator >> (istream& is, String& s)
{
delete[] s.a;
s.a = new char[20];
int m = 20;
char c;
int i = 0;
while(is.get(c) && isspace(c));
if(is)
{
do
{
s.a[i] = c;
i++;
if(i == m - 1)
{
s.a[i] = '\0';
char* b = new char[m];
strcpy(b, s.a);
m = m * 2;
s.a = new char[m];
strcpy(s.a, b);
delete[] b;
}
}
while (is.get(c) && !isspace(c));
if(is)
{
is.unget();
}
}
s.size = i;
s.a[i] = '\0';
return is;
}
int main()
{
String a = "abcd";
String b = "www";
String c(6, 'l');
String d;
String e = a;
String f;
cin >> f;
String g;
g = a + b;
if(a < b)
{
cout << "a < b" << endl;
}
else
{
cout << "a >= b" << endl;
}
if(e == a)
{
cout << "e == a" << endl;
}
else
{
cout << "e != a" << endl;
}
b += a;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
cout << g << endl;
cout << g[0] << endl;
return 0;
}
// Output:
/*
t
a < b
e == a
abcd
wwwabcd
llllll
abcd
t
abcdwww
a
*/
分享到:
相关推荐
1.6.1 Packet Tracer - Implement a Small Network Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
7.4.1 Packet Tracer - Implement DHCPv4 Cisco Packet Tracer 思科模拟器 正确答案文件 由于程序问题无法保存激活DHCP端口配置 另加满分步骤截图 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次...
3.6.1 Packet Tracer - Implement VLANs and Trunking Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
6.4.1 Packet Tracer - Implement Etherchannel Cisco Packet Tracer 思科模拟器 正确答案文件 可直接上交正确答案文件 本答案版权归mewhaku所有,严禁再次转载!!! Copyright @mewhaku 2022 All Rights ...
2.7.6 Packet Tracer - Implement Basic Connectivity.pka
6.4.1-packet-tracer---implement-etherchannel.pka
RSA-Montgomery-Algorithm-Hardware-implement-master.zip
Implement with Class and Collection a List Collection with add and remove data elements.
faruto改进的libsvm库,增加了遗传算法,粒子群、网格寻优等函数。
how-to-implement-a-DB-like-sqlite ##Description 在这个项目中我们会学习SQLite源码并仿制其做成一个完整的数据库. 整个过程中我们会对SQLite进行多次demo, 不断的加入新功能, 直到其成为一个安全, 快速, 功能较为...
前端大厂最新面试题-implement-map-using-reduce.docx
前端大厂最新面试题-implement-queue-using-stack.docx
With more than 100 practical recipes, this book is a guide showcasing techniques to use the power of C++ scripting while developing games with UE4. It will start with adding and editing C++ classes ...
Phase-1-Implement-OOPS-using-JAVA-with-Data-Structures-and-Beyond:-1期项目
@RunWith(JUnit4.class) public class TestStringGenerator { } Make it extends from IconicsStringGenerator @RunWith(JUnit4.class) public class TestStringGenerator extends IconicsStringGenerator { } ...
深度学习领域YOLOV7足球比赛视频目标检测(带数据集)--yolov7-explanation-and-implementation-from-scratch 语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用yolo算法在足球比赛中目标检测。...
用全球路径服务实现另一种域间路由,张威,毕军,本文介绍了一种基于集中路由服务的新域间路由方案:全球路径服务(GPS)。该方案通过域间隧道技术(如MPLS)提供不同于GPS的域间路��
In the concluding chapters, we will focus on a multitude of real-world case studies and problems associated with areas such as computer vision, audio analysis and natural language processing (NLP). ...
Machine Learning: Step-by-Step Guide To Implement Machine Learning Algorithms with Python By 作者: Rudolph Russell ISBN-10 书号: 1719528403 ISBN-13 书号: 9781719528405 出版日期: 2018-05-22 pages ...
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...