【C++】一篇文章带你深入了解list

news2024/9/21 7:44:27

在这里插入图片描述

目录

  • 一、list的介绍
  • 二、 标准库中的list类
    • 2.1 list的常见接口说明
      • 2.1.1 list对象的常见构造
        • 2.1.1.1 [无参构造函数](https://legacy.cplusplus.com/reference/list/list/list/)
        • 2.1.1.2 [有参构造函数(构造并初始化n个val)](https://legacy.cplusplus.com/reference/list/list/list/)
        • 2.1.1.3 [有参构造函数(使用迭代器进行初始化构造)](https://legacy.cplusplus.com/reference/list/list/list/)
        • 2.1.1.4 [拷贝构造函数](https://legacy.cplusplus.com/reference/list/list/list/)
      • 2.1.2 list iterator的使用
        • 2.1.2.1 [begin()](https://legacy.cplusplus.com/reference/list/list/begin/) + [end()](https://legacy.cplusplus.com/reference/list/list/end/)
        • 2.1.2.2 [rbegin()](https://legacy.cplusplus.com/reference/list/list/rbegin/) + [rend()](https://legacy.cplusplus.com/reference/list/list/rend/)
      • 2.1.3 list对象的容量操作
        • 2.1.3.1 [empty()函数](https://legacy.cplusplus.com/reference/list/list/empty/)
        • 2.1.3.2 [size()函数](https://legacy.cplusplus.com/reference/list/list/size/)
      • 2.1.4 list对象的增删查改及访问
        • 2.1.4.1 [push_front()函数](https://legacy.cplusplus.com/reference/list/list/push_front/)
        • 2.1.4.2 [pop_front()函数](https://legacy.cplusplus.com/reference/list/list/pop_front/)
        • 2.1.4.3 [push_back()函数](https://legacy.cplusplus.com/reference/list/list/push_back/)
        • 2.1.4.4 [pop_back()函数](https://legacy.cplusplus.com/reference/list/list/pop_back/)
        • 2.1.4.5 [insert()函数](https://legacy.cplusplus.com/reference/list/list/insert/)
        • 2.1.4.6 [erase()函数](https://legacy.cplusplus.com/reference/list/list/erase/)
        • 2.1.4.7 [swap()函数](https://legacy.cplusplus.com/reference/list/list/swap/)
        • 2.1.4.8 [clear()函数](https://legacy.cplusplus.com/reference/list/list/clear/)
        • 2.1.4.9 [front()函数](https://legacy.cplusplus.com/reference/list/list/front/) + [back()函数](https://legacy.cplusplus.com/reference/list/list/back/)
      • 2.1.5 list的迭代器失效
  • 三、list的模拟实现
    • 3.1 list 节点类的实现
    • 3.2 list 中默认成员函数的实现
    • 3.3 list 中 size、empty 和 swap 函数的实现
    • 3.4 list 中 迭代器类 的实现
    • 3.5 list 中 迭代器 、 范围构造函数 和 clear 函数 的实现
    • 3.6 list 中 insert 和 erase 的实现
    • 3.7 list 中 push_back、pop_back、push_front 和 pop_front 函数的实现
    • 3.8 list 中 反向迭代器类 和 反向迭代器 的实现
    • 3.9 list 实现汇总及函数测试
  • 四、 list 与 vector 的对比
  • 结尾

一、list的介绍

  1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

二、 标准库中的list类

2.1 list的常见接口说明

2.1.1 list对象的常见构造

2.1.1.1 无参构造函数
list();
int main()
{
	list<int> l;

	return 0;
}

在这里插入图片描述


2.1.1.2 有参构造函数(构造并初始化n个val)
list (size_type n, const value_type& val = value_type());
int main()
{
	list<int> l(5, 4);

	return 0;
}

在这里插入图片描述


2.1.1.3 有参构造函数(使用迭代器进行初始化构造)
template <class InputIterator>
 	 list (InputIterator first, InputIterator last);
int main()
{
	string s("Love");
	list<int> l(s.begin(), s.end());

	return 0;
}

在这里插入图片描述


2.1.1.4 拷贝构造函数
list (const list& x);
int main()
{
	list<int> l1(5,6);
	list<int> l2(l1);

	return 0;
}

在这里插入图片描述


2.1.2 list iterator的使用

2.1.2.1 begin() + end()
	  iterator begin();
const_iterator begin() const;
获取第一个数据位置的iterator/const_iterator

 	  iterator end();
const_iterator end() const;
获取最后一个数据的下一个位置的iterator/const_iterator
int main()
{
	list<int> l;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);
	}

	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';
		++it;
	}

	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.2.2 rbegin() + rend()
	  reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
获取最后一个数据位置的reverse_iterator/const_reverse_iterator 

	  reverse_iterator rend();
const_reverse_iterator rend() const;
获取第一个数据前一个位置的reverse_iterator/const_reverse_iterator 
int main()
{
	list<int> l;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);
	}

	list<int>::reverse_iterator it = l.rbegin();
	while (it != l.rend())
	{
		cout << *it << ' ';
		++it;
	}

	cout << endl;

	return 0;
}

在这里插入图片描述

注意

  1. beginend为正向迭代器,对迭代器执行++操作,迭代器向后移动
  2. rbeginrend为反向迭代器,对迭代器执行++操作,迭代器向前移动

2.1.3 list对象的容量操作

2.1.3.1 empty()函数
bool empty() const;         判断是否为空
int main()
{
	list<int> l;
	cout << l.empty() << endl;

	l.push_back(1);
	cout << l.empty() << endl;

	return 0;
}

在这里插入图片描述

2.1.3.2 size()函数
size_type size() const;      获取数据个数
int main()
{
	list<int> l;
	cout << l.size() << endl;

	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);
	}

	cout << l.size() << endl;

	return 0;
}

2.1.4 list对象的增删查改及访问

2.1.4.1 push_front()函数
void push_front (const value_type& val);  头插
int main()
{
	list<int> l;
	
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);

	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.4.2 pop_front()函数
void pop_front();  头删

在这里插入图片描述


2.1.4.3 push_back()函数
void push_back (const value_type& val);   尾插
int main()
{
	list<int> l;

	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.4.4 pop_back()函数
void pop_back();  尾删
int main()
{
	list<int> l;

	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	for (auto e : l)
	{	cout << e << ' ';	}
	cout << endl;

	l.pop_back();

	for (auto e : l)
	{	cout << e << ' ';	}
	cout << endl;
	return 0;
}

在这里插入图片描述


2.1.4.5 insert()函数
iterator insert (iterator position, const value_type& val);
insert()函数能够在position之前插入val,并返回插入数据位置的 iterator 

void insert (iterator position, size_type n, const value_type& val);
insert()函数能够在position之前插入 n 个 val             

template <class InputIterator>
		void insert (iterator position, InputIterator first, InputIterator last);
insert()函数能够在position之前插入一段迭代器区间的数据       		
int main()
{
	list<int> l;
	string s("Love");

	l.push_back(1);
	l.push_back(2);

	for (auto e : l)
	{	
		cout << e << ' ';	
	}
	cout << endl;

	// insert()函数能够在position之前插入val,并返回插入数据位置的 iterator 
	cout << *(l.insert(l.begin(), 20)) << endl;

	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	// insert()函数能够在position之前插入 n 个 val        
	l.insert(++l.begin() , 3 ,30);
	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	// insert()函数能够在position之前插入一段迭代器区间的数据       		
	l.insert(++l.begin(), s.begin() , s.end());
	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.4.6 erase()函数
iterator erase (iterator position);
erase()函数能够删除在position位的的数据,并返回删除数据后面数据位置的 iterator

iterator erase (iterator first, iterator last);
erase()函数能够删除在迭代器区间 [first,last) 的的数据,并返回删除数据后面数据位置的 iterator             
int main()
{
	list<int> l;
	
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);
	}
	cout << endl;

	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	// erase()函数能够删除在position位的的数据
	// 并返回删除数据后面数据位置的 iterator
	cout << *(l.erase(l.begin())) << endl;
	for (auto e : l)
	{
		cout << e << ' ';
	}
	cout << endl;

	// erase()函数能够删除在迭代器区间 [first,last) 的的数据
	// 并返回删除数据后面数据位置的 iterator        
	cout << *(l.erase(++l.begin(),--l.end())) << endl;
	for (auto e : l)
	{
		cout << e << ' ';
	}

	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.4.7 swap()函数
void swap (list& x);
交换两个list的数据空间
int main()
{
	list<int> l1(4, 10);
	list<int> l2(5, 5);

	for (auto e : l1)
	{	cout << e << ' ';	}
	cout << endl;

	for (auto e : l2)
	{	cout << e << ' ';	}
	cout << endl;

	l1.swap(l2);

	for (auto e : l1)
	{	cout << e << ' ';	}
	cout << endl;

	for (auto e : l2)
	{	cout << e << ' ';	}
	cout << endl;

	return 0;
}

在这里插入图片描述


2.1.4.8 clear()函数
void clear();
清除list中的有效数据
int main()
{
	list<int> l(4, 10);
	cout << l.size() << endl;

	l.clear();
	cout << l.size() << endl;

	return 0;
}

在这里插入图片描述


2.1.4.9 front()函数 + back()函数
访问list中的第一个数据
	  reference front();
const_reference front() const;

访问list中的最后一个数据
 	  reference back();
const_reference back() const;
int main()
{
	list<int> l;

	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);
	}

	cout << "front:" << l.front() << endl;
	cout << "back:" << l.back() << endl;

	return 0;
}

在这里插入图片描述


2.1.5 list的迭代器失效

前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

int main()
{
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	list<int> l(array, array + sizeof(array) / sizeof(array[0]));
	auto it = l.begin();
	while (it != l.end())
	{
		// erase()函数执行后,it所指向的节点已被删除
		// 因此it无效,在下一次使用it时,必须先给其赋值
		l.erase(it);
		++it;
	}

	return 0;
}


// 改正
int main()
{
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	list<int> l(array, array + sizeof(array) / sizeof(array[0]));
	auto it = l.begin();
	while (it != l.end())
	{
		l.erase(it++); // it = l.erase(it);
	}
}

三、list的模拟实现

3.1 list 节点类的实现

namespace aj
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T())
            :_val(val)
        {}

        ListNode<T>* _prev = nullptr;
        ListNode<T>* _next = nullptr;
        T _val;
    };
};

3.2 list 中默认成员函数的实现

namespace aj
{
    //list
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    
    public:
        // List的构造
        list()
        {
            CreateHead();
        }

        // 构造并用n个val初始化
        list(int n, const T& value = T())
        {
            CreateHead();

            while (n--)
            {
                push_back(value);
            }
        }

        // 链表的拷贝构造
        // list(const list<T>& l)
        list(list<T>& l)
        {
            CreateHead();

            for (auto e : l)
            {
                push_back(e);
            }
        }

        list<T>& operator=(const list<T> l)
        {
            swap(l);

            return *this;
        }
        ~list()
        {
            clear();

            delete _head;
            _head = nullptr;
        }

        void swap(list<T>& l)
        {
            std::swap(_head, l._head);
            std::swap(_size, l._size);
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数

    };
};

3.3 list 中 size、empty 和 swap 函数的实现

namespace aj
{
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        size_t size()const
        {
            return size;
        }
        bool empty()const
        {
            return _head->_next == _head && _head->_prev == _head;
        }

        void swap(list<T>& l)
        {
            std::swap(_head, l._head);
            std::swap(_size, l._size);
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数
    };
};

3.4 list 中 迭代器类 的实现

namespace aj
{
    //List的迭代器类
    //template<class T>
    template<class T, class Ref, class Ptr>
    struct ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;
        // 成员变量
        PNode _pNode;

    public:
        // 迭代器的构造函数
        ListIterator(PNode pNode = nullptr)
            :_pNode(pNode)
        {}

        Ref operator*()
        {
            return _pNode->_val;
        }

        Ptr operator->()
        {
            return &(_pNode->_val);
        }

        Self& operator++()
        {
            _pNode = _pNode->_next;
            return *this;
        }

        Self operator++(int)
        {
            Self tmp(*this);
            ++* this;
            return tmp;
        }

        Self& operator--()
        {
            _pNode = _pNode->_prev;
            return *this;
        }

        Self operator--(int)
        {
            Self tmp(*this);
            --* this;
            return tmp;
        }

        bool operator!=(const Self& l)
        {
            return _pNode != l._pNode;
        }

        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;
        }

    };
};

3.5 list 中 迭代器 、 范围构造函数 和 clear 函数 的实现

namespace aj
{
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;

        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            CreateHead();
            while (first != last)
            {
                push_back(first._pNode->_val);
                ++first;
            }
        }
        
        // List Iterator
        iterator begin()
        {
            // return iterator(_head->_next);
            return _head->_next;
        }

        iterator end()
        {
            // return iterator(_head);
            return _head;
        }

        const_iterator begin()const
        {
            return _head->_next;

        }
        const_iterator end()const
        {
            return _head;
        }

        void clear()
        {
            list<T>::iterator lit = begin();
            while (lit != end())
            {
                lit = erase(lit);
            }
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数
    };
};

3.6 list 中 insert 和 erase 的实现

namespace aj
{
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T&> const_iterator;
        
        // 在pos位置前插入值为val的节点,返回插入新节点的位置
        iterator insert(iterator pos, const T& val)
        {
            // 通过迭代器找到所需的节点指针
            Node* cur = pos._pNode;
            Node* prev = cur->_prev;

            // 创建新的节点
            Node* newnode = new Node(val);

            // 节点间相互连接
            newnode->_prev = prev;
            prev->_next = newnode;
            cur->_prev = newnode;
            newnode->_next = cur;

            // 节点数量++
            _size++;

            //return iterator(newnode);
            return newnode;
        }

        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            assert(_size > 0);
            // 找到所需要的节点指针
            Node* cur = pos._pNode;
            Node* next = cur->_next;
            Node* prev = cur->_prev;

            // 节点相互连接
            next->_prev = prev;
            prev->_next = next;

            // 删除节点
            delete cur;
            cur = nullptr;

            // 减少节点数目
            --_size;
            // 返回删除节点的下一个位置
            // return iterator(next);
            return next;
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数
    };
};

3.7 list 中 push_back、pop_back、push_front 和 pop_front 函数的实现

namespace aj
{
    template<class T>
    class list
    {
    public:
        typedef ListNode<T> Node;
        typedef Node* PNode;
        
        void push_back(const T& val) { insert(end(), val); }
        void pop_back() { erase(--end()); }
        void push_front(const T& val) { insert(begin(), val); }
        void pop_front() { erase(begin()); }

    private:
        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数
    };
};


3.8 list 中 反向迭代器类 和 反向迭代器 的实现

反向迭代器的++就是正向迭代器的--,反向迭代器的--就是正向迭代器的++,因此反向迭代器的实现可以借助正向迭代器,即:反向迭代器内部可以包含一个正向迭代器,对正向迭代器的接口进行包装即可。

注意:反向迭代器类可以被所有的容器封装成反向迭代器使用。

下面两种反向迭代器的实现虽然不同,但是功能是相同的。反向迭代器类的实现不同,那么对应封装迭代器的时候也要做出相应的改变。

在这里插入图片描述

reverse_iterator.h 反向迭代器非对称版本
#pragma once

// 不对称版本
namespace aj
{
	// 适配器 -- 复用
	template<class Iterator, class Ref, class Ptr>
	struct Reverse_iterator
	{
		typedef Reverse_iterator<Iterator,Ref,Ptr> Self;

		Reverse_iterator(const Iterator& it)
			:_it(it)
		{}

		Ref operator*()
		{
			return *_it;
		}

		Ptr operator->()
		{
			return _it.operator->();
		}

		Self& operator++()
		{
			--_it;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			--_it;
			return tmp;
		}

		Self& operator--()
		{
			++_it;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			++_it;
			return tmp;
		}

		bool operator!=(const Self& s)
		{
			return _it != s._it;
		}

		bool operator==(const Self& s)
		{
			return _it == s._it;
		}
		Iterator _it;
	};
}
list.h 反向迭代器非对称版本
#include"reverse_iterator.h"

namespace aj
{
    template<class T>
    class list
    {
    public:
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        // typedef ListIterator<T> iterator;

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

    public:
 
        // reverse_iterator 不对称版本
        reverse_iterator rbegin()
        {
            // return iterator(_head->_next);
            return --end();    //这里可以使用--end()也可以使用end()-1,但是没有写operator-()
        }                      //这里就使用--end(),这里能使用--end()的原因是
                               //end()返回传值返回的自定义类型的临时对象,具有常性,是常量
                               //但是这里有编译器的特殊处理
                               //使得const对象可以调用非const成员函数

        reverse_iterator rend()
        {
            // return iterator(_head);
            return end();
        }

        const_reverse_iterator rbegin()const
        {
            return --end();

        }
        const_reverse_iterator rend()const
        {
            return end();
        }
    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数

    };
};
reverse_iterator.h 反向迭代器对称版本
#pragma once
// 对称版本
namespace aj
{
	// 适配器 -- 复用
	template<class Iterator, class Ref, class Ptr>
	struct Reverse_iterator
	{
		typedef Reverse_iterator<Iterator,Ref,Ptr> Self;

		Reverse_iterator(const Iterator& it)
			:_it(it)
		{}

		Ref operator*()
		{
			Iterator tmp(_it);
			return *--tmp;
		}

		Ptr operator->()
		{
			return --_it.operator->();
		}

		Self& operator++()
		{
			--_it;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			--_it;
			return tmp;
		}

		Self& operator--()
		{
			++_it;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			++_it;
			return tmp;
		}

		bool operator!=(const Self& s)
		{
			return _it != s._it;
		}

		bool operator==(const Self& s)
		{
			return _it == s._it;
		}
		Iterator _it;
	};
}
list.h 反向迭代器对称版本
#include"reverse_iterator.h"

namespace aj
{
    template<class T>
    class list
    {
    public:
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        // typedef ListIterator<T> iterator;

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

    public:
        // reverse_iterator 对称版本
        reverse_iterator rbegin()
        {
            // return iterator(_head->_next);
            return end();
        }

        reverse_iterator rend()
        {
            // return iterator(_head);
            return begin();
        }

        const_reverse_iterator rbegin()const
        {
            return end();

        }
        
        const_reverse_iterator rend()const
        {
            return begin();
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数

    };
};

3.9 list 实现汇总及函数测试

reverse_iterator.h
#pragma once

// 不对称版本
//namespace aj
//{
//	// 适配器 -- 复用
//	template<class Iterator, class Ref, class Ptr>
//	struct Reverse_iterator
//	{
//		typedef Reverse_iterator<Iterator,Ref,Ptr> Self;
//
//		Reverse_iterator(const Iterator& it)
//			:_it(it)
//		{}
//
//		Ref operator*()
//		{
//			return *_it;
//		}
//
//		Ptr operator->()
//		{
//			return _it.operator->();
//		}
//
//		Self& operator++()
//		{
//			--_it;
//			return *this;
//		}
//
//		Self operator++(int)
//		{
//			Self tmp(*this);
//			--_it;
//			return tmp;
//		}
//
//		Self& operator--()
//		{
//			++_it;
//			return *this;
//		}
//
//		Self operator--(int)
//		{
//			Self tmp(*this);
//			++_it;
//			return tmp;
//		}
//
//		bool operator!=(const Self& s)
//		{
//			return _it != s._it;
//		}
//
//		bool operator==(const Self& s)
//		{
//			return _it == s._it;
//		}
//		Iterator _it;
//	};
//}


// 对称版本
namespace aj
{
	// 适配器 -- 复用
	template<class Iterator, class Ref, class Ptr>
	struct Reverse_iterator
	{
		typedef Reverse_iterator<Iterator,Ref,Ptr> Self;

		Reverse_iterator(const Iterator& it)
			:_it(it)
		{}

		Ref operator*()
		{
			Iterator tmp(_it);
			return *--tmp;
		}

		Ptr operator->()
		{
			return --_it.operator->();
		}

		Self& operator++()
		{
			--_it;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			--_it;
			return tmp;
		}

		Self& operator--()
		{
			++_it;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			++_it;
			return tmp;
		}

		bool operator!=(const Self& s)
		{
			return _it != s._it;
		}

		bool operator==(const Self& s)
		{
			return _it == s._it;
		}
		Iterator _it;
	};
}
list.h
#pragma once

#include<iostream>
#include<assert.h>
#include <string>
#include <vector>
using namespace std;

#include"reverse_iterator.h"

namespace aj
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T())
            :_val(val)
        {}

        ListNode<T>* _prev = nullptr;
        ListNode<T>* _next = nullptr;
        T _val;
    };


    //List的迭代器类
    //template<class T>
    template<class T, class Ref, class Ptr>
    struct ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T ,Ref , Ptr> Self;
    // 成员变量
        PNode _pNode;

        // typedef ListIterator<T, Ref, Ptr> Self;
    public:
        // 迭代器的构造函数
        ListIterator(PNode pNode = nullptr)
            :_pNode(pNode)
        {}

        Ref operator*()
        {
            return _pNode->_val;
        }

        Ptr operator->()
        {
            return &(_pNode->_val);
        }

        Self& operator++()
        {
            _pNode = _pNode->_next;
            return *this;
        }

        Self operator++(int)
        {
            Self tmp(*this);
            ++* this;
            return tmp;
        }

        Self& operator--()
        {
            _pNode = _pNode->_prev;
            return *this;
        }

        Self operator--(int)
        {
            Self tmp(*this);
            --* this;
            return tmp;
        }

        bool operator!=(const Self& l)
        {
            return _pNode != l._pNode;
        }

        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;
        }

    };


    //list类
    template<class T>
    class list
    {
    public:
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        // typedef ListIterator<T> iterator;

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

    public:
        ///
        // List的构造
        list()
        {
            CreateHead();
        }

        list(int n, const T& value = T())
        {
            CreateHead();

            while (n--)
            {
                push_back(value);
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            CreateHead();
            while (first != last)
            {
                push_back(first._pNode->_val);
                ++first;
            }
        }
        // 链表的拷贝构造
        // list(const list<T>& l)
        list(list<T>& l)
        {
            CreateHead();

            for (auto e : l)
            {
                push_back(e);
            }
        }

        //list<T>& operator=(const list<T> l)
             
        list<T>& operator=(list<T> l)
        {
            swap(l);

            return *this;
        }
        ~list()
        {
            clear();

            delete _head;
            _head = nullptr;
        }


        ///
        // List Iterator
        iterator begin()
        {
            // return iterator(_head->_next);
            return _head->_next;
        }

        iterator end()
        {
            // return iterator(_head);
            return _head;
        }

        const_iterator begin()const
        {
            return _head->_next;

        }
        const_iterator end()const
        {
            return _head;
        }
         reverse_iterator 不对称版本
        //reverse_iterator rbegin()
        //{
        //    // return iterator(_head->_next);
        //    return --end();    //这里可以使用--end()也可以使用end()-1,但是没有写operator-()
        //}                      //这里就使用--end(),这里能使用--end()的原因是
        //                       //end()返回传值返回的自定义类型的临时对象,具有常性,是常量
        //                       //但是这里有编译器的特殊处理
        //                       //使得const对象可以调用非const成员函数
                                 
        //reverse_iterator rend()
        //{
        //    // return iterator(_head);
        //    return end();
        //}

        //const_reverse_iterator rbegin()const
        //{
        //    return --end();

        //}
        //const_reverse_iterator rend()const
        //{
        //    return end();
        //}

        // reverse_iterator 对称版本
        reverse_iterator rbegin()
        {
            // return iterator(_head->_next);
            return end();
        }

        reverse_iterator rend()
        {
            // return iterator(_head);
            return begin();
        }

        const_reverse_iterator rbegin()const
        {
            return end();

        }
        const_reverse_iterator rend()const
        {
            return begin();
        }

        ///
        // List Capacity
        size_t size()const
        {
            return size;
        }
        bool empty()const
        {
            return _head->_next == _head && _head->_prev == _head;
         }

        
        // List Access
        T& front()
        {
            assert(_head->_next != _head);
            return _head->_next->_val;
        }

        const T& front()const
        {
            assert(_head->_next != _head);
            return _head->_next->_val;
        }
        T& back()
        {
            assert(_head->_prev != _head);
            return _head->_prev->_val;
        }
        const T& back()const
        {
            assert(_head->_prev != _head);
            return _head->_prev->_val;
        }


        
        // List Modify
        void push_back(const T& val) { insert(end(), val); }
        void pop_back() { erase(--end()); }
        void push_front(const T& val) { insert(begin(), val); }
        void pop_front() { erase(begin()); }
        // 在pos位置前插入值为val的节点,返回插入新节点的位置
        iterator insert(iterator pos, const T& val)
        {
            // 通过迭代器找到所需的节点指针
            Node* cur = pos._pNode;
            Node* prev = cur->_prev;

            // 创建新的节点
            Node* newnode = new Node(val);

            // 节点间相互连接
            newnode->_prev = prev;
            prev->_next = newnode;
            cur->_prev = newnode;
            newnode->_next = cur;

            // 节点数量++
            _size++;

            //return iterator(newnode);
            return newnode;
        }

        // 删除pos位置的节点,返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            assert(_size > 0);
            // 找到所需要的节点指针
            Node* cur = pos._pNode;
            Node* next = cur->_next;
            Node* prev = cur->_prev;

            // 节点相互连接
            next->_prev = prev;
            prev->_next = next;

            // 删除节点
            delete cur;
            cur = nullptr;

            // 减少节点数目
            --_size;
            // 返回删除节点的下一个位置
            // return iterator(next);
            return next;
        }

        void clear()
        {
            list<T>::iterator lit = begin();
            while (lit != end())
            {
                lit = erase(lit);
            }
        }

        void swap(list<T>& l)
        {
            std::swap(_head, l._head);
            std::swap(_size, l._size);
        }

    private:
        void CreateHead()
        {
            _head = new Node();
            _head->_next = _head;
             _head->_prev = _head;

            _size = 0;
        }

        PNode _head;    // 头结点
        int _size;      // 记录链表中节点的个数

    };


    struct AA
    {
        AA(int a1 = 0 , int a2 = 0)
            :_a1(a1)
            ,_a2(a2)
        {}

        int _a1;
        int _a2;
    };

    //template<class T>
    //void print_list(const list<T>& l)
    //{
    //    // list<T>未实例化的类模板,编译器不能去他里面去找
    //    // 那么编译器就无法确定这里的
    //    // const_iterator是静态变量还是内嵌类型
    //    // 加上typename就相当于告诉编译器这里是内嵌类型
    //    // 等list<T>初始化后再到类中去取
    //    typename list<T>::const_iterator it = l.begin();
    //    while (it != l.end())
    //    {
    //        cout << *it << ' ';
    //        ++it;
    //    }
    //    cout << endl;
    //}


    template<class Container>
    void print_container(const Container& l)
    {
        typename Container::const_iterator it = l.begin();
        while (it != l.end())
        {
            cout << *it << ' ';
            ++it;
        }
        cout << endl;
    }

