1.vector容器介紹
向量(Vector)是一個封裝了動態(tài)大小數組的順序容器(Sequence Container)。跟任意其它類型容器一樣,它能夠存放各種類型的對象??梢院唵蔚恼J為,向量是一個能夠存放任意類型的動態(tài)數組。
1.1 vector和普通數組區(qū)別
普通數組是靜態(tài)的,在初始化是就確定空間大小,不支持動態(tài)擴展;
vector則可以看做是一個動態(tài)的數組,可以存放任意數據(基本數據類型和自定義數據類型均可以),支持動態(tài)擴展空間;
2.vector容器的構造函數
vector容器,可以看做是一個單端數組,構造函數有:無參構造、有參構造、拷貝構造。
vector 容器:--->單端數組
vector和普通數組的區(qū)別:
	普通數組是靜態(tài)空間,創(chuàng)建是就分配好
	vector支持動態(tài)擴展
vector容器常用迭代器:
	v.rend() -->指向第一個元素的前一個位置
	v.end()  -->指向最后一個元素的下一個位置
	v.begin() -->指向最后一個元素
	vector容器的迭代器是支持隨機訪問的迭代器
vector構造函數:
	無參構造:vector v;
	有參構造:vector(v.begin(),b.end());  --將begin到end之間的內容拷貝
			  vector(n,elem); //將n個elem內容拷貝
	拷貝構造:vector(const vector &v);
vector構造函數使用示例:
#include 
using namespace std;
#include 
void PrintVector(const vector& ptr)
{
	//若傳入的vector是一個常量,則才是需要迭代器是需要使用:const_iterator
	for ( vector:: const_iterator v = ptr.begin(); v != ptr.end(); v++)
	{
		cout < *v < " ";
	} 
	cout < endl;
}
void test()
{
	vector vtr;//默認構造
	for (int i = 0; i < 5; i++)
	{
		vtr.push_back(i);
	}
	PrintVector(vtr);
	vector v2(vtr.begin(), vtr.end());//將begin~end之間的內容拷貝
	PrintVector(v2);
	vector v3(10, 5);//賦值10個5
	PrintVector(v3);
	vectorv4(v3);//拷貝構造
	PrintVector(v4);
}
int main()
{
	test();
	system("pause");
}

3.vector賦值
vector賦值可以直接"="賦值,也可以使用成員函數assign賦值。
vector賦值:
	vector &operator=(const vector &v);//重載=
	assign(beg,end);//將beg~end區(qū)間進行賦值
	assign(n,elem);//n個elem賦值
賦值操作示例:
#include 
using namespace std;
#include 
void PrintVector(const vector& p)
{
	for (vector::const_iterator ptr = p.begin(); ptr != p.end(); ptr++)
	{
		cout < *ptr< " ";	
	}
	cout < endl;
}
void test()
{
	vector v;
	for (int i = 0; i < 5; i++)
	{
		v.push_back(i);
	}
	PrintVector(v);
	vector v2 = v;//等號賦值
	PrintVector(v2);
	vector v3(v.begin(), v.end());//區(qū)間賦值
	PrintVector(v3);
	vector v4(5, 666);//5個666賦值
	PrintVector(v4);
}
int main()
{
	test();
	system("pause");
}

4.vector獲取容量和成員個數
vector和普通數組一樣,下標是從0開始的。獲取容量大小使用capacity()函數,判斷容器是否為空可以使用empty()函數,獲取成員個數使用size()函數。還可以使用resize函數指定容器大?。?/p>
vector容器的容量和成員個數:
判斷vector容器是否為空:empty()
容量:capacity()
容器中的元素個數:size()
指定容器長度為num:resize(int num);
	若容器變長,則以默認值填充,默認值為0
	若容器變小,則末尾超出的元素將被刪除
指向容器的長度為num:resize(int num,elem);
	若容器變長,則用elem填充
	若容器變小,則末尾超出的元素將被刪除
使用示例:
#include 
using namespace std;
#include 
void PrintVector(const vector& p)
{
	for (vector::const_iterator ptr = p.begin(); ptr != p.end(); ptr++)
	{
		cout < *ptr < " ";
	}
	cout < endl;
	cout < "容量:" < p.capacity();
	cout < endl;
	cout < "元素個數:" < p.size();
	cout < endl;
}
void test()
{
	vector vtr;
	for (int i = 0; i < 5; i++)
	{
		vtr.push_back(i);
	}
	PrintVector(vtr);
	//指定容器長度
	vtr.resize(10,666);//指定長度為10,超出則用666填充
	PrintVector(vtr);
	vtr.resize(3);//指定長度小于實際長度,則會刪除超出的元素,但空間超出的空間還是存在
	PrintVector(vtr);
	vectorv2;
	v2.resize(5);//沒有指定填充值則默認為0
	if (v2.empty())
	{
		cout < "v2為空" < endl;
	}
	PrintVector(v2);
}
int main()
{
	test();
	system("pause");
}

