#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
typedef long long int ll;
#define f(i,a,n) for(int i=a;i<n;++i)
#define ff(i,a,n) for(int i=a;i<=n;++i)
#define fre freopen("1.txt","r",stdin)
class myString{
char *data;int length;
public:
myString(){
data = NULL;
length = 0;
}
myString(char a[]){
length = strlen(a);
data=new char[length];
for(int i=0; i<length;i++){
if(a[i]=='\0'||a[i]=='\t'||a[i]=='\r')break;
else data [i]=a[i];
}
}
~myString(){
if(data != NULL)delete []data;
data =NULL;
}
int size(){
int siz=0;
f(i,0,length)siz++;
return siz;
}
void output(){
for(int i=0; i<length;i++)cout<<data[i];
}
void set(char a[]){ // 拷贝构造函数
length = strlen(a);
data=new char[length];
for(int i=0; i<length;i++){
if(a[i]=='\0'||a[i]=='\t'||a[i]=='\r')break;
else data [i]=a[i];
}
}
myString( myString & r){ // 拷贝构造函数
length=r.length;
data = new char [r.length];
for(int i=0;i<length;i++)data[i]=r.data[i];
}
myString& operator =(const myString &a) {//深拷贝
if (data){
delete[]data;data = NULL;
length= 0;
}
length = a.length;
data = new char[length];
for(int i=0;i<length;i++)data[i] = a.data[i];
}
myString& operator + ( myString& str ){
char* s ;
s= data;
int len = strlen(data) + strlen(str.data)+1 ;
data = new char[len];
strcpy(data,s);
delete[] s;
f(i,0,str.length)data[length+i]=str.data[i];
length += str.length;
return *this;
}
myString sub(int i,int num){
myString s;
if(i>length);
if(i+num<=length){
s.length=num;
s.data=new char[num];
f(j,0,num)s.data[j]=data[i+j-1];
}
if(i+num>length){//越界了
s.length=length-i+1;
s.data=new char[num];
f(j,0,s.length)s.data[j]=data[i+j-1];
}
return s;
}
friend istream& operator>>(istream& cin, myString& str){
string s;getline(cin,s);
//getchar();
str.length=s.size();
str.data=new char[s.size()];
for(int i=0;i<s.size();i++)str.data[i]=s[i];
return cin;
}
friend ostream& operator<<(ostream& cout , myString& str){
if (str.length){
//cout<<str.length<< " : ";
f(i,0,str.length)cout<<str.data[i];
}
else cout<<0;
return cout;
}
bool operator ==(const myString& a) {
if (a.length == length) {
for (int i = 0; i < length; i++) {
if (a.data[i] != data[i]) return false;
}
return true;
}
else return false;
}
bool operator >(const myString& a) {
string s=this->data;string b=a.data;
if(s>b)return true;
return false;
}
bool operator <(const myString& a) {
string s=this->data;string b=a.data;
if(s<b)return true;
return false;
}
char&operator[](int i){
return data[i];
}
const char & operator[](int i) const{
return data[i];
}
};
void read( int *p, int n ) {
for ( int i = 0; i < n; i++ )
cin >> p[i];
}
int main() {
myString a;
cin >> a;
cout << a << endl;
for ( int i = 0; i < a.size(); i ++ )
cin >> a[i];
for ( int i = 0; i < a.size(); i ++ )
cout << a[i];
cout << endl;
read( a, 20 );
cout << a << endl;
return 0;
}
string oj1846 read函数不会
最新推荐文章于 2024-11-09 20:38:27 发布
这篇博客介绍了一个自定义的C++字符串类`myString`,包括构造函数、拷贝构造函数、赋值运算符重载、加法运算符重载、子串提取、输入输出流操作符重载等成员函数的实现。通过这个类,可以进行字符串的基本操作和比较。
摘要由CSDN通过智能技术生成