💻
C++
  • C++学习指南
  • 第一章 基础入门
    • 1 C++初识
      • 1.1 Visual Studio 下载及安装
      • 1.2 第一个C++程序
      • 1.3 注释
      • 1.4 变量
      • 1.5 常量
      • 1.6 关键字
      • 1.7 标识符命名规则
  • 2 数据类型
    • 2.1 整型
    • 2.2 sizeof关键字
    • 2.3 实型(浮点型)
    • 2.4 字符型
    • 2.5 转义字符
    • 2.6 字符串型
    • 2.7 布尔类型 bool
    • 2.8 数据的输入
  • 3 运算符
    • 3.1 算术运算符
    • 3.2 赋值运算符
    • 3.3 比较运算符
    • 3.4 逻辑运算符
  • 4 程序流程结构
    • 4.1 选择结构
    • 4.2 循环结构
    • 4.3 跳转语句
  • 5 数组
    • 5.1 概述
    • 5.2 一维数组
    • 5.3 二维数组
  • 6 函数
    • 6.1 概述
    • 6.2 函数的定义
    • 6.3 函数的调用
    • 6.4 值传递
    • 6.5 函数的常见样式
    • 6.6 函数的声明
    • 6.7 函数的分文件编写
  • 7 指针
    • 7.1 指针的基本概念
    • 7.2 指针变量的定义和使用
    • 7.3 指针所占内存空间
    • 7.4 空指针和野指针
    • 7.5 const修饰指针
    • 7.6 指针和数组
    • 7.7 指针和函数
    • 7.8 指针、数组、函数
  • 8 结构体
    • 8.1 结构体基本概念
    • 8.2 结构体定义和使用
    • 8.3 结构体数组
    • 8.4 结构体指针
    • 8.5 结构体嵌套结构体
    • 8.6 结构体做函数参数
    • 8.7 结构体中const使用场景
    • 8.8 结构体案例
  • 第二章 核心编程
    • 9 内存分区模型
      • 9.1 程序运行前
      • 9.2 程序运行后
      • 9.3 new操作符
    • 10 引用
      • 10.1 引用的基本使用
      • 10.2 引用的注意事项
      • 10.3 引用做函数参数
      • 10.4 引用做函数返回值
      • 10.5 引用的本质
      • 10.6 常量的引用
    • 11 函数提高
      • 11.1 函数默认参数
      • 11.2 函数占位参数
      • 11.3 函数重载
    • 12 类和对象
      • 12.1 封装
      • 12.2 对象的初始化和清理
      • 12.3 C++对象模型和this指针
      • 12.4 友元
      • 12.5 运算符重载
      • 12.6 继承
      • 12.7 多态
    • 13 文件操作
      • 13.1 文本文件
      • 13.2 二进制文件
  • 第三章 提高编程
    • 14 模板
      • 14.1 模板的概念
      • 14.2 函数模板
      • 14.3 类模板
    • 15 STL初识
      • 15.1 STL的诞生
      • 15.2 STL基本概念
      • 15.3 STL六大组件
      • 15.4 STL中容器、算法、迭代器
      • 15.5 容器算法迭代器初识
    • 16 STL常用容器
      • 16.1 string容器
      • 16.2 vector容器
      • 16.3 deque容器
      • 16.4 评委打分案例
      • 16.5 stack容器
      • 16.6 queue容器
      • 16.7 list容器
      • 16.8 set/multiset容器
      • 16.9 map/multimap容器
      • 16.10 员工分组案例
    • 17 STL函数对象
      • 17.1 函数对象
      • 17.2 谓词
      • 17.3 内建函数对象
    • 18 STL常用算法
      • 18.1 常用遍历算法
      • 18.2 常用查找算法
      • 18.3 常用排序算法
      • 18.4 常用拷贝和替换算法
      • 18.5 常用集合算法
      • 18.6 常用算法生成算法
由 GitBook 提供支持
在本页
  • 1.vector基本概念
  • 2.vector构造函数
  • 5.vector插入和删除
  • 6.vector数据存取
  • 7.vector互换容器
  • 8.vector预留空间

这有帮助吗?

  1. 第三章 提高编程
  2. 16 STL常用容器

16.2 vector容器

1.vector基本概念

功能:

  • vector数据结构和数组非常相似,也称为单端数组

vector与普通数组区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

  • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间

  • vector容器的迭代器是支持随机访问的迭代器

2.vector构造函数

功能描述:

  • 创建vector容器

