C++容器作业

1.定长数组:array 头文件

  实际用法数组怎么用 我们就怎么用

  正常的使用

  

#include<iostream>
#include<array>
#include<string>
using namespace std;




int main() 
{
    array<int, 4> test = { 9,9,9 }; //元素类型为int ,个数为4
    array<string, 5> teststring = { "狐狸","狐狸2" }; //元素类型为string ,个数为5
    cout << teststring.size() << endl;//数组的元素个数
    for (int i = 0; i < teststring.size(); i++)
    {
        cout << teststring[i] << endl;//通过下标去访问就好了
    }


    return 0;
}

 实际上这个就是一个类模板,我们也可以实现模拟出一个Myarray

#include<iostream>
#include<array>
#include<string>
using namespace std;
template <class _Ty, size_t size>
class Foxarray
{
public:
    Foxarray()
    {
        memroy = new _Ty[size];//所谓数组就是动态内存申请
        mysize = size;
    }
    _Ty& operator[](int index)//重载运算符 从而实现下标访问我们定义类的操作
    {
        return memroy[index];
    }
    size_t Foxsize()
    {
        return mysize;
    }
    ~Foxarray()//做了动态内存申请了 要记得写析构函数
    {
        delete[] memroy;
    }

protected:
    _Ty* memroy;
    size_t mysize;

};





int main() 
{
   
    Foxarray < string, 3 > fox;//因为我没写多的构造函数,所以得通过下标访问去做插入
    for (int i = 0; i <fox.Foxsize(); i++)
    {
        cin >> fox[i];
    }

    for (int i = 0; i < fox.Foxsize(); i++)//输出结果看一下
    {
        cout << fox[i];
    }

    return 0;
}

在容器中 有一个叫做迭代器的东西,它的本质是类的对象模仿指针的操作

在C++中 逐渐模糊化 淡化C语言指针的概念影响。

int main() 
{

    array<int, 4>arr= {1, 2, 3, 4};
    array<int, 4>::iterator iter;//迭代器类对象的创建
    for (iter = arr.begin(); iter < arr.end(); iter++)//迭代器访问
    {
        cout << *iter << endl;
    }

}

当然这种操作最关键的点呢,是运算符重载,我们也可以为我们写的array类进行迭代器操作

#include<iostream>
#include<array>
#include<string>
using namespace std;
template <class _Ty, size_t size>
class Foxarray
{
public:
    Foxarray()
    {
        memroy = new _Ty[size];//所谓数组就是动态内存申请
        mysize = size;
    }
    _Ty& operator[](int index)//重载运算符 从而实现下标访问我们定义类的操作
    {
        return memroy[index];
    }

    _Ty* begin()
    {
        return memroy + 0;
    }
    _Ty* end()
    {
        return memroy + size;
    }
    ~Foxarray()//做了动态内存申请了 要记得写析构函数
    {
        delete[] memroy;
    }
    class MyIterator
    {
    public:
        MyIterator(_Ty* pmove = nullptr) :pmove(pmove) {}//缺省写法 ,为无参构造函数做准备
        void operator=(_Ty* pmove)
        {
            this->pmove = pmove;
        }
        bool operator!=(_Ty* pmove)
        {
            return this->pmove != pmove;
        }
        MyIterator operator++(int)
        {
            this->pmove++;
            return *this;
        }
        _Ty operator*()
        {
            return *pmove;
        }
    protected:
        _Ty* pmove;
    };



protected:
    _Ty* memroy;
    size_t mysize;

};





int main() 
{

    Foxarray<char, 3> Foxchar;
    Foxarray<char, 3>::MyIterator iser;
    for (int i = 0; i < 3; i++)
    {
        Foxchar[i] = (char)i * 100;
    }
    for (iser= Foxchar.begin(); iser != Foxchar.end(); iser++) 
    {
        cout << *iser;
    }

}

定长数组也可以存放自定义类型数据

#include<iostream>
#include<array>
#include<string>
using namespace std;
class Fox;
template <class _Ty, size_t size>
class Foxarray
{
public:
    Foxarray()
    {
        memroy = new _Ty[size];//所谓数组就是动态内存申请
        mysize = size;
    }
    _Ty& operator[](int index)//重载运算符 从而实现下标访问我们定义类的操作
    {
        return memroy[index];
    }

    _Ty* begin()
    {
        return memroy + 0;
    }
    _Ty* end()
    {
        return memroy + size;
    }
    ~Foxarray()//做了动态内存申请了 要记得写析构函数
    {
        delete[] memroy;
    }
    class MyIterator
    {
    public:
        MyIterator(_Ty* pmove = nullptr) :pmove(pmove) {}//缺省写法 ,为无参构造函数做准备
        void operator=(_Ty* pmove)
        {
            this->pmove = pmove;
        }
        bool operator!=(_Ty* pmove)
        {
            return this->pmove != pmove;
        }
        MyIterator operator++(int)
        {
            this->pmove++;
            return *this;
        }
        _Ty operator*()
        {
            return *pmove;
        }
    protected:
        _Ty* pmove;
    };



protected:
    _Ty* memroy;
    size_t mysize;

};
class Fox
{
public:
    Fox() {}
    Fox(string name, int age) :name(name), age(age) {}
    void print()
    {
        cout << name << "\t" << age << endl;
    }
protected:
    string name;
    int age;
};




