栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++容器作业

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++容器作业

1.定长数组:array 头文件

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

  正常的使用

  

#include
#include
#include
using namespace std;




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


    return 0;
}

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

#include
#include
#include
using namespace std;
template 
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[i];
    }

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

    return 0;
}

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

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

int main() 
{

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

}

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

#include
#include
#include
using namespace std;
template 
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 Foxchar;
    Foxarray::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
#include
#include
using namespace std;
class Fox;
template 
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 foxarr = { Fox("狐狸1",18),Fox("狐狸2",3),Fox("狐狸3",4) };
    for (Fox v : foxarr)//它会自动循环到最后一个元素
    {
        v.print();

    }

动态数组:

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

头文件 vector

#include
#include
#include
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() 
{
    vectorvec;//带类型
    vectorvec2(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::iterator ite;
    for (ite = vec.begin(); ite != vec.end(); ite++)
    {
        cout << *ite << endl;
    }
    vector vfox;
    vfox.push_back(Fox("狐狸", 123));
    for (int i = 0; i < 1; i++) 
    {
        cout << vfox[i];
    }

    return 0;
}

动态数组基本成员函数

int main() 
{

	vector 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 vecData;
	vecData.assign(array, array +6);   //复制元素的个数
	cout << vecData[4];
}

容器的嵌套

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
void testArrayVsArray()
{
	array, 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> foxData;
	for (int i = 0; i < 4; i++) 
	{
		vector 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, 3> vecArr;
	vector 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, 3>, 3>> vec;
	vector a;
	vector b;
	vector c;
	a.push_back(999);
	b.push_back(888);
	c.push_back(12);
	array < vector, 3 >arrvec = { a, b, c };//有三个不定长的数组
	array, 3>, 3> arrc3 = { arrvec,arrvec ,arrvec };
	vector, 3>, 3>> ve;
	ve.push_back(arrc3);//插入这种类型的元素不定长
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665981.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号