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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值