实现string类

1 char字符串以'/0'结尾,但是strlen()时它不占大小,cout时它也不会输出来

 

2 new出来的char[]大小一定要满足操作,否则操作越界,delete时出错

  new出来的指针移动了,delete也会出错

 

3 模板函数的声明和定义要放在同一个文件里,默认放在头文件

strings.h:

 

#pragma once
#include<string>

class strings
{
public:
	strings();
	strings(const char* s);
	strings(const strings & other);  //拷贝构造函数
	strings(int n, char c);			 //常见一个包含n个字符为c的对象
	strings(const char* s, int n);   //将字符串初始化为s的前n个字符,即使已经到了串尾
	template<class T>
	strings(const T ptr1, const T ptr2);//用一个数组内的两个指针构造对象
	int size();
	strings& operator=(const strings& other);
	strings operator+(const strings& other);
	bool operator==(const strings& other);
	char operator[] (int index);
	friend std::ostream& operator<<(std::ostream& out, const strings& str);
	virtual ~strings();
private:
	char* date;
	int datelength;
};

template<class T>
strings::strings(const T ptr1, const T ptr2)
{
	T temp = ptr1;
	int size = sizeof(*ptr1);
	datelength = strlen(ptr1) - strlen(ptr2);
	date = new char[datelength + 1];
	int i = 0;
	for (temp; temp<ptr2; temp += size)
	{
		date[i++] = *temp;
	}
	date[datelength] = '\0';
}

 

 

strings.cpp

 

#include "stdafx.h"
#include "strings.h"
#include<string>
#include<iostream>
strings::strings() //默认构造空字符串,'\0'不占长度
{
	datelength = 0;
	date = new char[1];
	*date = '\0';
}

strings::strings(const char* s)
{
	if (!s)	//如果s为nullptr,则会为其初始化为空字符串,即以'\0'结尾
	{
		datelength = 0;
		date = new char[1];
		*date = '\0';
	}
	else
	{
		datelength = strlen(s);
		date = new char[datelength+1];//最后空间放'\0'
		strcpy(date, s);
	}
}

strings::strings(const strings & other) //other字符串已经被初始化,因此不可能为nullptr,不需要判断为空
{
	datelength = other.datelength;
	date = new char[datelength + 1];
	strcpy(date, other.date);
}

strings::strings(int n, char c)
{
	datelength = n;
	date = new char[datelength + 1];
	for (int i = 0; i < datelength; i++)
		date[i] = c;
	date[datelength] = '\0'; //不能是date[datelength+1],否则越界,析构时出错
}

strings::strings(const char* s, int n)
{
	datelength = n;
	date = new char[datelength + 1];
	strncpy(date, s, n);
	date[datelength] = '\0';
}

strings& strings::operator=(const strings& other)
{
	if (this->date == other.date)
		return *this;
	delete[] date;
	datelength = other.datelength;
	date = new char[datelength + 1];
	strcpy(date, other.date);
	return *this;
}

strings strings::operator+(const strings& other)
{
	strings temp;
	datelength = this->datelength + other.datelength;
	temp.datelength = datelength; //大小别忘了更新,否则后面赋值的时候越界
	temp.date = new char[temp.datelength + 1];
	strcpy(temp.date, date);
	strcat(temp.date, other.date);//要保证目标字符串由足够空间
	return temp; 		//这里要注意,它返回的是临时对象,因此函数返回值不能为引用
}

bool strings::operator==(const strings& other)
{
	if (this->datelength != other.datelength)
		return false;
	else
		return strcmp(this->date, other.date) ? false : true;
}

char strings::operator[] (int index)
{
	if (index >= 0 && index < this->datelength)
		return this->date[index];
}

std::ostream& operator<<(std::ostream& out, const strings&  str)
{
	out << str.date;
	return out;
}



strings::~strings()
{
	if (this->date)
	{
		delete []  this->date;
		this->date= nullptr;
	}
}


main函数:

 

 

// String.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"strings.h"
#include<iostream>
#include<cstdlib>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	strings a;
	cout << "默认构造函数: " << a << endl;

	strings b("zhang");
	cout << "字符串构造: " << b << endl;

	strings c(b);
	cout << "拷贝构造函数: " << c << endl;

	strings d(5, '6');
	cout << "构造由5个‘6’组成的对象: " << d << endl;

	char *str= "123456";
	strings e(str, 3);
	cout << "用字符串的前3个字符构造对象: " << e<< endl;
	
	a = b;
	cout << "赋值号重载:" << a << endl;
	
	bool result = (a == b);
	cout << "等于号重载:" << result << endl;
	
	a = a + b;
	cout << "加号重载:" << a << endl;
	
	cout << "大小:"<<a.size() << endl;

	char* ss = "123456";
	strings bb(&ss[0], &ss[3]);
	cout << "用字符串的两个指针构造对象:" << bb << endl;
	
	system("pause");
	return 0;
}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的string是一个用于处理字符串的标准库。它提供了一系列成员函数和操作符重载,使得字符串的操作更加方便和高效。 C++中的string位于命名空间std中,因此在使用之前需要包含头文件< string >。 下面是一个简单的示例,展示了如何使用string来创建、初始化和操作字符串: ```cpp #include <iostream> #include <string> int main() { // 创建一个空字符串 std::string str; // 初始化字符串 std::string greeting = "Hello, world!"; // 获取字符串长度 int length = greeting.length(); std::cout << "Length: " << length << std::endl; // 连接字符串 std::string name = "Alice"; std::string message = greeting + " My name is " + name; std::cout << "Message: " << message << std::endl; // 获取子串 std::string substring = message.substr(7, 5); std::cout << "Substring: " << substring << std::endl; // 查找子串 size_t position = message.find("world"); if (position != std::string::npos) { std::cout << "Found at position: " << position << std::endl; } else { std::cout << "Not found" << std::endl; } return 0; } ``` 上述示例中,我们首先创建了一个空字符串`str`,然后使用赋值运算符将字符串"Hello, world!"赋给了变量`greeting`。接着,我们使用`length()`函数获取了字符串的长度,并使用`+`运算符将多个字符串连接起来形成新的字符串。我们还使用`substr()`函数获取了字符串的子串,并使用`find()`函数查找了子串在原字符串中的位置。 除了上述示例中的操作,string还提供了许多其他有用的成员函数和操作符重载,如插入、删除、替换、比较等。你可以参考C++的官方文档或其他相关资料来了解更多关于string的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值