Stringの深いコピー、比較及び削除・変更などの操作
stringクラスはc++デフォルトで提供されていますが、stringクラスの書き方を知ることも重要です.面接にはstringクラスに関する問題がたくさんあります.stringクラスはc++クラスと対象の掌握度を考察することができ、ある程度面接者のプログラミング能力も考察しました.
以下はstringクラスのいくつかの基本的な機能で、面接の時間が短いため、このごく短い時間でstringクラスの機能をすべて実現することは不可能で、私たちは最も基本的な機能を実現することができて、クラスの中の構造関数、構造関数、賦値、複製などの機能を実現することができます.
テスト例は次のとおりです.
以上はstring類の部分実現について、不足があれば、よろしくお願いします.
以下はstringクラスのいくつかの基本的な機能で、面接の時間が短いため、このごく短い時間でstringクラスの機能をすべて実現することは不可能で、私たちは最も基本的な機能を実現することができて、クラスの中の構造関数、構造関数、賦値、複製などの機能を実現することができます.
#include<iostream>
using namespace std;
#include<assert.h>
class String
{
public:
//
String(char* str = "")// string s; ""
:_str(new char[strlen(str) + 1])//+1,
{
strcpy(_str, str);
_size = strlen(str);
_capacity = _size;
}
String(const String& s);
String& operator=(String& s);
~String();
void Display();
public:
//
char& operator[](size_t index);//
bool operator==(const String& s);//
int compare(const String& s);//
void CheckCapacity(size_t size);//
void PushBack(char ch);//
void PopBack();//
void Insert(size_t pos, const char ch);//
void InsertS(size_t pos, const char* ch,size_t len);// len
void Erase(size_t pos,size_t len);// pos len
int Find(const char ch);// ,
int FindS(const char* ch);//
private:
char* _str;
size_t _size;
size_t _capacity;
};
//
String::String(const String& s)
:_str(NULL)//
{
char* tmp = s._str;
swap(_str, tmp);
_size = s._size;
_capacity = s._size;
}
String& String::operator = (String& s)
{
if (_str != s._str)
{
swap(_str, s._str);
}
return *this;
}
String::~String()//
{
if (_str)
{
delete[] _str;
_str = NULL;
_size = 0;
_capacity = 0;
}
}
char& String::operator[](size_t index)//
{
return _str[index];
}
bool String::operator==(const String& s)//
{// char* cur tmp, ,
char* cur = _str;
char* tmp = s._str;
while (cur && tmp)
{
if (*cur != *tmp)
{
return 0;
}
else
{
++cur;
++tmp;
if (*cur == NULL && *tmp == NULL)
return 1;
}
}
return 0;
}
int String::compare(const String& s)//
{
char* cur = _str;
char* tmp = s._str;
while (cur && tmp)
{
if (*cur != *tmp)
{
return *cur - *tmp;
}
else
{
++cur;
++tmp;
if (*cur == NULL && *tmp == NULL)
return 0;
}
}
if (cur)
return *cur;
else
return -(*tmp);
}
void String::Display()
{
if (_size == 0)
cout << "NULL" ;
cout << _str << endl;
}
void String::CheckCapacity(size_t size)//
{
if (size+1 >= _capacity)//size+1 '\0'
{
_capacity = size > _capacity * 2 ? size : _capacity * 2;
_str = (char *)realloc(_str, sizeof(char)*_capacity);
}
}
void String::PushBack(char ch)//
{
CheckCapacity(_size + 2);
_str[_size++] = ch;
_str[_size] = '\0';
}
void String::PopBack()//
{
if (_size)
{
_str[_size - 1] = '\0';
--_size;
}
}
void String::Insert(size_t pos, const char ch)//
{
assert(pos);
CheckCapacity(_size + 1);
_size++;
int count = _size;
while (count != pos - 1)
{
_str[count] = _str[count - 1];
count--;
}
_str[pos-1]=ch;
}
void String::InsertS(size_t pos, const char* ch, size_t len)// len
{
assert(pos);
CheckCapacity(_size + len);
_size = _size + len;
while (len)
{
Insert(pos,*ch);
pos++;
ch++;
len--;
}
}
void String::Erase(size_t pos, size_t len)// pos len
{
assert(pos);
assert(len);
while (_str[len + pos - 1])
{
_str[pos - 1] = _str[len + pos - 1];
_str[len + pos - 1] = NULL;// ,
pos++;
}
_size -= len;
}
int String::Find(const char ch)//
{
int pos = 1;
char* str = _str;
while (str)
{
if (*str == ch)
{
return pos;
}
pos++;
str++;
}
return 0;
}
int String::FindS(const char* ch)// ,
{
char* str= _str;
char* s2 = (char*)ch;
int pos = 1;
while (str)
{
char* s1 = str;
while (s1 && s2 && *s1==*s2)
{
s1++;
s2++;
}
if (*s2==NULL)// , s2
return pos;
str++;
pos++;
}
return 0;
}
テスト例は次のとおりです.
void Test1()
{// 、 、
//String s;
//String s1("ssssss");
//String s2(s1);
//String s3 = s1;
//s.Display();
//s1.Display();
//s2.Display();
//s3.Display();
String S1("hello fo");
String S2("hello foc");
//String S2("hworld!");
//S1.Display();
//S1[6] = 't';
S1.Display();
bool b= S1 == S2 ;
cout << b << endl;
cout << S1.compare(S2) << endl;
}
void Test2()
{//
String S1;
S1.PushBack('h');
S1.PushBack('e');
S1.PushBack('l');
S1.PushBack('l');
S1.PushBack('o');
S1.Display();
//S1.PopBack();
//S1.PopBack();
//S1.PopBack();
//S1.Display();
//S1.PopBack();
//S1.PopBack();
//S1.PopBack();
//S1.Display();
String S2("hello orld!");
//S2.Insert(7,'w');
//S2.Display();
//S2.InsertS(6, " to try!", 3);
//S2.Display();
//S2.Erase(6, 3);
//S2.Display();
//cout << "d->" << S2.Find('d') << endl;
cout << "ello->" << S2.FindS("ello ") << endl;
}
以上はstring類の部分実現について、不足があれば、よろしくお願いします.