注意:在resize()函數時,若指定的大小比原空間大,則容器會進行擴充;若指定的大小比原空間小,則會將超出的成員刪除,但空間大小不就刪除。
5.vector容器成員刪除與插入
vector容器是一個單端數組,通過bush_back()函數可以實現從末尾插入數據。
pop_back()從末尾刪除數據;
從指定位置插入數據可以使用insert()成員函數,該函數有多個重載版本。
要從指定位置刪除數據可以使用erase()成員函數,該函數有多個重載版本;
clear()函數實現清空容器。
vector插入與刪除:
	push_back();//尾插
	pop_back();//尾刪
	insert(const_iterator pos,elem);//迭代器指向位置pos插入元素elem
	insert(const_iterator pos,int count,elem)//迭代器指向位置pos插入count個元素elem
	erase(const_iterator pos);//刪除迭代器指向的元素
	erase(const_iterator start,const_iterator end);//刪除迭代器start~end之間的元素
	clear();//刪除容器中所有元素
實現示例:
#include 
using namespace std;
#include 
class Person
{
	friend ostream& operator<(ostream& cout, const Person& per);
public:
	Person() {
	}
	Person(int age, string name) :age(age), name(name)
	{
	}
	Person& operator=(Person p)
	{
		this-?>age = p.age;
		this->name = p.name;
		return *this;
	}
private:
	int age;
	string name;
};
ostream& operator<(ostream& cout, const Person &per)
{
	cout < "姓名:" < per.name < "t年齡:" < per.age;
	return cout;
}
void PrintVector(const vector& p)
{
	for (vector::const_iterator ptr = p.begin(); ptr != p.end(); ptr++)
	{
		cout < *ptr < endl;
	}
}
void test()
{
	//實例化對象
	Person v1(18,"小王");
	Person v2(18, "小李");
	Person v3(18, "小劉");
	Person v4(18, "小陳");
	Person v5(18, "小蔣");
	Person v[5] = { v1,v2,v3,v4,v5 };
	for (int i = 0; i < sizeof(v) / sizeof(v[0]); i++)
	{
		cout < v[i] < endl;
	}
	cout < "--------------------vector-------------------" < endl;
	//創(chuàng)建容器
	vector vtr;
	//尾插
	for (int i = 0; i < 5; i++)
	{
		vtr.push_back(v[i]);//賦值
	}
	PrintVector(vtr);
	//尾刪
	cout < "t尾刪" < endl;
	vtr.pop_back();
	PrintVector(vtr);
	//插入
	cout < "t插入" < endl;
	Person temp(24, "老呂");
	vtr.insert(vtr.begin(), temp);
	PrintVector(vtr);
	cout < "t第三個位置插入3個值" < endl;
	vector::iterator ptr = vtr.begin();
	ptr += 3;
	vtr.insert(ptr, 3,temp);
	PrintVector(vtr);
	cout < "t刪除首位置的值" < endl;
	vtr.erase(vtr.begin());
	PrintVector(vtr);
	cout < "t刪除第3個位置到第6個位置的值" < endl;
	ptr = vtr.begin();
	vtr.erase(ptr+3, ptr+6);
	PrintVector(vtr);
	cout < "t清空" < endl;
	vtr.clear();//或者使用vtr.erase(vtr.begin(),vtr.end);
	PrintVector(vtr);
	cout < "空間大小:" < vtr.capacity();
	cout < endl;
	cout < "元素個數:" < vtr.size();
	cout < endl;
}
int main()
{
	test();
	system("pause");
}

6.vector容器數據存取
vector容器也可以像普通數組一樣同[]訪問成員,此外還可以使用at()成員函數實現數據讀寫;
vector數據存取
	at(int idx);//返回下標對應的內容
	operator[];//重載[]
	front();//返回容器中第一個元素
	back();//返回容器中最后一個元素
實現示例:
#include 
using namespace std;
#include 
#include 
void PrintVector(int val)
{
	cout < val < " ";
}
void test()
{
	
	vector vtr;
	for (int i = 0; i < 5; i++)
	{
		vtr.push_back(i);
	}
	for_each(vtr.begin(), vtr.end(), PrintVector);
	cout < endl;
	cout < "第一個值:" < vtr.front() < endl;
	cout < "最后一個值:" < vtr.back() < endl;
	vtr[0] = 100;
	cout < "vtr[0]=" < vtr.at(0) < endl;
	
}
int main()
{
	test();
	system("pause");
}

