vector一些用法

参考来源:http://blog.youkuaiyun.com/duan19920101/article/details/50617190/

定义: vector<type> test;
在结尾插入:test.push_back();
打印出该动态数组的大小:test.size();
得到开始和结尾迭代器:test.begin(); test.end();
定义迭代对象:vector<type>::iterator it;
指定位置插入:在迭代器iterator前插入num个data test.insert(iterator,num,data);
/ 在iterator前插入[itb,ine)的数据 test.insert(iterator, itb, ine)

清除:test.clear();
删除某个:test.erase(iterator);
得到数组头的值/数组尾值:test.back();


索引:1.test.at(3) = test[3] = vector<type>:iterator *(it+3)
#include<iostream>
#include<vector>
using namespace std;
vector<int> test;

int main()
{
	vector<int>::iterator it;
	int a;
	test.push_back(1);
	test.push_back(2);
	//test.insert(test.begin(),test.begin(),test.end());//print 1 2 1 2
	//test.insert(test.begin()+2,4,5);//print 1 2 5 5 5

	//cout << test.size()<<" ";//print 2
	//test.erase(test.begin());//print 2
	//test.clear();//print 
	//a = test.back(); //print 2
	
	cout << a <<"\n";
	for(it = test.begin();it < test.end();it++)
	{
		cout << *it << " ";
	}
	getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值