C++ 提高教程 STL vector容器-预留空间

 

# include<iostream>
#include<vector>
# include<algorithm>
# include<string>
using namespace std;

void printVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//基本使用
void test01()
{
	vector<int>v1;
	int num = 0;//统计其开辟次数
	int* p = NULL;
	//利用reserve来预留空间
	v1.reserve(10000);
	for (int i = 0; i < 10000;i++)
	{
		v1.push_back(i);

		if (p != &v1[0])
		{
			p = &v1[0];
			num++;
		}

	}
	cout << num << endl;
	//printVector(v1);
}



int main()
{
	test01();
	system("pause");
	return 0;
}
全部评论

相关推荐

Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务