首页 > 编程语言 >【C++】string类中常用函数的模拟实现

【C++】string类中常用函数的模拟实现

时间:2024-09-15 15:25:17浏览次数:3  
标签:const string pos C++ str end 类中 size

【C++】string类中常用函数的模拟实现

1. string.h

#include<assert.h>

namespace wch
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}
		//若使用初始化列表初始化要注意:初始化顺序为声明顺序
		//构造函数无参对象初始化 string str;
		string(const char* str = "")//string(const char* str = "\0")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		string(const string& s)//构造函数有参对象初始化 string str("abc");
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);

			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);

			return _str[pos];
		}

		void reserve(size_t n)
		{
		//要加判断条件,因为当n <= _capacity时不做任何处理
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				// 2倍扩容
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			_str[_size] = ch;

			++_size;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				// 至少扩容到_size + len
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}

		string& operator+=(char ch)//末尾添加字符
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* str)//末尾添加字符串
		{
			append(str);
			return *this;
		}

		void insert(size_t pos, size_t n, char ch)//从下标pos位置开始向后插入n个字符ch
		{
			assert(pos <= _size);

			if (_size + n > _capacity)
			{
				// 至少扩容到_size + n
				reserve(_size + n);
			}

			// 挪动数据
			/*int end = _size;
			while (end >= (int)pos)//整型提升有符号向无符号提升
			{
				_str[end + n] = _str[end];
				--end;
			}*/

			// 挪动数据
			size_t end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + n] = _str[end];
				--end;
			}

			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}

			_size += n;
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);

			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				// 至少扩容到_size + len
				reserve(_size + len);
			}

			size_t end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + len] = _str[end];
				--end;
			}

			for (size_t i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}

			_size += len;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos <= _size);

			if (len == npos || pos + len >= _size)//将pos后面的数据删完
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else//删除pos到结尾的部分连续数据
			{
				size_t end = pos + len;
				while (end <= _size)
				{
					_str[pos++] = _str[end++];
				}
				_size -= len;
			}
		}

		size_t find(char ch, size_t pos = 0)//找字符下标
		{
			assert(pos < _size);

			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}

			return npos;
		}

		size_t find(const char* str, size_t pos = 0)//找字符串下标
		{
			assert(pos < _size);

			const char* ptr = strstr(_str + pos, str);
			if (ptr)
			{
				return ptr - _str;
			}
			else
			{
				return npos;
			}
		}

		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);

			size_t n = len;
			if (len == npos || pos + len > _size)
			{
				n = _size - pos;
			}

			string tmp;
			tmp.reserve(n);
			for (size_t i = pos; i < pos + n; i++)
			{
				tmp += _str[i];
			}

			return tmp;
		}

	private:
		size_t _size;
		size_t _capacity;
		char* _str;

	public:
		//const static size_t npos = -1; // 虽然可以这样用,但是不建议
		const static size_t npos;

		//const static double x = 1.1;//不支持除了int以外其他const static类型在类内定义并初始化
	};

	const size_t string::npos = -1;
	//const double string::x = 1.1;
};

2. Text.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

#include"string.h"

void test_string1()
{
	wch::string s1("hello world");
	cout << s1.c_str() << endl;

	wch::string s2;
	cout << s2.c_str() << endl;

	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}

	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;

	const wch::string s3("hello world");
	s3[0];

	//wch::string::const_iterator cit = s3.begin();
	auto cit = s3.begin();
	while (cit != s3.end())
	{
		//*cit += 1;

		cout << *cit << " ";
		++cit;
	}
	cout << endl;

	wch::string::iterator it = s1.begin();
	while (it != s1.end())
	{
		*it += 1;

		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto ch : s1)
	{
		cout << ch << " ";
	}
	cout << endl;
}

void test_string2()
{

	wch::string s1("hello world");
	cout << s1.c_str() << endl;

	s1.push_back(' ');
	s1.push_back('#');
	s1.append("hello world");
	cout << s1.c_str() << endl;

	wch::string s2("hello world");
	cout << s2.c_str() << endl;

	s2 += ' ';
	s2 += '#';
	s2 += "hello world";
	cout << s2.c_str() << endl;
}

void test_string3()
{
	wch::string s1("helloworld");
	cout << s1.c_str() << endl;

	s1.insert(5, 3, '#');
	cout << s1.c_str() << endl;

	s1.insert(0, 3, '#');
	cout << s1.c_str() << endl;


	wch::string s2("helloworld");
	s2.insert(5, "%%%%%");
	cout << s2.c_str() << endl;
}

void test_string4()
{
	wch::string s1("helloworld");
	cout << s1.c_str() << endl;

	s1.erase(5, 3);
	cout << s1.c_str() << endl;

	s1.erase(5, 30);
	cout << s1.c_str() << endl;

	s1.erase(2);
	cout << s1.c_str() << endl;
}

