vector容器

1.头文件与命令空间的应用

#include <vector>
using std::vector;

2.遍历方式

#include <vector>
#include <iostream>
 
using namespace std;
 
struct Point
{
	double x;
	double y;
	Point()
	{
		x = 0;
		y = 0;
	}
};
 
 
int main()
{
	vector<Point> m_testPoint;
	m_testPoint.clear();
	m_testPoint.shrink_to_fit();    // 减少容器的容量以适应其大小并销毁超出容量的所有元素。 
 
	for (int i = 0; i<10; ++i)
	{
		Point temp;
		temp.x = i*i;
		temp.y = i*i;
		m_testPoint.push_back(temp);
	}
 
	// 第一种遍历方式,下标
	cout << "第一种遍历方式,下标访问" << endl;
	for (int i = 0; i<m_testPoint.size(); ++i)
	{
 
		cout << m_testPoint[i].x << "	" << m_testPoint[i].y << endl;
	}
 
	// 第二种遍历方式,迭代器
	cout << "第二种遍历方式,迭代器访问" << endl;
	for (vector<Point>::iterator iter = m_testPoint.begin(); iter != m_testPoint.end(); iter++)
	{
		cout << (*iter).x << "	" << (*iter).y << endl;
	}
 
	// 第三种遍历方式,auto关键字
	cout << "C++11,第三种遍历方式,auto关键字" << endl;
	for (auto iter = m_testPoint.begin(); iter != m_testPoint.end(); iter++)
	{
		cout << (*iter).x << "	" << (*iter).y << endl;
	}
 
	// 第四种遍历方式,auto关键字的另一种方式
	cout << "C++11,第四种遍历方式,auto关键字" << endl;
	for (auto i : m_testPoint)
	{
		cout << i.x << "	" << i.y << endl;
	}
 
	return 0;
}

3.vector常用的成员函数

  • vector<T> v;                                                 // 采用模板实现类实现,默认构造函数
  • vector(v.begin(), v.end());                      // 将v[begin(), end())区间中的元素拷贝给本身。
  • vector(n, elem);                          // 构造函数将n个elem拷贝给本身。
  • vector(const vector &vec);                        // 拷贝构造函数。
  • vector& operator=(const vector &vec); // 重载等号操作符

  • assign(beg, end);                        // 将[beg, end)区间中的数据拷贝赋值给本身。

  • assign(n, elem);                          // 将n个elem拷贝赋值给本身。

  • empty();                                        // 判断容器是否为空

  • capacity();                                   // 容器的容量

  • size();                                          // 返回容器中元素的个数

  • resize(int num);                          // 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。//如果容器变短,则末尾超出容器长度的元素被删除。

  • resize(int num, elem);      // 重新指定容器的长度为num,若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除

  • push_back(ele);                            // 尾部插入元素ele
  • pop_back();                                   // 删除最后一个元素
  • insert(const_iterator pos, ele);                           // 迭代器指向位置pos插入元素ele
  • insert(const_iterator pos, int count,ele);   // 指向位置pos插入count个元素ele
  • erase(const_iterator pos);                                       // 删除迭代器指向的元素
  • erase(const_iterator start, const_iterator end);// 删除迭代器从start到end之间的元素
  • clear();                                // 删除容器中所有元素
  • at(int idx);                         // 返回索引idx所指的数据
  • operator[];                          // 返回索引idx所指的数据
  • front();                               // 返回容器中第一个数据元素
  • back();                                 // 返回容器中最后一个数据元素
  • swap(vec);          // 将vec与本身的元素互换
  • reserve(int len);     // 容器预留len个元素长度,预留位置不初始化,元素不可访问。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值