///

    // 测试无参构造、n个val的构造、迭代器区间构造
    void test_list1()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);

        list<int> l1(10, 20);
        list<int> l2(++l1.begin(), --l1.end());

        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;

        for (auto e : l1)
        {
            cout << e << ' ';
        }
        cout << endl;

        for (auto e : l2)
        {
            cout << e << ' ';
        }
        cout << endl;
    }

    // 测试 insert push_back push_front
    // 测试 iterator 范围for
    // 测试 operator !=  operator* 
    void test_list2()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);
        l.push_front(10);
        l.push_front(20);


        list<int>::iterator lit = l.begin();
        while (lit != l.end())
        {
            cout << *lit << ' ';
            ++lit;
        }
        cout << endl;

        for (auto& e : l)
        {
            e += 10;
            cout << e << ' ';
        }
        cout << endl;
    }

    // 测试 erase pop_back pop_front
    void test_list3()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);

        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;

        l.pop_back();
        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;

        l.pop_front();
        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;
    }

    // 测试 operator++  operator++(int)
    // 测试 operator--  operator--(int)
    void test_list4()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);

        list<int>::iterator lit1 = l.begin();
        cout << *(lit1++) << endl;
        cout << *(++lit1) << endl;

        list<int>::iterator lit2 = l.end();
        cout << *(--lit2) << endl;
        cout << *(lit2--) << endl;
        cout << endl;
    }

    // 测试 operator==  operator!=
    void test_list5()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);

        list<int>::iterator lit = l.end();
        cout << (lit == lit) << endl;
        cout << (lit != lit) << endl;
        cout << endl;
    }

    void test_list6()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);

        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;
        cout << l.front() << ' ' << l.back() << endl;

        l.pop_back();
        l.pop_front();

        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;
        cout << l.front() << ' ' << l.back() << endl;

        l.pop_back();
        l.pop_front();

        for (auto e : l)
        {
            cout << e << ' ';
        }
        cout << endl;
        cout << l.front() << ' ' << l.back() << endl;
    }

    // 测试 operator->
    void test_list7()
    {
        list<AA> l1;
        l1.push_back(AA(1, 1));
        l1.push_back(AA(2, 2));
        l1.push_back(AA(3, 3));
        l1.push_back(AA(4, 4));

        list<AA>::iterator it = l1.begin();
        while (it != l1.end())
        {
            cout << it->_a1 << ' ' << it->_a2 << endl;
            ++it;
        }
    }

    // 测试拷贝构造和赋值重载  
    void test_list8                                                                      ()
    {
        list<int> l1;
        l1.push_back(1);
        l1.push_back(2);
        l1.push_back(3);
        l1.push_back(4);

        // 拷贝构造l2
        list<int> l2(l1);

        // 输出l1和l2的值
        for (auto e : l1)
        {
            cout << e << ' ';
        }
        cout << endl;

        for (auto e : l2)
        {
            cout << e << ' ';
        }
        cout << endl;

        // l2中的值都*10,并输出
        for (auto& e : l2)
        {
            e *= 10;
            cout << e << ' ';
        }
        cout << endl;

        // 将l2赋值给l1
        l1 = l2;

        // 输出l1和l2的值
        for (auto e : l1)
        {
            cout << e << ' ';
        }
        cout << endl;

        for (auto e : l2)
        {
            cout << e << ' ';
        }
        cout << endl;
    }

    // 测试print_container
    void test_list9()
    {
        list<int> l1;
        l1.push_back(1);
        l1.push_back(2);
        l1.push_back(3);
        l1.push_back(4);

        print_container(l1);

        list<string> l2;
        l2.push_back("1111111111111111111");
        l2.push_back("2222222222222222222");
        l2.push_back("3333333333333333333");
        l2.push_back("4444444444444444444");

        print_container(l2);

        vector<string> v;
        v.push_back("1111111111111111111");
        v.push_back("2222222222222222222");
        v.push_back("3333333333333333333");
        v.push_back("4444444444444444444");
        print_container(v);
    }

    void test_list10()
    {
        list<int> l1;
        l1.push_back(1);
        l1.push_back(2);
        l1.push_back(3);
        l1.push_back(4);

        list<int>::reverse_iterator it = l1.rbegin();
        while (it != l1.rend())
        {
            cout << *it << ' ';
            ++it;
        }
        cout << endl;
    }
};