void test_string5()
{
	// 2118
	wch::string url = "ftp://www.baidu.com/?tn=65081411_1_oem_dg";

	size_t pos1 = url.find("://");
	cout << pos1 << endl;
	if (pos1 != wch::string::npos)
	{
		wch::string protocol = url.substr(0, pos1);
		cout << protocol.c_str() << endl;
	}

	size_t pos2 = url.find('/', pos1 + 3);
	cout << pos2 << endl;
	if (pos2 != wch::string::npos)
	{
		wch::string domain = url.substr(pos1 + 3, pos2 - (pos1 + 3));
		wch::string uri = url.substr(pos2 + 1);

		cout << domain.c_str() << endl;
		cout << uri.c_str() << endl;
	}
}

int main()
{
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string4();
	test_string5();
	return 0;
}

标签:const,string,pos,C++,str,end,类中,size
From: https://blog.csdn.net/2303_80737493/article/details/142282676

相关文章

  • C++string容器
    string容器基本概念本质:string是C++风格的字符串,而string本质上是一个类string和char*区别:char*是一个指针string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器特点:string类内部封装了很多成员方法例如:查找find,拷贝copy,删除delete替换replac......
  • C++: 二叉树进阶面试题
    做每件事之前都心存诚意,就会事半功倍.目录前言1.根据二叉树创建字符串2.二叉树的层序遍历Ⅰ3.二叉树的层序遍历Ⅱ4.二叉树的最近公共祖先5.二叉搜索树与双向链表6.根据一棵树的前序遍历与中序遍历构造二叉树7.根据一棵树的中序遍历与后序遍历构造二叉树8.二......
  • windows C++ 并行编程-并行容器和对象
    并行模式库(PPL)包括几个容器和对象,这些容器和对象提供对其元素的线程安全访问。并发容器提供对最重要操作的并发安全访问。在这里,并发安全意味着指针或迭代器始终有效。它不保证元素初始化或特定的遍历顺序。这些容器的功能与C++标准库提供的功能类似。例如,concurren......
  • windows C++-并行编程-PPL任务并行(一)
    在并发运行时中,任务是执行特定作业并通常与其他任务并行运行的工作单元。任务可以分解为组织成任务组的其他更细化的任务。编写异步代码,并希望在异步操作完成之后进行某种操作时,可使用任务。例如,可以使用一个任务以异步方式从文件读取,然后使用另一个任务(延续任务,本文档稍后......
  • c++修炼之路之AVL树与红黑树
    目录一:AVL树1.AVL树的概念2.AVL树插入数据后平衡因子及更新的情况3.AVL树节点的定义 4.AVL树的插入及旋转 二:红黑树 1.红黑树的概念及性质2.红黑树节点的定义3.红黑树的插入操作情况 4.红黑树与AVL树的比较 接下来的日子会顺顺利利,万事胜意,生活明朗---------......
  • 南沙C++信奥老师解一本通题: 1161:转进制
    ​ 题目描述】用递归算法将一个十进制数X转换成任意进制数M(M≤16)。【输入】一行两个数,第一个十进制数X,第二个为进制M。【输出】输出结果。【输入样例】3116{将十进制31转化为十六进制数}【输出样例】1F#include<iostream>usingnamespacestd;intx,m;void......
  • C++ 定义静态成员 static 关键字不能在定义出重复出现
    定义静态成员和其他的成员函数一样,我们既可以在类的内部也可以在类的外部定义静态成员函数。当在类的外部定义静态成员时,不能重复static关键字,该关键字只出现在类内部的声明语句:voidAccount::rate(doublenewRate){interestRate=newRate;}Note:和类的所有成员一样,当我......
  • C++ 3/5 法则相关
    拷贝构造函数拷贝构造函数的第一个参数必须是一个引用类型。虽然我们可以定义一个接受非const引用的拷贝构造函数,但此参数几乎总是一个const的引用。拷贝构造函数在几种情况下都会被隐式地使用。因此,拷贝构造函数通常不应该是explicit的(参见7.5.4节,第265页)。一般情况,......
  • C++ Primer Plus 第六版中文版(上)
    参考资料:《C++PrimerPlus第六版中文版》笔记作者:Mr.Crocodile欢迎转载文章目录开始学习C++头文件命名规定名称空间`cout`、`cin`函数处理数据简单变量变量命名规则整型运算符`sizeof`和头文件climitsclimits中的符号常量变量初始化整型字面量整型字面量后缀char......
  • C++编译器的那些事
    接上文OK!Rightnow!  Let's go!C++编译器是如何工作的?C++编译器实际负责什么?我们把C++代码写成文本。就是这样,他只是一个文本文件,然后我们需要一些将文本转换为实际应用程序的方法,我们的计算机可以运行。从文本形式到实际可执行的二进制文件,我们基本上有两个主要......