当前位置: 首页 > article >正文

C++中vector类的使用

目录

1.vector类常用接口说明

1.1默认成员函数

1.1.1构造函数(constructor)

1.1.2 赋值运算符重载(operator=())

2. vector对象的访问及遍历操作(Iterators and Element access)

3.vector类对象的容量操作(Capacity)

4. vector类对象的修改及相关操作(Modifiers and String operations)

5. 使用vector存储string对象以及实现二维数组


    C++中的vector对应与C语言中的顺序表,底层还是通过数组来存储数据的。可以参考用C语言实现顺序表。vector和string不一样的是vector是类模板,类模板只能显式实例化。vector是STL中一种重要的数据结构。C++之所以设计STL就是为了统一各种数据结构的接口,所以下面介绍的vector的接口在使用上与string等其他数据结构具有相同的用法。

#include <iostream>
#include <vector>

using namepsace std;

int main()
{
    vector<int> v1;    //类模板只能显示实例化
    return 0;
}

1.vector类常用接口说明

        vector类的接口我按照C++函数网址进行介绍,这里只进行常用接口的介绍,其他接口、类中的函数参数和函数重载若有需要请参考该网址,下列介绍就不一一列出了。vector的接口和string的接口很相似,可以参考C++中string类的使用进行对比。

1.1默认成员函数

1.1.1构造函数(constructor)

        这里的默认构造其实和string类类似,这里就不一一说明了。

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