四、 list 与 vector 的对比

vectorlist都是STL中非常重要的序列式容器,由于两个容器的底层结构不同,导致其特性以及应用场景不同,其主要不同如下:

vectorlist
底层结构动态顺序表,一段连续空间带头结点的双向循环链
访问支持随机访问,访问某个元素的效率O(1)不支持随机访问,访问某个元素的效率为O(N)
插入和删除头部和中部的插入效率低,因为需要移动大量数据,效率为O(N),尾插和尾删的效率高,效率为O(1) 。插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低任意位置的插入和删除效率高,不需要移动数据,效率为O(N)
空间利用率底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
迭 代 器 失 效在插入元素时,要给所有的迭代器重新赋值,因为插入元素有可能会导致重新扩容,致使原来迭代器失效,删除时,当前迭代器需要重新赋值否则会失效插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效,其他迭代器不受影响
使 用 场 景需要高效存储,支持随机访问,不关心插入删除效率大量插入和删除操作,不关心随机访问

结尾

如果有什么建议和疑问,或是有什么错误,大家可以在评论区中提出。
希望大家以后也能和我一起进步!!🌹🌹
如果这篇文章对你有用的话,希望大家给一个三连支持一下!!🌹🌹
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1617194.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Win10下VS2015无法添加任何文件,提示未能加载文件或程序集“Microsoft.VisualStudio.JSLS...