7.vector容器互換元素
在vector容器中,可以通過成員函數swap()函數實現兩個容器的成員互換。
注意:swap互換元素同時也可將空間大小進行互換。
vector容器互換
swap(vec);//將vec中的元素和本身的元素互換
注意:swap互換元素同時也可將空間大小進行互換
使用示例:
#include 
#include 
using namespace std;
void PrintVector(const vector& p)
{
	for (vector::const_iterator ptr = p.begin(); ptr != p.end(); ptr++)
	{
		cout < *ptr < " ";
	}
	cout < endl;
}
void test()
{
	vector vtr(10,666);
	vector vtr2;
	for (int i = 0; i < 5; i++)
	{
		vtr2.push_back(i);
	}
	cout < "t互換前" < endl;
	PrintVector(vtr);
	PrintVector(vtr2);
	cout < "vtr大小:" < vtr.capacity() < "t元素個數:"(vtr).swap(vtr);
	/*
	vector(vtr) --使用匿名對象,將匿名對象初始化為vtr
	vector(vtr).swap(vtr);  --在通過swap函數和匿名對象互換,此時即可實現收縮內存
	*/
	cout < "vtr大?。? < vtr.capacity() < "t元素個數:" < vtr.size() < endl;
}
int main()
{
	test();
	cout < "------------------test02示例------------------------" < endl;
	test02();
	system("pause");
}
()

由于swap函數不僅可以互換元素,而且空間也大小也是可以互換的,所以有些清空下可以使用swap()函數來合理使用空間,避免空間資源浪費。
8.vector容器預留空間
reserve()成員函數可以指定空間大小,為vector容器預留空間。
reserve函數和resize()函數區(qū)別:
resize()函數指定大小后會直接初始化空間;
reserve()函數指定的大小不會初始化空間,預留的空間不能直接訪問,必須在賦值之后采用訪問。
reserve(int len);//容器預留len長度的元素,預留位置初始化,元素不可訪問
使用示例:
#include 
using namespace std;
#include 
void test()
{
	vectorvtr;
	vtr.reserve(10);
	cout < "空間大小:" < vtr.capacity() < "元素個數:" vtr2;
	vtr2.resize(10);//指定長度
	cout < "空間大小:" < vtr2.capacity() < "元素個數:" < vtr2.size() < endl;
}
void test02()
{
	vector vtr;
	int* p=NULL;
	cout < "不適用預留空間:" < endl;
	int num = 0;
	for (int i = 0; i < 100000; i++)
	{
		vtr.push_back(i);
		if (p != &vtr[0])
		{
			p = &vtr[0];
			num++;
		}
	}
	cout < "動態(tài)擴展空間次數:" < num vtr2;
	vtr2.reserve(100000);//預留100000空間
	p = NULL;
	cout < "使用預留空間:" < endl;
	num = 0;
	for (int i = 0; i < 100000; i++)
	{
		vtr2.push_back(i);
		if (p != &vtr2[0])
		{
			p = &vtr2[0];
			num++;
		}
	}
	cout < "動態(tài)擴展空間次數:" < num < endl;
}
int main()
{
	test();
	cout < "t示例2:" < endl;
	test02();
	system("pause");
}
;>()

vector容器空間是根據成員來動態(tài)擴展,若一開始就知道使用的空間大概需要多大,則可以使用reserve()函數來指定,從而可以減少中間動態(tài)擴展的次數。
- 
                                編程
                                +關注
關注
89文章
3705瀏覽量
96515 - 
                                函數
                                +關注
關注
3文章
4403瀏覽量
66599 - 
                                容器
                                +關注
關注
0文章
521瀏覽量
22763 - 
                                C++
                                +關注
關注
22文章
2120瀏覽量
76611 - 
                                Vector
                                +關注
關注
3文章
85瀏覽量
9815 
發(fā)布評論請先 登錄
ccs如何使用C語言的“容器”?
C++ vector刪除符合條件元素的編程技巧
C++中vector的定義與初始化
    
C++入門之string
C++入門之數組的概念
C++學習筆記之順序容器
C++入門之通用算法
動態(tài)數組和C++ std::vector詳解
使用C++ sort函數對vector進行自定義排序
C++之父新作帶你勾勒現代C++地圖
    
          
        
        
c++之vector容器
                
 
    
    
    
    
           
            
            
                
            
評論