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