错误&#xff1a;未能加载文件或程序集“Microsoft.VisualStudio.JSLS, Version14.0.0.0, Cultureneutral, PublicKeyTokenb03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。 解决&#xff1a; 1. 管理员身份打开cmd 2. cd C:\Program Files (x86)\Microsoft Vis…

Matplotlib官网查阅资料

Matplotlib官网详细的地址&#xff1a; 英文文档&#xff1a;https://matplotlib.org/stable/contents.html中文文档&#xff1a;https://www.matplotlib.org.cn/ Matplotlib英文官网: 查找属性&#xff1a; 1.进入官网。 2.查找参数属性。 Matplotlib中文官网: 查找属性:…

SVN小乌龟汉化问题

1.首先确认中文语言包和SVN版本需要一致&#xff08;点击右键 选择最后一个选项即可查看&#xff09; 官网链接 点击这个官网链接可以下载对应版本的中文包 2.下载好之后直接无脑下一步安装即可 3.如果还是没有中文&#xff0c;找到这个文件夹&#xff0c;把里面的内容全部删…

【黑马头条】-day12项目部署和发布-jenkins

文章目录 1 持续集成2 软件开发模式2.1 瀑布模式2.2 敏捷开发2.2.1 迭代开发2.2.2 增量开发 3 Jenkins3.1 Jenkins安装3.1.1 导入镜像3.1.2 配置3.1.3 初始化设置 3.2 插件安装3.3 服务器环境准备3.3.1 Docker安装配置3.3.2 Git安装配置3.3.3 Maven安装配置 3.4 Jenkins工具配置…

