C++ STL容器之Vector詳解
Vector簡介
vector數(shù)據(jù)結(jié)構(gòu)和數(shù)組非常相似,也稱為單端數(shù)組。
vector與普通數(shù)組區(qū)別: 不同之處在于數(shù)組是靜態(tài)空間,而vector可以動態(tài)擴展
動態(tài)擴展: 并不是在原空間之后續(xù)接新空間,而是找更大的內(nèi)存空間,然后將原數(shù)據(jù)拷貝新空間,釋放原空間
vector容器的迭代器是支持隨機訪問的迭代器
Vector用法
vector存放內(nèi)置數(shù)據(jù)類型
- 容器: vector
- 算法: for_each
- 迭代器: vector::iterator
構(gòu)造函數(shù)
vector(): 創(chuàng)建一個空vector
vector(int nSize): 創(chuàng)建一個vector,元素個數(shù)為nSize
vector(int nSize,const t& t): 創(chuàng)建一個vector,元素個數(shù)為nSize,且值均為t
vector(const vector&): 復(fù)制構(gòu)造函數(shù)
vector(begin,end): 復(fù)制[begin,end)區(qū)間內(nèi)另一個數(shù)組的元素到vector中
實例:
//構(gòu)造函數(shù)
vector<int> vecInt;
struct Node{
int x, y, step;
char s;
}
vector<Node> vecNode;
vector插入與刪除操作
函數(shù)原型:
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(); //刪除容器中所有元素
//構(gòu)造函數(shù)
vector<int> vecInt;
int n= 123;
vecInt.push_back(n);
1.插入位置,插入值
iterator insert(iterator __position, const value_type& __x);
2.插入位置,插入數(shù)量,插入值
void insert(iterator __position, size_type __n, const value_type& __x);
3.插入位置,迭代器開始位,迭代器結(jié)束位
template<typename _InputIterator>
void insert(iterator __position, _InputIterator __first, _InputIterator __last)
c.clear()移除容器中的所有數(shù)據(jù)。
vector<int>::iterator it;
for(it = v.begin();it!=v.end();it++){
cout << *it << "\t";
}
v.clear();
for(it = v.begin();it!=v.end();it++){
cout << *it << "\t";
}
cout << endl;
vector數(shù)據(jù)存取與遍歷
at(int idx); //返回索引idx所指的數(shù)據(jù)
operator[]; //返回索引idx所指的數(shù)據(jù)
front(); //返回容器中第一個數(shù)據(jù)元素
back(); //返回容器中最后一個數(shù)據(jù)元素
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
if(!vec.empty()){
cout << “the first number is:” << v.front() << endl;
cout << “the last number is:” << v.back() << endl;
}
完整實例:
#include <iostream>
#include<vector>//引用vector頭文件
using namespace std;
/**
vector 詳解實例
**/
int main() {
//構(gòu)造函數(shù)
vector<int> vecInt;
//循環(huán)添加數(shù)據(jù)
for(int i=0;i<200;i++){
vecInt.push_back(i);
}
//遍歷輸出
for (vector<int>::iterator it = vecInt.begin(); it != vecInt.end(); it++) {
cout << " num:" << *it << endl;
}
return 0;
}
運行結(jié)果:
判斷函數(shù)
bool empty() const:判斷向量是否為空,若為空,則向量中無元素
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
if(!v.empty()){
cout << "v is not empty!" << endl;
}
vector容量與大小
capacity(); //容器的容量
size(); //返回容器中元素的個數(shù)
resize(int num); //重新指定容器的長度為num,若容器變長,則以默認(rèn)值填充新位置。 //如果容器變短,則末尾超出容器長度的元素被刪除。
resize(int num, elem); //重新指定容器的長度為num,若容器變長,則以elem值填充新位置。 //如果容器變短,則末尾超出容器長度的元素被刪除
vector<int> v;
v.push_back(1);
cout << v.capacity() << endl; // 1
v.push_back(2);
cout << v.capacity() << endl; // 2
v.push_back(3);
cout << v.capacity() << endl; // 4
vector預(yù)留空間
功能描述: 減少vector在動態(tài)擴展容量時的擴展次數(shù)
函數(shù)原型: reserve(int len); //容器預(yù)留len個元素長度,預(yù)留位置不初始化,元素不可訪問。
如果數(shù)據(jù)量較大,可以一開始利用reserve預(yù)留空間
vector互換容器
swap(vec); // 將vec與本身的元素互換
swap可以使兩個容器互換,可以達到實用的收縮內(nèi)存效果:
vector(v).swap(v); //匿名對象
vector<int> v1,v2,v3;
v1.push_back(10);
v2.swap(v1);
swap(v3,v1);
其他函數(shù)
void swap(vector&): 交換兩個同類型向量的數(shù)據(jù)
void assign(int n,const T& x): 設(shè)置向量中第n個元素的值為x
void assign(const_iterator first,const_iterator last): 向量中[first,last)中元素設(shè)置成當(dāng)前向量元素
//c.assign(beg,end)將[beg,end)一個左閉右開區(qū)間的數(shù)據(jù)賦值給c。
vector<int> v1,v2;
v1.push_back(10);
v1.push_back(20);
v2.push_back(30);
v2.assign(v1.begin(),v1.end());
c.assign (n,elem)將n個elem的拷貝賦值給c。
vector<int> v;
v.assign(5,10);//往v里放5個10
迭代器
這里簡單提一下迭代器:
迭代器相當(dāng)于指針,例如:
// 對于變量而言,使用指針指向?qū)?yīng)的變量
// 以后就可以使用 * 加指針來操作該變量了
int a = 10;
int *p;
p = &a;
使用指針操作該變量
例如: *p = 11; // 操作后a變?yōu)?11
對于容器,使用迭代器操作容器中對應(yīng)位置的值
當(dāng)?shù)髦赶蛄巳萜髦械哪澄恢?則可以使用 * 加迭代器操作該位置了
// 定義一個vector
std::vector<int> myVec;
//添加10個元素
for(int j =0 ; j<10 ; j++)
{
myVec.push_back(j);
}
// 定義一個迭代器
std::vector<int>::iterator p;
// 指向容器的首個元素
p = myVec.begin();
// 移動到下一個元素
p ++;
// 修改該元素賦值
*p = 20 ; //< 則myVec容器中的第二個值被修改為了20
// 循環(huán)掃描迭代器,改變所有的值
for(p = myVec.begin() ; p!= myVec.end(); p++ )
{
*p = 50;
}
=0 ; j<10 ; j++)
{
myVec.push_back(j);
}
// 定義一個迭代器
std::vector<int>::iterator p;
// 指向容器的首個元素
p = myVec.begin();
// 移動到下一個元素
p ++;
// 修改該元素賦值
*p = 20 ; //< 則myVec容器中的第二個值被修改為了20
// 循環(huán)掃描迭代器,改變所有的值
for(p = myVec.begin() ; p!= myVec.end(); p++ )
{
*p = 50;
}
迭代器后面會有一篇博文專門講解。這里只要會用就行了。