C++STL——vector容器和大小操作
功能描述:
對vector容器和大小操作
函數原型:
empty(); //判斷容器是否為空
capacity();//容器的容量
size(); //返回容器指定長度為num,若容器變長,則以默認值填充新位置。
//如果容器變短,則末尾超出容器長度的元素被刪除
resize(int num, elem);//重新指定容器的長度為num,若容器變長,則以elem值填 充新位置。
//如果容器變短,則末尾超出容器長度的元素被刪除
代碼示例:
#include<iostream>
#include<vector>
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器和大小
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;
}
//重新制定大小
v1.resize(15,88); //利用重載版本,可以指定默認值填充,參數2
printVector(v1);//如果重新制定的比原來長了,默認用0填充新的位置
v1.resize(5);
printVector(v1);//如果重新指定比原來小,后面的數字會刪掉
}
int main()
{
test01();
return 0;
}
總結:
判斷是否為空—empty
返回元素個數—size
返回容器容量–capacity
重新指定大小–resize
vector數據存取
功能描述:
對vector中的數據的存取操作
函數原型:
at(int idx); //返回索引idx所指的數據
operator[]; //返回索引idx所指的數據
front(); //返回容器中第一個數據元素
back(); //返回容器中最后一個數據元素
代碼示例:
#include<iostream>
#include<vector>
using namespace std;
//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;
//利用at方式訪問元素
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
//獲取第一個元素
cout << "第一個元素為:" << v1.front() << endl;
//獲取最后一個元素
cout << "最后一個元素為:" << v1.back() << endl;
}
int main()
{
test01();
return 0;
}
總結:
除了用迭代器獲取vector容器中元素,【】和at也可以
front返回容器第一個元素
back返回容器最后一個元素
vector互換容器
功能描述:
實現兩個容器內元素進行互換
函數原型:
swap(vec); //將vec與本身的元素互換
代碼示例:
#include<iostream>
#include<vector>
using namespace std;
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//1.基本使用
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout << "交換前:" << endl;
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);
}
//2.實際用途
//巧用swap可以收縮內存空間
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;
//巧用swap收縮內存
vector<int>(v).swap(v);
cout << "v的容量為:" << v.capacity() << endl;
cout << "v的大小為:" << v.size() << endl;
}
int main()
{
test01();
test02();
return 0;
}
總結:swap可以使兩個容器互換,可以達到使用的收縮內存效果
vector預留空間
功能描述:
減少vector在動態擴展容量時的擴展次數
函數原型:
reserve(int len); //容器預留len個長度,預留位置不初始化,元素不可訪問。
代碼示例:
#include<iostream>
#include<vector>
using namespace std;
//vector預留空間
void test01()
{
vector<int>v;
//利用reserver預留空間
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();
return 0;
}
總結:如果數據量較大,可以一開始利用reserver預留空間