vector的用法
vector包括的函數為:
寫了一段程序,測試一下幾個常用的函數:
[cpp]
#include<iostream>
#include<vector>
#include<list>
#include<deque>
using namespace std;
int main()
{
//初始化
vector<int> a(3,7);
cout<<"輸出元素";
///迭代器
for(vector<int>::iterator itr=a.begin();itr<a.end();++itr)
cout<<*itr;
cout<<endl;
//測試是否為空 empty()
if(!a.empty())
cout<<"此容器的大小為:"<<a.capacity()<<"含有元素為:"<<a.size();
else
cout<<"元素為空";
cout<<endl;
//清楚元素clear()
a.clear();
if(!a.empty())
cout<<"容器裡的元素被clear()清理完畢"<<endl;
//調整容器的大小 resize
a.resize(5);
cout<<"此容器的大小重新調整為:"<<a.capacity()<<endl;
cout<<"默認的5個元素大小為:";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
a.push_back(3);
a.push_back(4);
//返回第一個元素
cout<<"返回第一個元素"<<a.front()<<",此時容器的capacity為:"<<a.capacity()<<"可見push_back 只是在末尾增加元素";
cout<<endl<<"通過at獲取元素"<<a.at(5)<<endl;
//pop返回
a.pop_back();
cout<<"pop_back後,容器的大小為:"<<a.capacity()<<"元素的個數為:"<<a.size()<<endl<<"輸出元素";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
cout<<endl<<"由此可見,pop_back只彈出元素,不返回"<<endl;
a.assign(4,5);//不會改變capacity,會改變size
cout<<"capacity:"<<a.capacity()<<endl;;
a[0]=2;
cout<<"顯示第0個和第3個位置的元素,驗證[]和assign的賦值功能:"<<a[0]<<","<<a[3];
cout<<"front()返回最後一個元素"<<a.back();
vector<int>::iterator it=a.begin();
a.erase(it);
cout<<"erase後的元素為:";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
a.swap(vector<int>());
return 0;
}
#include<iostream>
#include<vector>
#include<list>
#include<deque>
using namespace std;
int main()
{
//初始化
vector<int> a(3,7);
cout<<"輸出元素";
///迭代器
for(vector<int>::iterator itr=a.begin();itr<a.end();++itr)
cout<<*itr;
cout<<endl;
//測試是否為空 empty()
if(!a.empty())
cout<<"此容器的大小為:"<<a.capacity()<<"含有元素為:"<<a.size();
else
cout<<"元素為空";
cout<<endl;
//清楚元素clear()
a.clear();
if(!a.empty())
cout<<"容器裡的元素被clear()清理完畢"<<endl;
//調整容器的大小 resize
a.resize(5);
cout<<"此容器的大小重新調整為:"<<a.capacity()<<endl;
cout<<"默認的5個元素大小為:";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
a.push_back(3);
a.push_back(4);
//返回第一個元素
cout<<"返回第一個元素"<<a.front()<<",此時容器的capacity為:"<<a.capacity()<<"可見push_back 只是在末尾增加元素";
cout<<endl<<"通過at獲取元素"<<a.at(5)<<endl;
//pop返回
a.pop_back();
cout<<"pop_back後,容器的大小為:"<<a.capacity()<<"元素的個數為:"<<a.size()<<endl<<"輸出元素";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
cout<<endl<<"由此可見,pop_back只彈出元素,不返回"<<endl;
a.assign(4,5);//不會改變capacity,會改變size
cout<<"capacity:"<<a.capacity()<<endl;;
a[0]=2;
cout<<"顯示第0個和第3個位置的元素,驗證[]和assign的賦值功能:"<<a[0]<<","<<a[3];
cout<<"front()返回最後一個元素"<<a.back();
vector<int>::iterator it=a.begin();
a.erase(it);
cout<<"erase後的元素為:";
for(vector<int>::iterator it=a.begin();it<a.end();++it)
cout<<*it;
a.swap(vector<int>());
return 0;
}
測試結果
:
list的用法
與vector相比,list沒有capacity,原因是因為:list不需要對內存重新全部分配,list用鏈表的,每增加一個元素都要新開辟一個元素,然後插入到list中,記住list是雙鏈表。list多了一個pash_front和pop_front。
[cpp]
#include<list>
#include<iostream>
using namespace std;
int main()
{
//定義LISTINT
typedef list<int> LISTINT;
typedef list<char> LISTCHAR;
LISTINT list_int(3);
list_int.assign(4,5);
list_int.push_back(6);
list_int.push_front(10);
list_int.push_front(1);
list_int.pop_front();
list_int.pop_back();
list_int.push_back(6);
for(LISTINT::iterator itr=list_int.begin();itr!=list_int.end();++itr)
cout<<*itr<<",";
cout<<endl;
for(LISTINT::reverse_iterator itr=list_int.rbegin();itr!=list_int.rend();++itr)
cout<<*itr<<",";
cout<<endl;
list_int.erase(--list_int.end());
list_int.sort();
for(LISTINT::iterator itr=list_int.begin();itr!=list_int.end();++itr)
cout<<*itr<<",";
return 0;
}
#include<list>
#include<iostream>
using namespace std;
int main()
{
//定義LISTINT
typedef list<int> LISTINT;
typedef list<char> LISTCHAR;
LISTINT list_int(3);
list_int.assign(4,5);
list_int.push_back(6);
list_int.push_front(10);
list_int.push_front(1);
list_int.pop_front();
list_int.pop_back();
list_int.push_back(6);
for(LISTINT::iterator itr=list_int.begin();itr!=list_int.end();++itr)
cout<<*itr<<",";
cout<<endl;
for(LISTINT::reverse_iterator itr=list_int.rbegin();itr!=list_int.rend();++itr)
cout<<*itr<<",";
cout<<endl;
list_int.erase(--list_int.end());
list_int.sort();
for(LISTINT::iterator itr=list_int.begin();itr!=list_int.end();++itr)
cout<<*itr<<",";
return 0;
}