int main() 
{

    array<Fox, 3> foxarr = { Fox("狐狸1",18),Fox("狐狸2",3),Fox("狐狸3",4) };
    for (Fox v : foxarr)//它会自动循环到最后一个元素
    {
        v.print();

    }

动态数组:

动态数组 ,就是会因为元素个数而自动扩增的容器

头文件 vector

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Fox
{
public:
    Fox(string name, int age) :name(name), age(age) {}
    friend ostream& operator<<(ostream& out, const Fox& temp)
    {
        out << temp.name << "\t" << temp.age;
        return out;
    }
protected:
    string name;
    int age;
};
int main() 
{
    vector<int>vec;//带类型
    vector<int>vec2(3);//带类型带长度
    //带长度可以使用下标法访问插入,不带长度的必须使用成员函数啊
    //插入的范围也只有确认大小的访问,不可以越界插入

    for (int i = 0; i < 3; i++) 
    {
        cin >> vec2[i];
    }

    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(999);//不带长度成员函数插入
    vec2.push_back(215);//确认长度也可以通过成员函数扩增插入
    //可以通过迭代器去遍历数组
    vector<int>::iterator ite;
    for (ite = vec.begin(); ite != vec.end(); ite++)
    {
        cout << *ite << endl;
    }
    vector<Fox> vfox;
    vfox.push_back(Fox("狐狸", 123));
    for (int i = 0; i < 1; i++) 
    {
        cout << vfox[i];
    }

    return 0;
}

动态数组基本成员函数

int main() 
{

	vector<int> Foxdata = { 9,8,7,6};
	cout << Foxdata.size() << endl;		//当前元素个数
	cout << Foxdata.empty() << endl;	//判断是否为空   
	cout << Foxdata.front() << endl;	//访问第一个元素
	cout << Foxdata.back() << endl;		//访问最后一个元素
	cout << Foxdata.at(2) << endl;		//下标访问
	cout << Foxdata[2] << endl;			//和at(2)一样的效果
	Foxdata.emplace(Foxdata.begin() +1, 100); //修改下标1的元素为100
	Foxdata.emplace_back(999);			//和push_back一样的功能
	Foxdata.emplace(Foxdata.begin(), 9999);	//修改第一个元素
	//复制
	int array[] = { 1,2,3 };
	vector<int> vecData;
	vecData.assign(array, array +6);   //复制元素的个数
	cout << vecData[4];
}

容器的嵌套

#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
void testArrayVsArray()
{
	array<array<int, 3>, 4> foxarr;//容器二维数组的概念 [4][3],有一个 
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 3; j++) {
			foxarr[i][j] = i * j;
			cout << foxarr[i][j] << "\t";
		}
		cout << endl;
	}
}
void testVectorVsVector()
{
	srand((unsigned int)time(nullptr));
	vector<vector<int>> foxData;
	for (int i = 0; i < 4; i++) 
	{
		vector<int> temp;
		for (int j = 0; j < rand() % 4; j++) 
		{
			temp.push_back(i * j);//先插入临时的int temp里面
		}
		foxData.push_back(temp);//插入到我们的容器里面来
	}
	//不等列数的二维数组
	for (int i = 0; i < foxData.size(); i++)
	{
		for (int j = 0; j < foxData[i].size(); j++)//通过下标访问我们每个temp.size
		{
			cout << foxData[i][j] << "\t";
		}
		cout << endl;
	}
}
void testArrayVsVector()
{
	array<vector<int>, 3> vecArr;
	vector<int> vec1[3] = { { 1,2,3 } , {1,2,3,4}, {1,2} };
	for (int i = 0; i < vecArr.size(); i++) {
		vecArr[i] = vec1[i];
	}
	//不等列数的二位数组
	for (int i = 0; i < vecArr.size(); i++) {
		for (int j = 0; j < vecArr[i].size(); j++) {
			cout << vecArr[i][j] << "\t";
		}
		cout << endl;
	}
	vector<array<array<vector<int>, 3>, 3>> vec;
	vector<int> a;
	vector<int> b;
	vector<int> c;
	a.push_back(999);
	b.push_back(888);
	c.push_back(12);
	array < vector<int>, 3 >arrvec = { a, b, c };//有三个不定长的数组
	array<array<vector<int>, 3>, 3> arrc3 = { arrvec,arrvec ,arrvec };
	vector<array<array<vector<int>, 3>, 3>> ve;
	ve.push_back(arrc3);//插入这种类型的元素不定长
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值