void test_vector1()
{
	//1.default
	vector<int> v1;	//size == 0 capacity == 0
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	//2.fill
	vector<int> v2(10, 1);	//用n个值进行初始化
	for (auto e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	//3.range
	vector<int> v3(++v2.begin(), --v2.end());	//用迭代器区间进行构造

	vector<int>::iterator it = v3.begin();	//通过迭代器进行遍历
	while (it != v3.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//4.copy
	vector<int> v4 = v2;
	for (auto e : v4)
	{
		cout << e << " ";
	}
	cout << endl;

	//5.initializer list
	vector<int>v5 = { 0,3,5,6,9,3,0 };
	for (auto e : v5)
	{
		cout << e << " ";
	}
	cout << endl;
}

int main()
{
    test_vector1();
    return 0;
}

1.1.2 赋值运算符重载(operator=())

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

void test_vector2()
{
	vector<int> v1(10, 1);

	//1.copy
	vector<int> v2;
	v2 = v1;
	for (auto& e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	//1.initializer list
	vector<int> v3;
	v3 = { 1,2,3,4,5,6 };
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
}

int main()
{    
    test_vector2();
    return 0;
}

2. vector对象的访问及遍历操作(Iterators and Element access)

         vector对象的访问及遍历操作和string基本上是一模一样的,并且两个数据结构的底层都是通过数组进行实现的,参考C++中string类的使用即可。

3.vector类对象的容量操作(Capacity)

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

void TestVectorExpand()
{
	//vs下是1.5倍扩容,g++下是两倍扩容
	size_t sz;
	vector<int> v;
	//v.reserve(99);	最少开n个
	sz = v.capacity();
	cout << "making v grow:\n";
	cout << "capacity changed: " << sz << "\n";
	for (int i = 0; i < 100; ++i)
	{
		v.push_back(i);
		if (sz != v.capacity())
		{
			sz = v.capacity();
			cout << "capacity changed: " << sz << "\n";
		}
	}
}

void test_vector3()
{
	//1.size
	//2.capacity
	//3.empty
	vector<int> v1(10, 1);
	cout << v1.size() << endl;
	cout << v1.capacity() << endl;
	cout << v1.empty() << endl;
	v1.clear();
	cout << endl;
	cout << v1.size() << endl;
	cout << v1.capacity() << endl;
	cout << v1.empty() << endl;

	cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
	TestVectorExpand();

	//不缩容,不改变size
	vector<int> v2(10, 1);
	v2.reserve(20);
	cout << v2.size() << endl;
	cout << v2.capacity() << endl;
	cout << endl;

	v2.reserve(15);
	cout << v2.size() << endl;
	cout << v2.capacity() << endl;
	cout << endl;

	v2.reserve(5);
	cout << v2.size() << endl;
	cout << v2.capacity() << endl;
	cout << endl;

	cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;

	vector<int> v3(10, 1);
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << v3.size() << endl;
	cout << v3.capacity() << endl;
	cout << endl;
	//vs不缩容,如果小于n < size,则缩到n,如果size < n < capacity,把size变为n, 如果n > capacity则扩容之后把size变为n
	v3.resize(15, 2);
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << v3.size() << endl;
	cout << v3.capacity() << endl;
	cout << endl;

	v3.resize(25, 3);
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << v3.size() << endl;
	cout << v3.capacity() << endl;
	cout << endl;

	v3.resize(5);
	for (auto& e : v3)
	{
		cout << e << " ";
	}
	cout << endl;
	cout << v3.size() << endl;
	cout << v3.capacity() << endl;
}

int main()
{
    test_vector3();
    return 0;
}

4. vector类对象的修改及相关操作(Modifiers and String operations)

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

void test_vecotr4()
{
	vector<int> v(10, 1);
	v.push_back(2);
	v.insert(v.begin(), 5);

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.insert(v.begin() + 3, 3);
	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.pop_back();
	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.erase(v.begin(), v.begin() + 3);
	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

int main()
{
    test_vector4();
    return 0;
}

5. 使用vector存储string对象以及实现二维数组

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

void test_vector5()
{
	vector<string> v1;
	string s1 = "xxxxx";
	v1.push_back(s1);

	v1.push_back("yyyyy");	//隐式类型转换
	for (auto& e : v1)		
	{
		cout << e << " ";
	}
	cout << endl;

	//二维数组,初始化一个10*5的二维数组
	vector<int> v(5, 1);	//初始化行
	vector<vector<int>> vv(10, v);    //初始化列
	vv[2][1] = 2;
	for (size_t i = 0; i < vv.size(); i++)
	{
		for (size_t j = 0; j < vv[i].size(); j++)
		{
			cout << vv[i][j] << " ";
		}
		cout << endl;
	}
}

int main()
{    
    test_vector5();
    return 0;
}

 

 


http://www.kler.cn/news/326164.html

相关文章:

  • Google Tag Manager - 服务器端代码植入
  • Python与SQL Server数据库结合导出Excel并做部分修改
  • ElasticSearch安装分词器与整合SpringBoot
  • 【制作自解压程序】使用7Z制作自解压程序
  • OceanBase技术解析:自适应分布式下压技术
  • 【软件整理资料】软件项目配套资料,项目计划书(word)
  • IDEA使用技巧和插件推荐
  • 爬虫及数据可视化——运用Hadoop和MongoDB数据进行分析
  • js中的深拷贝与浅拷贝 手写深拷贝代码
  • 深入剖析 Android Lifecycle:构建高效稳定的应用
  • 如何设计能吸引下载的截图以及注意事项
  • SpringBoot助力墙绘艺术市场创新
  • golang学习笔记16-数组
  • java 解析excel (本地资源)
  • Android常用C++特性之std::find_if
  • CF1619D.New Year‘s Problem
  • 解决 TypeError: Expected state_dict to be dict-like, , got <class ‘*‘>.
  • Acwing 最小生成树
  • 每日OJ题_牛客_NC40链表相加(二)_链表+高精度加法_C++_Java
  • 《黑神话:悟空》天命人速通法宝 | 北通鲲鹏20智控游戏手柄评测
  • linux打开桌面软件(wps)、获取已打开的文件名(wps)
  • Ini文件读写配置工具类 - C#小函数类推荐
  • 汽车免拆诊断案例 | 2016 款宾利GT车仪表盘上的多个故障灯点亮
  • 使用TensorFlow实现一个简单的神经网络:从入门到精通
  • 动手学深度学习(李沐)PyTorch 第 3 章 线性神经网络
  • TiDB 性能测试的几个优化点
  • Leetcode热题100-438 找出字符串中所有字母异位数
  • R语言非参数回归预测摩托车事故、收入数据:局部回归、核回归、LOESS可视化...
  • 408算法题leetcode--第19天
  • java通过webhook给飞书发送群消息