C++STL容器(一)

2023-05-16

定长数组

#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;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++STL容器(一) 的相关文章

随机推荐

  • linux 命令行进行桌面图标的打开

    近期在处理一个需求 xff0c 需要在代码中打开桌面的某个图标 xff0c 因此 xff0c 做了一些搜索 xff0c 最终发现 xff0c 有两个比较好用的命令 xff0c 下面来讲解一下 1 gtk launch 在linux系统一般已
  • 算法之滑动窗口寻找最长无重复字符串

    今天无聊的时候刷了一道leetcode的题目 xff0c 给定字符串 xff0c 查找最长无重复字符串 xff0c 具体题目信息如下 xff1a 给定一个字符串 s xff0c 请你找出其中不含有重复字符的 最长子串 的长度 示例 1 输入
  • 算法之图解单纯形算法C++

    在之前的算法博客中 xff0c 结合案例和算法的图形表示 xff0c 获得了较多同学的好评 xff0c 例如之前写的迪杰斯特拉算法这篇博客 xff0c 能够让很多新同学和老同学通过直观的方式去理解算法求解的过程 xff0c 这样理解起来会比
  • linux opencv打开两个USB摄像头

    在ubuntu linux系统下 xff0c 摄像头设备是通过 dev videox来表示的 xff0c 如果只有一个摄像头 xff0c 则会在 dev目录下显示 video0和video1两个设备 xff0c 其中 xff0c video
  • 实现手机app和微信小程序远程控制加人体探测自动化控制51单片机打开流水灯(ESP8266 STC89C52RC http请求转串口通信系统)

    首先你有这样的8266 这种8266自身带2个按键和烧录芯片方便调试 xff0c 综合性价比较高 有一个51单片机 或者其他芯片都行 还有就是需要这种人体红外探测模块和led灯 有了这2个芯片我们开始吧 xff01 1 先看一段视频效果演示
  • 图像算法之图像平移

    在图像处理领域中 xff0c 图像变换是最基本的算法 xff0c 本文主要详细分享一下图像平移算法的原理和代码实现 xff0c 废话不多说 xff0c 直接上干货 1 图像平移的原理 在计算机图像中 xff0c 主要是用的是 像素直角坐标系
  • OpenCV之Mat的详细介绍

    在opencv中 xff0c Mat作为图像的存储容器 xff0c 是非常基础也是非常重要的知识 xff0c 本文主要介绍Mat相关的操作 1 图像复制 再开始将拷贝之前 xff0c 先给大家分享一下浅拷贝和深拷贝 浅拷贝 xff1a 拷贝
  • windows下Linaro-arm-linux编译开启NEON的Opencv源码详细过程

    环境配置 1 windows安装cmake 下载地址 xff1a https cmake org download 按照上图中的下载cmake的安装包 xff0c 安装注意事项如下 xff1a 安装路径切记不能包含空格 xff01 xff0
  • Windows 工具之net

    net 是windows平台下用户管理的工具 xff0c 可以来添加 xff0c 删除和修改用户 1 创建用户 xff1a win 43 R 输入cmd xff0c 然后打开命令行窗口 xff0c 输入下面的命令 xff1a net use
  • windows平台相关命令收集

    1 获取IP地址命令 xff1a ipconfig 2 获取MAC地址命令 xff1a ipconfig all 3 查看所有端口信息 netstat nal findstr 8080
  • C/C++中比较好用的HTTP开源库

    以下是一些常用的C C 43 43 开源HTTP库及其对应的开源协议和链接 xff1a 1 libcurl xff1a 使用MIT X开源协议 xff0c 支持多种协议和多种数据传输方式 xff0c 被广泛应用于各种开发场景 官网链接 xf
  • 为Ubuntu网页设置稳定的数据隧道

    网站作为互联网世界中的一个个门户 xff0c 是我们对外表达的窗口 xff0c 无数个人和企业通过这个窗口传递着信息 xff0c 这个传递信息的窗口必须稳定持续存在 xff0c 才能让更多访客看到 xff0c 为我们带来更多的流量 而使用c
  • VisualStudio配置与Matlab混合编译问题总结

    VisualStudio配置与Matlab混合编译问题总结 写这篇博客记录一下自己在配置过程中遇到的种种问题 xff0c 也做一个小小总结 xff0c 试图把整个过程讲明白 xff0c 希望对面临同样问题的人有所帮助 网上此类教程很多 xf
  • C++与C的区别(作业)

    头文件的区别 1 C 43 43 中创建源文件后缀为 cpp xff0c C语言中创建源文件后缀为 c xff0c 头文件保持不变 xff0c 后缀为 h 2 包含自己的头文件 xff0c C语言与C 43 43 无区别 包含库目录 xff
  • C++和C语言区别(二)附二级指针的动态内存申请

    结构体区别 1 定义结构体与C语言一致 2 定义变量可省略关键字struct 3 C 43 43 结构体中允许函数存在 xff08 C 43 43 在没有写构造函数和权限限定的时候 xff0c 用法和C语言一致 xff09 xff08 1
  • 自制芯片 http转串口通信模块实现手机控制加红外控制51单片机实现双控制 28BYJ-48步进电机( 13003步进电机驱动器 STC89C52RC ESP8266)

    首先你有这样的8266 这种8266自身带2个按键和烧录芯片方便调试 xff0c 综合性价比较高 还需要有一个51单片机 或者其他单片机都行 还有就是需要这样的13003步进电机驱动器 43 28BYJ 48步进电机和红外接收头以及遥控器
  • C++类和对象(附C语言与C++链表)

    类和对象的基本概念 1 类 xff1a 一系列事物的抽象 xff0c 万物皆可为类 xff08 1 xff09 属性 xff1a 事物的特征 数据类型的描述 xff08 2 xff09 行为 xff1a 事物的操作 函数描述 2 对象 xf
  • C++类的组合

    1 以另一个类的对象为数据成员 xff0c 必须采用初始化参数列表的写法 include lt iostream gt include lt string gt using namespace std class Boy public Bo
  • C++模板

    函数模板 1 把类型当作未知量 2 语法 template lt typename Ty gt Ty function Ty a Ty b return a gt b a b template lt class Ty1 class Ty2
  • C++STL容器(一)

    定长数组 include lt iostream gt include lt string gt include lt array gt using namespace std template lt class Ty size t siz