CPP:动态数组vector是如此之简单,array显得不友好

#include<iostream>
#include <vector>//适用于任何数组
using namespace std;

//array  一旦分配  就无法变长
//vector分配的数组在堆上,适用于任何数据类类型  可以无限嵌套    收缩使用迭代器,动态数据增长     相当于数据结构中的线性表,可以实现线性存储
void main1()
{
	vector <int>myint{ 1, 2, 3, 4, 5, 6 };
	//遍历数组
	for (auto i :myint)
	{
		cout << i <<"    ";
	}
	cout << endl << endl;
	//插入数据   动态增长
	for (int i = 10; i < 20; i++)
	{
		myint.push_back(i);//压入数据  尾部增长
	}
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	for (int i = 0; i < myint.size(); i++)//myint.size()元素个数
	{
		cout << myint[i] << "   " << myint.at(i) << endl;//效果一样
	}

	//正向迭代器的本质是指针
	for (auto ib = myint.begin(), ie = myint.end(); ib != ie;ib++)//ib是指针
	{
		cout <<*ib<< "    ";
	}
	cout << endl << endl;

	//反向迭代器
	for (auto rb = myint.rbegin(), re = myint.rend(); rb != re; rb++)//此处是rbegin和rend
	{
		cout <<*rb<< "    ";
	}
	cout << endl << endl;

	cout <<"第一个:"<< myint.front() << endl;//输出第一个
	cout <<"最后一个:"<< myint.back() << endl;;//取出最后一个
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000421545?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

//动态调整vector大小
void main2()
{
	vector <int>myint{ 1, 2, 3, 4 };
	//遍历数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	for (int i = 10; i < 18; i++)
	{
		myint.push_back(i);
	}
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	myint.resize(5);//重新调整数组大小为5个元素,截取数组元素的前5个    等价于动态realloc
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	myint.resize(9, 99);//只有8个数据   原有数组的基础上添加数组元素个数为8个     缓冲为99
	for (auto i : myint)//如果指定数据个数小于原始数组个数  效果等价于一个参数的resize
	{
		cout << i << "    ";
	}
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000444167?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


//重新初始化assign
void main3()
{
	vector <int>myint{ 1, 2, 3, 4 };
	//遍历数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	//重新初始化
	myint.assign(7, 888);//初始化数组为  7个888
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000503196?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



//插入
void main4()
{
	vector <int>myint{ 1, 2, 3, 4 };
	//遍历数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;
	auto it = myint.begin() + 3;//在数组的第四个位置插入数据
	it = myint.insert(it, 8888);//根据位置插入数据
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout <<endl<< *it << endl;
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000519051?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


//插入多个   挨个遍历每一个位置
void main5()
{
	vector <int>myint{ 1, 2, 3, 4 };
	vector<int>myintX{ 33, 55, 88, 99 };
	//遍历数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;

	auto it = myint.begin() + 3;//从下标3处开始插入一个数组,下标为3位置上的数据及其以后的数据往后挪
	int a[5] = { 188, 199, 288, 299, 888888 };
	it = myint.insert(it, a, a + 5);//插入一个数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}

	//批量插入
	cout << endl << endl;
	//it = myint.insert(it, myint.begin(), myint.end());

	it = myint.insert(it, myintX.begin(), myintX.end());
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000534554?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


//删除
void main6()
{
	vector <int>myint{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	//遍历数组
	for (auto i : myint)
	{
		cout << i << "    ";
	}

	cout << endl << endl;
	auto it = myint.begin() + 3;
	//单个删除
	myint.erase(it);//删除第四个(下标为3)元素
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cout << endl << endl;
	//批量删除
	myint.erase(myint.begin(), myint.begin() + 3);
	for (auto i : myint)
	{
		cout << i << "    ";
	}
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000556735?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



//归并 分配
void main7()
{
	vector <int>myint1{ 1, 2, 3, 4,5,6,7 };
	vector <int> myint2{ 188, 288, 388, 499 };

	myint1.swap(myint2);//交换两数组中的每一个元素
	//遍历数组
	cout << "myint1:"<< endl;
	for (auto i : myint1)
	{
		cout << i << "    ";
	}
	cout << endl << "myint2:" << endl;
	for (auto i : myint2)
	{
		cout << i << "    ";
	}
	cin.get();
}
<img src="https://img-blog.csdn.net/20150822000615731?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


//测试vector  分配的内存在栈上还是在堆上     分配的数组在内存中也是连续的
void main8()
{
	vector <double>mydb;
	for (int i = 0; i < 1000000; i++)//由于vector分配的内存在堆上  所以不会溢出
	{
		mydb.push_back(i);
	}
	cin.get();
}



//测试  vector分配的数组  在内存中是否连续
void main9()
{
	vector <double>mydb{12,34,56,78};
	for (int i = 0; i < 10; i++)
	{
		cout << mydb[i] <<"     "<< &mydb[i] << endl;
	}
	cin.get();
}



//允许自动管理内存
void main10()
{
	vector<int>myint;//不分配内存
	int *p = myint.get_allocator().allocate(5);//获取分配器分配内存
	cout << myint.size() << endl << endl;

	for (int i = 0; i < 5; i++)
	{
		cout << (p[i] = i)<<endl;//调用内部分配器  分配内存
	}
	myint.get_allocator().deallocate(p, 5);//释放内存
	cin.get();
}



//vector 实现锯齿数组
void main11()
{
	vector<int>myint1{ 1, 3, 5, 6, 8, 9 };
	vector<int>myint2{ 11, 31, 51, 16, 81, 91 8, 3, 5, 2, 3, 8, 0, 5 };
	vector<int>myint3{ 10, 3, 50, 60, 8, 9 };
	vector<int>myint4{ 1, 30, 5, 6, 80, 9 };

	vector<vector<int>>myallint{ myint1, myint2, myint3, myint4 };
	for (auto i:myallint)
	{
		for (auto j:i)
		{
			cout << j << "    ";
		}
		cout << endl;
	}
	cin.get();
}


更多资料 · 微信公众号搜索【CTO Plus】关注后,获取更多,我们一起学习交流。

关于公众号的描述访问如下链接


关于Articulate“做一个知识和技术的搬运工。做一个终身学习的爱好者。做一个有深度和广度的技术圈。”一直以来都想把专业领域的技https://mp.weixin.qq.com/s/0yqGBPbOI6QxHqK17WxU8Q

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveRocket

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值