2
已解决
目前完成了string和vector
代码先放在这里(可能会有部分小问题)
#include<iostream>
#include<cstring>
using std::ostream;
using std::cout;
using std::endl;
using std::out_of_range;
using std::copy;
namespace STRING{
class STRing{
private:
char* str;
int len;
public:
class Iterator{
friend STRing;
private:
char* st;
public:
Iterator(){
st=nullptr;
}
Iterator(char* other){
st=other;
}
Iterator(const Iterator& other){
st=other.st;
}
~Iterator(){}
Iterator& operator=(char* other){
st=other;
return *this;
}
Iterator& operator=(const Iterator& other){
if(this!=&other){
st=other.st;
}
return *this;
}
char& operator*(){
return *st;
}
char* Ar(){
return st;
}
Iterator& operator++(){
++st;
return *this;
}
Iterator operator++(int){
Iterator old(st);
++st;
return old;
}
Iterator& operator--(){
--st;
return *this;
}
Iterator operator--(int){
Iterator old(st);
--st;
return old;
}
Iterator operator+(const int other){
return Iterator(st+other);
}
Iterator operator-(const int other){
return Iterator(st-other);
}
int operator-(const char* other){
return st-other;
}
int operator-(const Iterator& other){
return st-other.st;
}
bool operator==(const Iterator& other)const{
return (st==other.st);
}
bool operator!=(const Iterator& other)const{
return !(*this==other);
}
};
public:
STRing(){
str=new char[1];
str[0]='\0';
len=0;
}
STRing(const char c){
char* k;
k=new char[2];
k[0]=c;
k[1]='\0';
str=new char[2];
strcpy(str,k);
delete[] k;
str[1]='\0';
len=1;
}
STRing(const STRing& other){
int len2=other.len;
str=new char[len2+1];
strcpy(str,other.str);
str[len2]='\0';
len=len2;
}
STRing(const std::initializer_list<char>& other){
int Len=other.size();
str=new char[Len+1];
std::copy(other.begin(),other.end(),str);
str[Len]='\0';
len=Len;
}
~STRing(){
delete[] str;
len=0;
}
STRing& operator=(const char* other){
delete[] str;
int len2=strlen(other);
str=new char[len2+1];
strcpy(str,other);
str[len2]='\0';
len=len2;
return *this;
}
STRing& operator=(const STRing& other){
delete[] str;
int len2=other.len;
str=new char[len2+1];
strcpy(str,other.str);
str[len2]='\0';
len=len2;
return *this;
}
char& operator[](int index){
return str[index];
}
STRing operator+(const char c){
STRing other(c);
int len1=len;
int len2=other.len;
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other.str);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
result.str[len1+len2]='\0';
delete[] tmp;
return result;
}
STRing operator+(const char* other){
int len1=len;
int len2=strlen(other);
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
result.str[len1+len2]='\0';
delete[] tmp;
return result;
}
STRing operator+(const STRing& other){
int len1=len;
int len2=other.len;
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other.str);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
result.str[len1+len2]='\0';
delete[] tmp;
return result;
}
int Length()const{
return len;
}
int Size()const{
return len;
}
char* C_str()const{
return str;
}
STRing(const char* s){
int len2=strlen((char*)s);
str=new char[len2+1];
strcpy(str,s);
str[len2]='\0';
len=len2;
}
STRing& operator+=(const char c){
STRing other(c);
int len1=len;
int len2=other.len;
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other.str);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
delete[] tmp;
delete[] str;
int len3=result.len;
str=new char[len3+1];
strcpy(str,result.str);
str[len3]='\0';
len=len3;
return *this;
}
STRing& operator+=(const char* other){
int len1=len;
int len2=strlen(other);
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
delete[] tmp;
delete[] str;
int len3=result.len;
str=new char[len3+1];
strcpy(str,result.str);
str[len3]='\0';
len=len3;
return *this;
}
STRing& operator+=(const STRing& other){
int len1=len;
int len2=other.len;
char* tmp=new char[len1+len2+1];
strcpy(tmp,str);
strcat(tmp,other.str);
tmp[len1+len2]='\0';
STRing result(tmp);
result.len=len1+len2;
delete[] tmp;
delete[] str;
int len3=result.len;
str=new char[len3+1];
strcpy(str,result.str);
str[len3]='\0';
len=len3;
return *this;
}
bool operator==(const STRing& other){
return strcmp(str,other.str)==0;
}
bool operator<(const STRing& other){
return strcmp(str,other.str)<0;
}
bool operator>(const STRing& other){
return strcmp(str,other.str)>0;
}
bool operator!=(const STRing& other){
return strcmp(str,other.str)!=0;
}
bool operator<=(const STRing& other){
return strcmp(str,other.str)<=0;
}
bool operator>=(const STRing& other){
return strcmp(str,other.str)>=0;
}
STRing Substr(int pos,int Len){
STRing result;
for(int i=pos;i<pos+Len;i++){
result+=str[i];
}
return result;
}
void Erase(int pos,int Len){
STRing result;
for(int i=0;i<pos;i++){
result+=str[i];
}
for(int i=pos+Len;i<len;i++){
result+=str[i];
}
delete[] str;
str=new char[result.len+1];
strcpy(str,result.str);
str[result.len]='\0';
len=result.len;
return ;
}
void Insert(int pos,STRing other){
STRing result;
for(int i=0;i<pos;i++){
result+=str[i];
}
result+=other;
for(int i=pos;i<len;i++){
result+=str[i];
}
delete[] str;
str=new char[result.len+1];
strcpy(str,result.str);
str[result.len]='\0';
len=result.len;
return ;
}
void Replace(int pos,int Len,STRing other){
STRing result;
for(int i=0;i<pos;i++){
result+=str[i];
}
result+=other;
for(int i=pos+Len;i<len;i++){
result+=str[i];
}
delete[] str;
str=new char[result.len+1];
strcpy(str,result.str);
str[result.len]='\0';
len=result.len;
return ;
}
int Find(STRing other,int pos=0){
int len2=other.len;
for(int i=pos;i<=len-len2;i++){
STRing s=Substr(i,len2);
if(s==other)return i;
}
return -1;
}
Iterator Begin(){
Iterator it=str;
return it;
}
Iterator End(){
Iterator it=str+(len-1);
return it;
}
};
ostream& operator<<(ostream& os,const STRing& str){
os<<str.C_str();
return os;
}
STRing operator+(const char c,const STRing other){
STRing s;
s+=c;
s+=other;
return s;
}
}
namespace VECTOR{
using namespace STRING;
namespace srcfu{
template <typename vt>
void srccpy(vt* src,vt* other,int len){
for(int i=0;i<len;i++){
src[i]=other[i];
}
}
}
template <typename vt>
class Vector{
private:
#define srccpy(src,other,len) VECTOR::srcfu::srccpy(src,(vt*)other,len)
vt* data;
int len;
int cnt;
int ed;
private:
static vt null;
public:
class Iterator{
friend Vector;
private:
vt* st;
public:
Iterator(){
st=nullptr;
}
Iterator(vt* other){
st=other;
}
Iterator(const Iterator& other){
st=other.st;
}
~Iterator(){}
Iterator& operator=(vt* other){
st=other;
return *this;
}
Iterator& operator=(const Iterator& other){
if(this!=&other){
st=other.st;
}
return *this;
}
vt& operator*(){
return *st;
}
vt* Ar(){
return st;
}
Iterator& operator++(){
++st;
return *this;
}
Iterator operator++(int){
Iterator old(st);
++st;
return old;
}
Iterator& operator--(){
--st;
return *this;
}
Iterator operator--(int){
Iterator old(st);
--st;
return old;
}
Iterator operator+(const int other){
return Iterator(st+other);
}
Iterator operator-(const int other){
return Iterator(st-other);
}
int operator-(const vt* other){
return st-other;
}
int operator-(const Iterator& other){
return st-other.st;
}
bool operator==(const Iterator& other)const{
return (st==other.st);
}
bool operator!=(const Iterator& other)const{
return !(*this==other);
}
};
public:
Vector(){
data=new vt[0];
len=0;
ed=0;
cnt=0;
}
Vector(const int Len,const vt iv=null){
data=new vt[Len];
for(int i=0;i<Len;i++){
data[i]=iv;
}
len=Len;
ed=Len-1;
cnt=Len;
}
Vector(const Vector& other){
int Len=other.len;
data=new vt[Len];
for(int i=0;i<Len;i++){
data[i]=other[i];
}
len=Len;
ed=Len-1;
cnt=Len;
}
Vector(const std::initializer_list<vt>& other){
int Len=other.size();
data=new vt[Len];
copy(other.begin(),other.end(),data);
len=Len;
ed=Len-1;
cnt=Len;
}
~Vector(){
delete[] data;
len=0;
cnt=0;
ed=0;
}
void Push_back(const vt other){
if(len==cnt){
vt* type=new vt[cnt];
srccpy(type,data,len);
delete[] data;
cnt++;
data=new vt[cnt];
srccpy(data,type,len);
delete[] type;
}
data[len]=other;
ed=len;
len++;
}
void Pop_back(){
len=ed;
ed--;
}
private:
STRing numtostring(int x){
STRing s="";
while(x){
s=(char)(x%10+'0')+s;
x/=10;
}
return s;
}
public:
Vector& operator=(const Vector& other){
delete[] data;
int len2=other.len;
data=new vt[len2];
srccpy(data,other.data,other.len);
ed=other.ed;
cnt=len2;
return *this;
}
vt& operator[](int index){
return data[index];
}
vt& Front(){
return data[0];
}
vt& Back(){
return data[ed];
}
vt& At(int index){
if(index>=len){
throw std::out_of_range("[Index needs to be smaller than range] Index out of range.(range="+numtostring(len)+")");
return NULL;
}
return data[index];
}
bool Empty(){
return len==0;
}
int Size()const{
return len;
}
int Length()const{
return len;
}
Iterator Begin(){
Iterator it=data;
return it;
}
Iterator End(){
Iterator it=data+ed;
return it;
}
void Clear(){
delete[] data;
len=0;
cnt=0;
ed=0;
}
Iterator Erase(Iterator it){
Vector<vt> result;
int pos=it-data;
for(int i=0;i<pos;i++){
result.Push_back(data[i]);
}
for(int i=pos+1;i<len;i++){
result.Push_back(data[i]);
}
delete[] data;
data=new vt[result.len];
srccpy(data,result.data,result.len);
len=result.len;
cnt=result.cnt;
ed=result.ed;
Iterator New(data+pos);
return New;
}
Iterator Erase(Iterator start,Iterator end){
Vector<vt> result;
int pos=start-data;
int Len=end-start;
for(int i=0;i<pos;i++){
result.Push_back(data[i]);
}
for(int i=pos+Len;i<len;i++){
result.Push_back(data[i]);
}
delete[] data;
data=new vt[result.len];
srccpy(data,result.data,result.len);
len=result.len;
cnt=result.cnt;
ed=result.ed;
Iterator New(data+pos);
return New;
}
Iterator Insert(Iterator it,vt other){
Vector<vt> result;
int pos=it-data;
for(int i=0;i<pos;i++){
result.Push_back(data[i]);
}
result.Push_back(other);
for(int i=pos;i<len;i++){
result.Push_back(data[i]);
}
delete[] data;
data=new vt[result.len];
srccpy(data,result.data,result.len);
len=result.len;
cnt=result.cnt;
ed=result.ed;
Iterator New(data+pos+1);
return New;
}
void Insert(Iterator it,int Cnt,vt other){
Vector<vt> result;
int pos=it-data;
for(int i=0;i<pos;i++){
result.Push_back(data[i]);
}
for(int i=1;i<=Cnt;i++){
result.Push_back(other);
}
for(int i=pos;i<len;i++){
result.Push_back(data[i]);
}
delete[] data;
data=new vt[result.len];
srccpy(data,result.data,result.len);
len=result.len;
cnt=result.cnt;
ed=result.ed;
return ;
}
void Insert(Iterator it,Iterator start,Iterator end){
Vector<vt> result;
int pos=it-data;
for(int i=0;i<pos;i++){
result.Push_back(data[i]);
}
for(Iterator i=start;i!=end;i++){
result.Push_back(*i);
}
for(int i=pos;i<len;i++){
result.Push_back(data[i]);
}
delete[] data;
data=new vt[result.len];
srccpy(data,result.data,result.len);
len=result.len;
cnt=result.cnt;
ed=result.ed;
return ;
}
Iterator Find(Iterator start,Iterator end,vt other){
int l=start-data,r=end-data;
Iterator it;
for(int i=l;i<r;i++){
it=(data+i);
if(data[i]==other)return it;
}
return end;
}
};
}
namespace STD{
using namespace STRING;
using namespace VECTOR;
}
using namespace STD;
void show_string(){
STRing a("abc");
STRing b="def";
a+=b;
cout<<a<<endl;
cout<<a.Substr(0,3)<<endl;
a.Erase(0,3);
cout<<a<<endl;
a.Insert(0,"123");
cout<<a<<endl;
a.Replace(3,3,"456");
cout<<a<<endl;
STRing c="123456";
cout<<(a==c)<<endl;
cout<<a.Find("123")<<endl;
cout<<a.Find("123",1)<<endl;
c[0]='a';
cout<<c<<endl;
}
void show_vector(){
Vector<int> v={1,2,3,4};
v.Push_back(6);
v.Push_back(7);
v.Push_back(6);
v.Pop_back();
Vector<int>::Iterator it=v.Begin()+4;
v.Erase(it);
for(int i=0;i<v.Size();i++){
cout<<v[i]<<endl;
}
}
void show(){
show_string();
show_vector();
}
int main(){
show();
return 0;
}
董安雅在2023-04-04 20:33:52追加了内容
有什么问题可以说,
编译器为DEV_C++6.7.5版,编译器配置为。(使用DEV_C++5.11版GCC编译器可能会报编译错误)
也可以使用在线编译器(推荐Coliru,是C++标准推荐的在线编译器)。
董安雅在2023-05-21 11:17:37追加了内容
暂停更新!
0
0
0
0
0
0
0
0