199_vector容器-容量和大小

在这里插入图片描述

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

void test01()
{
    
    
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty())
	{
    
    
		cout << "v1 is empty" << endl;
	}
	else
	{
    
    
		cout << "v1 is not empty" << endl;
		cout << "v1 capacity is" << v1.capacity() << endl;
		cout << "v1 size is" << v1.size() << endl;
	}

	v1.resize(15, 100);
	printVector(v1);
}



int main()
{
    
    	
	test01();
}

猜你喜欢

转载自blog.csdn.net/weixin_42990464/article/details/124567605