接口测试和Mock学习路线(上)

一、接口测试和Mock学习路线-第一阶段&#xff1a; 掌握接口测试的知识体系与学习路线掌握面试常见知识点之 HTTP 协议掌握常用接口测试工具 Postman掌握常用抓包工具 Charles 与 Fiddler结合知名产品实现 mock 测试与接口测试实战练习 1.接口协议&#xff1a; 需要先了解 O…

截断堆积柱状图绘制教程

本教程原文链接&#xff1a;截断堆积柱状图绘制教程 欢迎大家转载&#xff01;&#xff01;&#xff01;&#xff01; 本期教程 写在前面 堆积柱状图是柱状图的常见类型之一&#xff0c;也是平时使用概率较高的图形之一。我们前期发布了很多个柱状图的绘制教程&#xff0c;若你…

Vue3、 Vue2 Diff算法比较

Vue2 Diff算法 源码位置:src/core/vdom/patch.ts 源码所在函数:updateChildren() 源码讲解: 有新旧两个节点数组:oldCh和newCh; 有下面几个变量: oldStartIdx 初始值=0 oldStartVnode 初始值=oldCh[0] oldEndIdx 初始值=oldCh.length - 1 oldEndVnode 初始值=oldCh[ol…

java多线程-悲观锁、乐观锁

简介 悲观锁&#xff1a;没有安全感&#xff0c;一上来就直接加锁&#xff0c;每次只能一个线程进入访问&#xff0c;访问完毕之后&#xff0c;再解锁。线程安全&#xff0c;但是性能差。乐观锁&#xff1a;很乐观&#xff0c;一开始不上锁&#xff0c;认为没有问题。等到要出现…

新的全息技术突破计算障碍

一种突破性的方法利用基于Lohmann透镜的衍射模型实时创建计算机生成全息图&#xff08;CGH&#xff09;&#xff0c;在保持3D可视化质量的同时&#xff0c;大大降低了计算负荷要求。 全息显示为制作逼真的三维图像提供了一条令人兴奋的途径&#xff0c;这种图像给人以连续深度…

【Linux系统编程】第七弹---权限管理操作(上)

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】【C详解】【Linux系统编程】 目录 1、修改文件权限的做法(一) 2、有无权限的表现 总结 上一弹我们讲解了Linux权限概念相关的知识&#xff0c;但是我们只知道有…

[部分WP]DASCTF X GFCTF 2024 WEB

Web EasySignin 考点&#xff1a; 越权SSRF gopher协议去攻击mysql 通过注册任意用户修改admin密码 登入然后点击康好看图片 抓包 ?url 典型SSRF漏洞 尝试file读取/etc/passwd无果 尝试gopher协议去攻击mysql 利用工具gopherus 盲猜数据库用户为root 然后再次次url编码得到…

7.2K star!一个完全免费,可以本地部署的 AI 搜索聚合器。新手可尝试

原文链接&#xff1a;7.2K star&#xff01;一个完全免费&#xff0c;可以本地部署的 AI 搜索聚合器。新手可尝试 ChatGPT 刚上线的时候我用的很少&#xff0c;还是习惯用 Google。主要还是因为不信任&#xff0c;怕它对我胡说八道。 慢慢的&#xff0c;也没有一个明确的时间…

HarmonyOS ArkUI实战开发-NAPI 加载原理(下)

上一节笔者给大家讲解了 JS 引擎解释执行到 import 语句的加载流程&#xff0c;总结起来就是利用 dlopen() 方法的加载特性向 NativeModuleManager 内部的链接尾部添加一个 NativeModule&#xff0c;没有阅读过上节文章的小伙伴&#xff0c;笔者强烈建议阅读一下&#xff0c;本…

使用d3.js画一个BoxPlot

Box Plot 在画Box Plot之前&#xff0c;先来了解下Box Plot是什么&#xff1f; 箱线图&#xff08;Box Plot&#xff09;也称盒须图、盒式图或箱型图&#xff0c;是一种用于展示数据分布特征的统计图表。 它由以下几个部分组成&#xff1a; 箱子&#xff1a;表示数据的四分…

【圆桌论坛】个人作为嘉宾参与问答环节的总结,Create 2024百度AI开发者大会之AI智能体开发与应用论坛

目录 ⭐前言⭐讨论话题✨本质和价值✨端侧部署✨应用商业模式✨商业模式 ⭐主题总结⭐有趣分享 ⭐前言 首先&#xff0c;非常荣幸和开心作为开发者和创业者代表参加百度Create AI大会分论坛圆桌论坛的问答环节。 在分论坛活动开始前&#xff0c;参加了文心智能体平台&#xff…

【iOS】类与对象底层探索

文章目录 前言一、编译源码二、探索对象本质三、objc_setProperty 源码探索四、类 & 类结构分析isa指针是什么类的分析元类元类的说明 五、著名的isa走位 & 继承关系图六、objc_class & objc_objectobjc_class结构superClassbitsclass_rw_tclass_ro_tro与rw的区别c…

【无法运行 AutoCAD,原因可能如下1) 此版本的 AutoCAD 装不正确】

