zoukankan      html  css  js  c++  java
  • 实现一个可变长数组

    在这里插入图片描述

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<set>
    #include<queue>
    #include<unordered_map>
    #include<cmath>
    using namespace std;
    class CArray
    {
    	int st = 32;
    	int size, temp;
    	int* ptr;
    public:
    	CArray(int s = 0);
    	CArray(CArray& a);
    	~CArray();
    	void push_back(int v);
    	void resize(int v);
    	void Resize(int v);
    	CArray& operator=(const CArray& a);
    	int length() { return max(size,temp); };
    	int& operator [](int i)
    	{
    		return ptr[i];
    	}
    };
    CArray::CArray(int s):size(s)
    {
    	if (s == 0)
    		ptr = NULL;
    	else
    		ptr = new int[s];
    }
    CArray::CArray(CArray& a)
    {
    	if (!a.ptr)
    	{
    		ptr = NULL;
    		size = 0;
    		return;
    	}
    	ptr = new int[a.size];
    	memcpy(ptr, a.ptr, sizeof(int) * a.size);
    	size = a.size;
    }
    CArray::~CArray()
    {
    	if (ptr) delete[]ptr;
    }
    CArray& CArray::operator=(const CArray& a)
    {
    	if (ptr == a.ptr)
    		return *this;
    	if (a.ptr == NULL)
    	{
    		if (ptr) delete[]ptr;
    		ptr = NULL;
    		size = 0;
    		return *this;
    	}
    	if (size < a.size)
    	{
    		if (ptr) delete[]ptr;
    		ptr = new int[a.size];
    	}
    	memcpy(ptr, a.ptr, sizeof(int) * a.size);
    	size = a.size;
    	return *this;
    }
    void CArray::resize(int v)
    {
    	int* temptr = new int[v];
    	memcpy(temptr, ptr, sizeof(int) * size);
    	delete[]ptr;
    	ptr = temptr;
    	if (v > size)
    	{
    		temp = size;
    		while (temp < v)
    		{
    			ptr[temp++] = 0;
    		}
    	}
    }
    void CArray::Resize(int v)
    {
    	int* temptr = new int[v];
    	memcpy(temptr, ptr, sizeof(int) * size);
    	delete[]ptr;
    	ptr = temptr;
    }
    void CArray::push_back(int v)
    {
    	if (ptr)
    	{
    		if(size<st)
    			ptr[size++] = v;
    		else
    		{
    			st = st * 2;
    			Resize(st);
    			ptr[size++] = v;
    		}
    	}
    	else
    	{
    		ptr = new int[st];
    		ptr[size++] = v;
    	}
    
    }
    int main()
    {
    	CArray a, a2, a3, a5;
    	for (int i = 0; i < 123; i++)
    	{
    		a.push_back(i);
    	}
    	a2 = a;
    	for (int i = 0; i < a2.length(); i++)
    	{
    		cout << a2[i] << " ";
    	}
    	cout << endl;
    	a2 = a3;
    	for (int i = 0; i < a2.length(); i++)
    	{
    		cout << a2[i] << " ";
    	}
    	cout << endl;
    	a[3] = 100;
    	CArray a4(a);
    	for (int i = 0; i < a4.length(); i++)
    	{
    		cout << a4[i] << " ";
    	}
    	cout << endl;
    	a5.resize(7);
    	a5[5] = 1;
    	for (int i = 0; i < a5.length(); i++)
    	{
    		cout << a5[i] << " ";
    	}
    	return 0;
    }`
    
    
  • 相关阅读:
    十月二十七学习报告
    十月二十六学习报告
    十月二十五学习报告
    十月二十四学习报告
    十月二十三学习报告
    十月二十二学习报告
    十月二十一学习报告
    十月十九学习报告
    十月十七学习报告
    十月十六学习报告
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811942.html
Copyright © 2011-2022 走看看