list 简化版模拟实现

 1ListNode

template<class T>
	struct ListNode
	{
	public:
		ListNode(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
		//private:
			//共有可访问
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _data;
	};

 实现iterator对Node*的封装  实现运算符重载

 

void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针    it内的_node 的
{
	MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动
	while (it != lt.end())
	{
		//报错,必须是可修改的左值  *it += 19;
		//*it += 19;
		cout << (*it) << ' ';
		it++;
	}
	cout << endl;
}

 2ListIterator

template<class T, class Ref, class Ptr>
	class ListIterator
	{
	public:
		typedef ListNode<T> Node;
		typedef ListIterator<T, Ref, Ptr> Self;//变化
		ListIterator(Node* node)
			:_node(node)
		{}
		Ref operator*()//变化
		{
			return _node->_data;
		}
		Ptr operator->()//变化
		{
			return &(_node->_data);
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp = *this;
			_node = _node->_next;
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		Self operator--(int)
		{
			Self tmp = *this;
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(Self it)
		{
			return _node != it._node;
		}
		//private:   共有
		Node* _node;
	};

 3list

template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		//typedef ListIterator<T> iterator;
		//typedef ListConstIterator<T> const_iterator;//但这样要实现两个类 可以合并

		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const  T*> const_iterator;

		iterator begin()//头是head下一个节点
		{
			return _head->_next;//单参数的自定义类型支持隐式类型转化  c++11对于多参数的也可以{xx,xx,xx}
		}
		iterator end()//尾是head
		{
			return _head;
		}
		const_iterator begin()const//直接加const 对于const对象能调用,然后刚好构成函数重载,
		{
			return _head->_next;//返回值也不能是 const itertor 这指的是it不能改,应该封装成(*it)不可改
		}
		const_iterator end()const
		{
			return _head;
		}
		void empty_init()
		{
			//先new一个节点
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		list(initializer_list<T>il)
		{
			empty_init();
			for (auto& e : il)
				push_back(e);
		}
		list()
		{
			empty_init();
		}
		list(const list<T>& lt)
		{
			empty_init();
			for (auto& i : lt)//要引用 有可能是自定义类型
			{
				push_back(i);
			}
		}
		/*void push_back(const T& x)
		{
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}*/
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(--end());//不能是-1,没有重载
		}
		void pop_front()
		{
			erase(begin());
		}
		void insert(iterator pos, T val)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}
		iterator erase(iterator pos)//迭代器失效  返回next
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;//
			return iterator(next);
		}
		size_t size()
		{
			size_t sz = 0;
			for (auto i : (*this))
				sz++;
			return sz;
		}
		bool empty()
		{
			return size() == 0;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		~list()
		{
			clear();
			delete _head;
		}
	private:
		Node* _head;
	};

 4对于const迭代器的解释和->操作符重载

fa5e1ff83ddb4e0f98f7ce0b8e20d359.png

struct A
{
	int _a1;
	int _a2;
	A(int a1 = 0, int a2 = 0)
		:_a1(a1)
		, _a2(a2)
	{}
};
void const_print(const MyList::list<int>& lt)//这里的const本意是lt的node中的data不可改,却使lt不可改(
//const迭代器是指, it已经封装过,把it当作指向node中的data的指针    
{
	MyList::list<int>::const_iterator it = lt.begin();//所以应该是 通过函数重载,返回一个迭代器,不能支持对(*it)的改动
	while (it != lt.end())
	{
		//报错,必须是可修改的左值  *it += 19;
		//*it += 19;
		cout << (*it) << ' ';
		it++;
	}
	cout << endl;
}
void print(MyList::list<int>& lt)
{
	MyList::list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		*it += 19;
		cout << (*it) << ' ';
		it++;
	}
	cout << endl;
}
void test2()
{
	MyList::list<A>lt;
	lt.push_back({ 1,2 });//隐式类型转换,c++11
	lt.push_back({ 1,2 });
	lt.push_back({ 1,2 });
	MyList::list<A>::iterator it = lt.begin();
	while (it != lt.end())
	{
		//报错cout << (*it) << " ";//(*it)访问的是 节点的数据 对于int 数据就是int 支持流输出 ,这里是A对象
		//其实就是把it当作 指向A的指针,但本质不是    (*it)就是A  
		//就是(*it)经过封装,(*it)就是A  ,,但it->没封装
		//cout << (*it)._a1 << "." << (*it)._a2<<endl;//但本应该it->data  要支持->重载  返回值是_data *
		cout << it->_a1 << "." << it->_a2 << endl;
		it++;
	}
	cout << endl;
}

 b1b94468eae447dea7b5c3a62572b24b.png

 

  • 37
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用和提供了关于实现vector的两种方法。其中,引用展示了一个使用reserve和push_back方法的示例,而引用展示了一个使用new和memcpy函数的示例。这两种方法都是常见的实现vector的方式。 在第一种方法中,通过reserve函数可以预留足够的内存空间,然后使用push_back函数逐个将元素添加到vector中。这种方法的好处是可以避免不必要的内存重分配,提高了效率。 而第二种方法使用new操作符在堆上分配内存空间,并使用memcpy函数将已有的vector对象的数据复制到新的内存空间中。通过这种方式,可以实现深拷贝,即两个vector对象拥有独立的内存空间。这种方法的好处是可以在不修改原始vector对象的情况下创建一个新的vector对象。 除了以上两种方法,还可以使用其他方式实现vector类。例如,可以使用动态数组来实现vector的底层数据结构,然后通过成员函数实现vector的各种操作,如增加、删除、查找等。 总结来说,c语言模拟实现vector的关键是动态内存管理和对元素的增删改查操作。可以使用预留空间和逐个添加元素的方式,也可以使用动态数组和复制数据的方式来实现vector类。具体的实现方式可以根据需求和实际情况选择。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++——vector模拟实现](https://blog.csdn.net/weixin_49449676/article/details/126813526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值