错误提示如下 打开autoremove&#xff0c;点击扩展&#xff0c;输入 无法运行&#xff0c;点击搜索 如果出现这个提示&#xff0c;请重启电脑再点击一遍此按钮 出现修复成功即可 ​如果没提示修复成功可以联系技术人员。 ps&#xff1a;autoremove每周六登录方式用其他登…

警惕侵权风险,张驰课堂六西格玛培训产品请认准官方平台购买

亲爱的朋友们&#xff1a; 在知识经济的时代&#xff0c;知识产权的保护显得尤为重要。我们深知&#xff0c;知识是创新的源泉&#xff0c;而知识产权则是保障创新成果得以合理运用的重要法律手段。然而&#xff0c;近期我们公司&#xff08;张驰课堂&#xff09;却遭遇了一起…

Web3革命:区块链如何重塑互联网

引言 互联网的发展已经深刻地改变了我们的生活方式&#xff0c;而现在&#xff0c;Web3和区块链技术正在为我们提供一个全新的数字世界的视角。本文将带你深入了解Web3的核心概念、技术特性以及它如何正在重塑我们的互联网体验。 从Web1.0到Web3&#xff1a;数字革命的演进 W…

C++ 面向对象-封装

C 是一种多范式编程语言&#xff0c;它支持面向对象编程&#xff08;OOP&#xff09;范式。面向对象编程是一种程序设计思想&#xff0c;其中程序由对象组成&#xff0c;每个对象都是一个实例&#xff0c;具有数据和相关操作。在C中&#xff0c;实现面向对象编程主要通过类和对…