函数原型:

  • vector<T> v; //采用模板实现类实现,默认构造函数

  • vector(v.begin(), v.end()); //将v[begin(), end())区间中的元素拷贝给本身。

  • vector(n, elem); //构造函数将n个elem拷贝给本身。

  • vector(const vector &vec); //拷贝构造函数。

示例:

#include <vector>

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; //无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int> v2(v1.begin(), v1.end());
	printVector(v2);

	vector<int> v3(10, 100);
	printVector(v3);
	
	vector<int> v4(v3);
	printVector(v4);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:vector的多种构造方式没有可比性,灵活使用即可

3.vector赋值操作

功能描述:

  • 给vector容器进行赋值

函数原型:

  • vector& operator=(const vector &vec);//重载等号操作符

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

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

示例:

#include <vector>

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; //无参构造
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2;
	v2 = v1;
	printVector(v2);

	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	vector<int>v4;
	v4.assign(10, 100);
	printVector(v4);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结: vector赋值方式比较简单,使用operator=,或者assign都可以

4.vector容量和大小

功能描述:

  • 对vector容器的容量和大小操作

函数原型:

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

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

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

  • resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除。

  • resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除

示例:

#include <vector>

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;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);
	if (v1.empty())
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空" << endl;
		cout << "v1的容量 = " << v1.capacity() << endl;
		cout << "v1的大小 = " << v1.size() << endl;
	}

	//resize 重新指定大小 ,若指定的更大,默认用0填充新位置,可以利用重载版本替换默认填充
	v1.resize(15,10);
	printVector(v1);

	//resize 重新指定大小 ,若指定的更小,超出部分元素被删除
	v1.resize(5);
	printVector(v1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

  • 判断是否为空 — empty

  • 返回元素个数 — size

  • 返回容器容量 — capacity

  • 重新指定大小 — resize

5.vector插入和删除

功能描述:

  • 对vector容器进行插入、删除操作

函数原型:

  • 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(); //删除容器中所有元素

示例:

#include <vector>

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;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	printVector(v1);
	//尾删
	v1.pop_back();
	printVector(v1);
	//插入
	v1.insert(v1.begin(), 100);
	printVector(v1);

	v1.insert(v1.begin(), 2, 1000);
	printVector(v1);

	//删除
	v1.erase(v1.begin());
	printVector(v1);

	//清空
	v1.erase(v1.begin(), v1.end());
	v1.clear();
	printVector(v1);
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

  • 尾插 — push_back

  • 尾删 — pop_back

  • 插入 — insert (位置迭代器)

  • 删除 — erase (位置迭代器)

  • 清空 — clear

6.vector数据存取

功能描述:

  • 对vector中的数据的存取操作

函数原型:

  • at(int idx); //返回索引idx所指的数据

  • operator[]; //返回索引idx所指的数据

  • front(); //返回容器中第一个数据元素

  • back(); //返回容器中最后一个数据元素

示例:

#include <vector>

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

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;

	cout << "v1的第一个元素为: " << v1.front() << endl;
	cout << "v1的最后一个元素为: " << v1.back() << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

  • 除了用迭代器获取vector容器中元素,[ ]和at也可以

  • front返回容器第一个元素

  • back返回容器最后一个元素

7.vector互换容器

功能描述:

  • 实现两个容器内元素进行互换

函数原型:

  • swap(vec); // 将vec与本身的元素互换

示例:

#include <vector>

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;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	vector<int>v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	printVector(v2);

	//互换容器
	cout << "互换后" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);
}

void test02()
{
	vector<int> v;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
	}

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	v.resize(3);

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;

	//收缩内存
	vector<int>(v).swap(v); //匿名对象

	cout << "v的容量为:" << v.capacity() << endl;
	cout << "v的大小为:" << v.size() << endl;
}

int main() {

	test01();

	test02();

	system("pause");

	return 0;
}

总结:swap可以使两个容器互换,可以达到实用的收缩内存效果

8.vector预留空间

功能描述:

  • 减少vector在动态扩展容量时的扩展次数

函数原型:

  • reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问。

    ​

示例:

#include <vector>

void test01()
{
	vector<int> v;

	//预留空间
	v.reserve(100000);

	int num = 0;
	int* p = NULL;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
		if (p != &v[0]) {
			p = &v[0];
			num++;
		}
	}

	cout << "num:" << num << endl;
}

int main() {

	test01();
    
	system("pause");

	return 0;
}

总结:如果数据量较大,可以一开始利用reserve预留空间

上一页16.1 string容器下一页16.3 deque容器

最后更新于4年前

这有帮助吗?