C++STL容器(一)

定长数组

#include <iostream>
#include <string>
#include <array>
using namespace std;
template <class _Ty,size_t size>
class Myarray
{
public:
	Myarray()
	{
		memroy = new _Ty[size];
	}
	_Ty& operator[](int index)
	{
		return memroy[index];
	}
	~Myarray()
	{
		delete[] memroy;
		memroy = nullptr;
	}
//迭代器
public:
	_Ty* begin()
	{
		return memroy + 0;
	}
	_Ty* end()
	{
		return memroy + size;
	}
	//类的对象模仿指针的行为
	class iterator
	{
	public:
		iterator(_Ty* pmove = nullptr) :pmove(pmove){}
		void operator=(_Ty* pmove)
		{
			this->pmove = pmove;
		}
		bool operator!=(_Ty* pmove)
		{
			return this->pmove != pmove;
		}
		iterator operator++(int)
		{
			this->pmove++;
			return *this;
		}
		_Ty operator*()
		{
			return pmove[0];
		}
	protected:
		_Ty* pmove;
	};
protected:
	_Ty* memroy;
};
//自实现定长数组
void testMyarray()
{
#define Size 3
	//创建并初始化
	array<int, Size> array1 = {0,0,0};
	for (int i = 0; i < array1.size(); i++)
	{
		array1[i]=i;
	}
	Myarray<int, 3> myarray;
	for (int i = 0; i < 3; i++)
	{
		myarray[i] = i;
	}
	//自实现迭代器
	Myarray<int, 3>::iterator iter;
	for (iter = myarray.begin(); iter != myarray.end(); iter++)
	{
		cout << *iter<<"\t";
	}
	cout << endl;
}
//其他操作
void testExOperator()
{
	array<int, 3> array1 = { 1, 2, 3 };
	//判断是否为空
	cout << array1.empty() << endl;
	//判断当前元素个数
	cout << array1.size() << endl;
	//填充
	array1.fill(5);
	//新版遍历
	for (auto i : array1)
	{
		cout << i << "\t";
	}
	cout << endl;
	array<int, 3> array2 = { 0, 0, 0 };
	//交换等长数据
	array1.swap(array2);
}
class Boy
{
public:
	Boy(){}
	Boy(string name, int age) :name(name), age(age){}
	void print()
	{
		cout << name << "\t" << age << endl;
	}
protected:
	string name;
	int age;
};
//定长数组处理自定义数据类型
void testUserData()
{
	array<Boy, 3> boydata;
	for (int i = 0; i < boydata.size(); i++)
	{
		string name = "name";
		boydata[i] = Boy(name + to_string(i), 18 + i);
	}
	for (auto v:boydata)
	{
		v.print();
	}
	//迭代器访问(对象模仿指针行为)
	array<Boy, 3>::iterator iter;
	//begin()第一个元素 end()最后一个位置
	(*boydata.begin()).print();
	(*(boydata.end()-1)).print();
	for (iter = boydata.begin(); iter != boydata.end(); iter++)
	{
		iter->print();
	}
}
int main()
{
	testMyarray();
	testExOperator();
	testUserData();
	return 0;
}

动态数组

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//模板遍历容器
template <class _Ty>
void printvector(vector<_Ty>& temp)
{
	for (auto v : temp)
	{
		cout << v << "\t";
	}
	cout << endl;
}
//vector基本用法
void testCreateVector()
{
	//1.不带长度的创建方式
	vector<int> vecdata;
	//无长度限制,只能用成员函数插入
	for (int i = 0; i < 3; i++)
	{
		vecdata.push_back(i);//尾插法
	}
	printvector(vecdata);
	//2.带长度的创建方式
	vector<string> strdata(3);
	//在长度限制内,采用下标法插入
	for (int i = 0; i < 3; i++)
	{
		string name = "name";
		strdata[i] = name+to_string(i);//注:vector subscript out of range动态数组下标超出范围
	}
	printvector(strdata);
	//3.带初始化的创建方式
	vector<double> ddata = { 0.1, 1.1, 2.1 };//自动推断长度为三
	printvector(ddata);
	//迭代器
	vector<string>::iterator iter;
	for (iter = strdata.begin(); iter != strdata.end(); iter++)
	{
		cout << *iter << "\t";
	}
	cout << endl;
}
//自定义类型数据
class Boy
{
public:
	Boy(string name, int age) :name(name), age(age){}
	friend ostream& operator<<(ostream& out, Boy& boy)
	{
		out << boy.name << "\t" << boy.age;
		return out;
	}
protected:
	string name;
	int age;
};
void testUserData()
{
	vector<Boy> boydata;
	for (int i = 0; i < 3; i++)
	{
		string name = "name";
		boydata.push_back(Boy(name + to_string(i), 18 + i));
	}
	printvector(boydata);
}
//其他操作
void testExOperator()
{
	vector<int> idata = { 1, 2, 3, 4 };
	cout << idata.size() << endl;     //当前容器元素个数
	cout << idata.empty() << endl;    //判断是否为空
	cout << idata.front() << endl;    //访问第一个元素
	cout << idata.back() << endl;     //访问最后一个元素
	cout << idata.at(2) << endl;      //下标访问  等价idata[2]
	idata.emplace(idata.begin()+2, 100);//修改下标为2的元素
	printvector(idata);
	idata.emplace_back(99);           //尾部插入
	printvector(idata);
	//批量复制
	int arr[] = { 1, 2, 3, 4, 5 };
	vector<int> vecdata;
	vecdata.assign(arr, arr + 5);
	printvector(vecdata);
}
int main()
{
	testCreateVector();
	testUserData();
	testExOperator();
	return 0;
}

array和vector的嵌套

#include <iostream>
#include <string>
#include <vector>
#include <array>
using namespace std;
void testAorA()
{
	array<array<int, 3>, 4> arrdata;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			arrdata[i][j] = i*j;
			cout << arrdata[i][j] << "\t";
		}
		cout << endl;
	}
}
void testVorV()
{
	vector<vector<int>> vecdata;
	for (int i = 0; i < 3; i++)
	{
		vector<int> temp;
		for (int j = 0; j < 4; j++)
		{
			temp.push_back(i*j);
		}
		vecdata.push_back(temp);
	}
	//不等列的二维数组
	for (int i = 0; i < vecdata.size(); i++)
	{
		for (int j = 0; j < vecdata[i].size(); j++)
		{
			cout << vecdata[i][j] << "\t";
		}
		cout << endl;
	}
}
void testAorV()
{
	array<vector<int>, 3> arrdata;
	vector<int> vecdata[3] = {{ 2, 3, 4 }, { 1, 3, 4 ,5}, { 1, 2}};
	for (int i = 0; i < arrdata.size(); i++)
	{
		arrdata[i] = vecdata[i];
	}
	for (int i = 0; i < arrdata.size(); i++)
	{
		for (int j = 0; j < arrdata[i].size(); j++)
		{
			cout << arrdata[i][j] << "\t";
		}
		cout << endl;
	}
}
void testVorA()
{
	vector<array<int, 3>> vecdata;
	array<int,3> arrdata[3] = { { 2, 3, 4 }, { 1, 3, 4}, { 1, 2 ,3} };
	for (int i=0; i < 3; i++)
	{
		vecdata.push_back(arrdata[i]);
	}
	for (int i = 0; i < vecdata.size(); i++)
	{
		for (int j = 0; j < vecdata[i].size(); j++)
		{
			cout << vecdata[i][j] << "\t";
		}
		cout << endl;
	}
}
int main()
{
	testAorA();
	testVorV();
	testAorV();
	testVorA();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值