在本文中,我们将创建我们的自定义字符串类,该类将具有与现有字符串类相同的功能。
字符串类具有以下基本功能:
- 不带参数的构造方法:这将为堆中的字符串对象分配存储,并将值分配为NULL字符。
- 仅带有一个参数的构造方法:它接受一个指向字符的指针,或者我们可以说如果我们传递一个字符数组,接受一个指向数组中第一个字符的指针,则String类的构造函数将分配存储在堆内存中。与传递的数组大小相同,并将数组的内容复制到堆中已分配的内存中。它使用cstring库中声明的strcpy()函数复制内容。
在执行上述操作之前,它会检查传递的参数是否为NULL指针,然后将其视为没有参数的构造函数。 - 复制构造函数:当从已创建的对象创建的任何具有相同类型的对象都执行深度复制时,将调用此方法。它在堆上为要创建的对象分配新空间,并复制传递的对象(作为引用传递)的内容。
- 移动构造函数:通常是在从同一类型的右值初始化对象(通过直接初始化或复制初始化)时调用的。它接受对自定义字符串类类型的对象的右值的引用。
以下是使用自定义字符串类Mystring的上述方法的实现:
// C++ program to illustrate the
// above discussed functionality
#include
#include
using namespace std;
// Custom string class
class Mystring {
// Initialise the char array
char* str;
public:
// No arguments Constructor
Mystring();
// Constructor with 1 arguments
Mystring(char* val);
// Copy Constructor
Mystring(const Mystring& source);
// Move Constructor
Mystring(Mystring&& source);
// Destructor
~Mystring() { delete str; }
};
// Function to illustrate Constructor
// with no arguments
Mystring::Mystring()
: str{ nullptr }
{
str = new char[1];
str[0] = '\0';
}
// Function to illustrate Constructor
// with one arguments
Mystring::Mystring(char* val)
{
if (val == nullptr) {
str = new char[1];
str[0] = '\0';
}
else {
str = new char[strlen(val) + 1];
// Copy character of val[]
// using strcpy
strcpy(str, val);
cout << "The string passed is: "
<< str << endl;
}
}
// Function to illustrate
// Copy Constructor
Mystring::Mystring(const Mystring& source)
{
str = new char[strlen(source.str) + 1];
strcpy(str, source.str);
}
// Function to illustrate
// Move Constructor
Mystring::Mystring(Mystring&& source)
{
str = source.str;
source.str = nullptr;
}
// Driver Code
int main()
{
// Constructor with no arguments
Mystring a;
// Convert string literal to
// char array
char temp[] = "Hello";
// Constructor with one argument
Mystring b{ temp };
// Copy constructor
Mystring c{ a };
char temp1[] = "World";
// One arg constructor called,
// then the move constructor
Mystring d{ Mystring{ temp } };
return 0;
}
输出:
The string passed is: Hello
The string passed is: Hello
字符串类的更多功能:
- pop_back():从Mystring对象中删除最后一个元素。
- push_back(char ch):接受字符作为参数,并将其添加到Mystring对象的末尾。
- length():返回mystring的长度。
- copy():它将mystring对象从给定位置(pos)和特定长度(len)复制到字符数组。
- swap():交换两个Mystring对象。
- 连接两个字符串使用重载“+”运算符:可以让我们连接两个字符串。
下面是说明上述功能的程序:
// C++ program to illustrate the
// above functionalities
#include
#include
using namespace std;
// Class Mystring
class Mystring {
// Prototype for stream insertion
friend ostream&
operator<<(
ostream& os,
const Mystring& obj);
// Prototype for stream extraction
friend istream& operator>>(
istream& is,
Mystring& obj);
// Prototype for '+'
// operator overloading
friend Mystring operator+(
const Mystring& lhs,
const Mystring& rhs);
char* str;
public:
// No arguments constructor
Mystring();
// pop_back() function
void pop_bk();
// push_back() function
void push_bk(char a);
// To get the length
int get_length();
// Function to copy the string
// of length len from position pos
void cpy(char s[], int len, int pos);
// Swap strings function
void swp(Mystring& rhs);
// Constructor with 1 arguments
Mystring(char* val);
// Copy Constructor
Mystring(const Mystring& source);
// Move Constructor
Mystring(Mystring&& source);
// Overloading the assignment
// operator
Mystring& operator=(
const Mystring& rhs);
// Destructor
~Mystring() { delete str; }
};
// Overloading the assignment operator
Mystring& Mystring::operator=(
const Mystring& rhs)
{
if (this == &rhs)
return *this;
delete[] str;
str = new char[strlen(rhs.str) + 1];
strcpy(str, rhs.str);
return *this;
}
// Overloading the plus operator
Mystring operator+(const Mystring& lhs,
const Mystring& rhs)
{
int length = strlen(lhs.str)
+ strlen(rhs.str);
char* buff = new char[length + 1];
// Copy the strings to buff[]
strcpy(buff, lhs.str);
strcat(buff, rhs.str);
// String temp
Mystring temp{ buff };
// delete the buff[]
delete[] buff;
// Return the concatenated string
return temp;
}
// Overloading the stream
// extraction operator
istream& operator>>(istream& is,
Mystring& obj)
{
char* buff = new char[1000];
is >> buff;
obj = Mystring{ buff };
delete[] buff;
return is;
}
// Overloading the stream
// insertion operator
ostream& operator<<(ostream& os,
const Mystring& obj)
{
os << obj.str;
return os;
}
// Function for swapping string
void Mystring::swp(Mystring& rhs)
{
Mystring temp{ rhs };
rhs = *this;
*this = temp;
}
// Function to copy the string
void Mystring::cpy(char s[], int len,
int pos)
{
for (int i = 0; i < len; i++) {
s[i] = str[pos + i];
}
s[len] = '\0';
}
// Function to implement push_bk
void Mystring::push_bk(char a)
{
// Find length of string
int length = strlen(str);
char* buff = new char[length + 2];
// Copy character from str
// to buff[]
for (int i = 0; i < length; i++) {
buff[i] = str[i];
}
buff[length] = a;
buff[length + 1] = '\0';
// Assign the new string with
// char a to string str
*this = Mystring{ buff };
// Delete the temp buff[]
delete[] buff;
}
// Function to implement pop_bk
void Mystring::pop_bk()
{
int length = strlen(str);
char* buff = new char[length];
// Copy character from str
// to buff[]
for (int i = 0; i < length - 1; i++)
buff[i] = str[i];
buff[length] = '\0';
// Assign the new string with
// char a to string str
*this = Mystring{ buff };
// delete the buff[]
delete[] buff;
}
// Function to implement get_length
int Mystring::get_length()
{
return strlen(str);
}
// Function to illustrate Constructor
// with no arguments
Mystring::Mystring()
: str{ nullptr }
{
str = new char[1];
str[0] = '\0';
}
// Function to illustrate Constructor
// with one arguments
Mystring::Mystring(char* val)
{
if (val == nullptr) {
str = new char[1];
str[0] = '\0';
}
else {
str = new char[strlen(val) + 1];
// Copy character of val[]
// using strcpy
strcpy(str, val);
}
}
// Function to illustrate
// Copy Constructor
Mystring::Mystring(const Mystring& source)
{
str = new char[strlen(source.str) + 1];
strcpy(str, source.str);
}
// Function to illustrate
// Move Constructor
Mystring::Mystring(Mystring&& source)
{
str = source.str;
source.str = nullptr;
}
// Driver Code
int main()
{
// Constructor with no arguments
Mystring a;
// Convert string literal to
// char array
char temp[] = "Hello";
// Constructor with one argument
Mystring b{ temp };
// Copy constructor
Mystring c{ a };
char temp1[] = "World";
// One arg constructor called,
// then the move constructor
Mystring d{ Mystring{ temp } };
// Remove last character from
// Mystring b
b.pop_bk();
// Print string b
cout << "Mystring b: "
<< b << endl;
// Append last character from
// Mystring b
b.push_bk('o');
// Print string b
cout << "Mystring b: "
<< b << endl;
// Print length of string b
cout << "Length of Mystring b: "
<< b << endl;
char arr[80];
// Copy string b chars from
// length 0 to 3
b.cpy(arr, 3, 0);
// Print string arr
cout << "arr is: "
<< arr << endl;
// Swap d and b
d.swp(b);
// Print d and b
cout << d << " "
<< b << endl;
// Concatenate b and b with
// overloading '+' operator
d = b + b;
// Print string d
cout << "string d: "
<< d << endl;
return 0;
}
输出:
Mystring b: Hell
Mystring b: Hello
Length of Mystring b: Hello
arr is: Hel
Hello Hello
string d: HelloHello
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。