C++:vector应用

vector ()
vector (size_t n,const T& val=T())
vector (Tterator first,Tterator last)
vector (const vector& v)
vector < int > v{1,2,3,4};

void TestVector01()
{
	//构造
	vector<int> v1;
	vector<int> v2(10, 5);//10个5

	int array[] = { 1,2,3,4,5 };
	vector<int> v3(array, array + sizeof(array) / sizeof(array[0]));

	string s("hello");
	vector<char> v4(s.begin(), s.end());

	vector<int> v5(v3);

	//遍历
	for (int i = 0; i < v2.size(); i++)
	{
		cout << v2[i] << " ";
	}
	cout << endl;
	for (auto e : v3)
	{
		cout << e << " ";
	}
	cout << endl;

	//vector<char>::iterator it = v4.begin();
	auto it = v4.begin();
	while (it != v4.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//vector<char>::reverse_iterator rit = v5.rbegin();
	auto rit = v5.rbegin();
	while (rit != v5.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	vector<int> v6{ 1,2,3,4,5,6 };//列表方式构造

}

void resize (size_t newsize,const T& val = T());
vector中旧元素个数为oldsize,当前容量为capacity
元素增多时:
1.多出元素使用val填充
2.newszie>capacity时,resize需要扩容
元素减少时:
只将元素个数减少,底层空间capacity大小不变

void TestVector02()
{
	vector<int> v{ 1,2,3,4,5 };
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.reserve(20);
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(10, 6);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(30, 8);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(50);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(40);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(20);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;

	v.resize(10);
	for (auto e : v)
		cout << e << " ";
	cout << endl;
	cout << v.size() << endl;
	cout << v.capacity() << endl;
}

void reserve (size_t newcapacity)
只将空间增大,不会缩小空间
不修改有效元素

void TestVector03()
{
	vector<int> v{ 1,2,3,4,5 };
	cout << v.size() << " " << v.capacity() << endl;
	v.reserve(10);
	cout << v.size() << " " << v.capacity() << endl;

	v.reserve(20);
	cout << v.size() << " " << v.capacity() << endl;
	v.reserve(30);
	cout << v.size() << " " << v.capacity() << endl;
	v.reserve(40);
	cout << v.size() << " " << v.capacity() << endl;
	v.reserve(20);
	cout << v.size() << " " << v.capacity() << endl;
	v.reserve(10);
	cout << v.size() << " " << v.capacity() << endl;
}

push_back()尾插
vector扩容:vs中按照1.5倍进行扩容, 避免,一边插入一边扩容,效率低

void TestVector05()
{
	vector<int> v;
	v.reserve(100);
	int cap = v.capacity();
	for (int i = 0; i < 100; i++)
	{
		v.push_back(i);//尾插   一边插入一边扩容
		if (cap != v.capacity())
		{
			cap = v.capacity();
			cout << cap << " ";
		}
	}
}

void TestVector06()
{
	vector<int> v;
	v.reserve(10);
	for (int i = 0; i < 10; i++)
		v.push_back(i);
	//在首位插入100
	v.insert(v.begin(), 100);
	//在第一个5的位置插入10个8
	auto pos = find(v.begin(), v.end(), 5);
	if (pos != v.end())
		v.insert(pos, 10, 8);

	int array[] = { 100,200,300 };
	//将数组插入vector末尾
	v.insert(v.end(), array, array + sizeof(array) / sizeof(array[0]));

	//消除
	v.erase(v.begin());//消除首位元素
	v.erase(v.begin(), v.begin() + 5);//消除前5个元素
	v.clear();
}

迭代器失效:
1.所有扩容可能会导致迭代器失效(旧空间释放)
插入push_back(data) insert(v.begin(),data) reserve(newcapacity assign(v.begin(),v.begin()+data))
2.删除指定元素
erase(pos):删除之后pos进行后序的元素都失效

失效后果:程序崩溃

预防:
在所有可能导致迭代器失效操作之后,需要给迭代器重新赋值

//扩容迭代器失效
void TestVector07()
{
	vector<int> v{ 1,2,3,4,5 };
	auto it = v.begin();
	
	//v.insert(v.begin(),10,5);//在起始位置插入10个5
	//v.resize(20,8);
	v.reserve(20);
	//在所有可能导致迭代器失效操作之后,需要给迭代器重新赋值
	it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
//删除迭代器失效
void TestVector08()
{
	vector<int> v{ 1,2,3,4,5,6 };

	auto it = find(v.begin(), v.end(), 5);//找到值为5元素删除
	if (it != v.end())
		//eraes的返回值:返回删除位置的新元素(删除元素的下一个元素)
		it = v.erase(it);

	cout << *it << endl;
}

void TestVector09()
{
	vector<int> v{ 1,2,3,4,5,6,7,8,9,2 };
	auto it = v.begin();
	while (it != v.end())
	{
		if (*it == 2)
			v.erase(it);
		else
			it++;
	}
}

构造二维数组

void TestVector()
{
	//5行6列
	vector<vector<int>> vv;
	vv.resize(5);
	for (int i = 0; i < 5; i++)
	{
		vv[i].resize(6, 8);
	}

	vector<vector<int>> vv2;
	vv2.resize(5, vector<int>(6, 8));

	vector<vector<int>> vv3(5, vector<int>(6, 8));
}

杨辉三角
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

void TestVectorYang(int n)
{
	vector<vector<int>> vv(n);
	for (int i = 0; i < vv.size(); i++)
	{
		vv[i].resize(i + 1, 1);

		//修改每行元素:非0列  以及  对角线上的元素
		for(int j = 1; j < i; j++)
		{
			vv[i][j] = vv[i - 1][j] + vv[i - 1][j - 1];
		}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值