c++ string 简易版
#include<iostream>
using namespace std
;
int strlen(const char *p
){
int size
= 0;
while(*p
++){++size
;}
return size
;
}
char* strcat(char a
[], const char b
[])
{
char* buff1
= a
;
const char* buff2
= b
;
while(*buff1
){
buff1
++;
}
while(*buff2
){
*buff1
++ = *buff2
++;
}
buff1
[strlen(buff2
)] = '\0';
return a
;
}
inline void strcpy(char *buff1
, const char *buff2
){
while(*buff2
){
*buff1
++ = *buff2
++;
}
buff1
[strlen(buff2
)] = '\0';
}
int strfind(const char* a
, const char b
){
int size
= 0;
const char *p
= a
;
while(*p
++){
++size
;
if(*p
== b
)return size
;
}
return size
;
}
bool strequal(const char* a
, const char b
[]){
if(strlen(a
) != strlen(b
)){
return false;
}
int size
= strlen(a
);
for(int i
=0;i
<size
+1;i
++){
if(a
[i
] != b
[i
]){
return false;
}
}
return true;
}
class String {
inline char* c_str() const {
return str
;
}
public:
inline int len()const{
return strlen(this->str
);
}
explicit String(const char* p
= nullptr) {
if (nullptr != p
) {
str
= new char[strlen(p
) + 1];
strcpy(str
, p
);
}
else {
str
= new char[1];
*str
= '\0';
}
}
inline int find(const char a
){
return strfind(this->str
, a
);
}
String(const String
& a
);
inline ~String() {
delete[] str
;
}
bool operator == ( const char b
[]){
return strequal(this->str
, b
);
}
bool operator == (const String
&a
){
return strequal(this->str
, a
.str
);
}
String
&operator = (const char a
[]) {
delete[] str
;
str
= new char[strlen(a
) + 1];
strcpy(str
, a
);
return *this;
}
String
& operator += (const String
& a
){
String
f(*this), f2(a
);
delete this->str
;
this->str
= new char[strlen(f
.str
)+strlen(f2
.str
)-1];
strcpy(this->str
, f
.str
);
strcpy(this->str
+strlen(f
.str
), f2
.str
);
return *this;
}
String
& operator += (const char a
[]){
String
f(*this);
delete this->str
;
this->str
= new char[strlen(f
.str
)+strlen(a
)-1];
strcpy(this->str
, f
.str
);
strcpy(this->str
+strlen(f
.str
), a
);
return *this;
}
String
operator + (const String
& a
){
String f
;
f
.str
= new char[strlen(f
.str
)+strlen(a
.str
)-1];
strcpy(f
.str
, this->str
);
strcpy(f
.str
+strlen(this->str
), a
.str
);
return f
;
}
String
operator + (const char a
[]){
String f
;
f
.str
= new char[strlen(this->str
)+strlen(a
)-1];
strcpy(f
.str
, this->str
);
strcpy(f
.str
+strlen(this->str
), a
);
return f
;
}
inline char operator[] (int size
){
if(strlen(this->str
) < size
) {return '\0';}
return str
[size
];
}
String
& operator = (const String
& a
);
inline friend ostream
& operator<<(ostream
& os
, const String
& a
) {
return os
<< a
.c_str();
}
inline friend istream
& operator >> (istream
&in
, String
&a
){
char* snew
= new char;
*snew
= '\0';
in
>>snew
;
a
= snew
;
*snew
= '\0';
delete snew
;
return in
;
}
String
&operator ()(const String
&a
);
inline int getline(){
size_t size
= strlen(this->str
);
scanf("%[^\n]", this->str
);
String
b(this->str
);
this->str
[size
] = '\0';
*this = b
;
return b
.len();
}
private:
char* str
;
};
String
& String
::operator ()(const String
&a
){
if(&a
== this){
return *this;
}
delete[] this->str
;
this->str
= new char[this->len()+1];
strcpy(this->str
, a
.str
);
return *this;
}
String
& String
::operator=(const String
& a
) {
if (&a
== this) {
return *this;
}
delete[] this->str
;
str
= new char[strlen(a
.str
) + 1];
strcpy(this->str
, a
.str
);
return *this;
}
inline String
::String(const String
& a
) {
str
= new char[strlen(a
.str
) + 1];
strcpy(str
, a
.str
);
}
实现了基本的加, 判断, 输入等.
感谢您对此代码的指点 !
转载请注明原文地址:https://blackberry.8miu.com/read-3098.html