c++ List、Vector、Stack、Queue使用


一、List使用

引入頭文件#include <list>

List基本函數
Lists將元素按順序儲存在鏈表中. 與 向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢.
assign() 給list賦值
back() 返回最后一個元素
begin() 返回指向第一個元素的迭代器
clear() 刪除所有元素
empty() 如果list是空的則返回true
end() 返回末尾的迭代器
erase() 刪除一個元素
front() 返回第一個元素
get_allocator() 返回list的配置器
insert() 插入一個元素到list中
max_size() 返回list能容納的最大元素數量
merge() 合並兩個list
pop_back() 刪除最后一個元素
pop_front() 刪除第一個元素
push_back() 在list的末尾添加一個元素
push_front() 在list的頭部添加一個元素
rbegin() 返回指向第一個元素的逆向迭代器
remove() 從list刪除元素
remove_if() 按指定條件刪除元素
rend() 指向list末尾的逆向迭代器
resize() 改變list的大小
reverse() 把list的元素倒轉
size() 返回list中的元素個數
sort() 給list排序
splice() 合並兩個list
swap() 交換兩個list
unique() 刪除list中重復的元素

舉例說明:

#include <iostream> 
#include <list> 

using namespace std; 
typedef list<int> INTLIST; 

//從前向后顯示list隊列的全部元素 
void put_list(INTLIST list, string name) 
{ 
    INTLIST::iterator plist; 
    
    cout << "The contents of " << name << " : "; 
    for(plist = list.begin(); plist != list.end(); plist++) 
        cout << *plist << " "; 
    cout<<endl; 
} 

//測試list容器的功能 
int main() 
{ 
    //list1對象初始為空 
    INTLIST list1; 
    //list2對象最初有10個值為6的元素 
    INTLIST list2(10,6); 
    //list3對象最初有9個值為6的元素 
    INTLIST list3(list2.begin(),--list2.end()); 
    
    //聲明一個名為i的雙向迭代器 
    INTLIST::iterator i; 
    
    //從前向后顯示各list對象的元素 
    put_list(list1,"list1"); 
    put_list(list2,"list2"); 
    put_list(list3,"list3"); 
    
    //從list1序列后面添加兩個元素 
    list1.push_back(2); 
    list1.push_back(4); 
    cout<<"list1.push_back(2) and list1.push_back(4):"<<endl; 
    put_list(list1,"list1"); 
    
    //從list1序列前面添加兩個元素 
    list1.push_front(5); 
    list1.push_front(7); 
    cout<<"list1.push_front(5) and list1.push_front(7):"<<endl; 
    put_list(list1,"list1"); 
    
    //在list1序列中間插入數據 插入3個9
    list1.insert(++list1.begin(),3,9); 
    cout<<"list1.insert(list1.begin()+1,3,9):"<<endl; 
    put_list(list1,"list1"); 
    
    //測試引用類函數 輸出第一個元素和最后一個元素
    cout<<"list1.front()="<<list1.front()<<endl; 
    cout<<"list1.back()="<<list1.back()<<endl; 
    
    //從list1序列的前后各移去一個元素 刪除元素
    list1.pop_front(); 
    list1.pop_back(); 
    cout<<"list1.pop_front() and list1.pop_back():"<<endl; 
    put_list(list1,"list1"); 
    
    //清除list1中的第2個元素 
    list1.erase(++list1.begin()); 
    cout<<"list1.erase(++list1.begin()):"<<endl; 
    put_list(list1,"list1"); 
    
    //對list2賦值並顯示 
    list2.assign(8,1); 
    cout<<"list2.assign(8,1):"<<endl; 
    put_list(list2,"list2"); 
    
    //顯示序列的狀態信息 
    cout<<"list1.max_size(): "<<list1.max_size()<<endl; 
    cout<<"list1.size(): "<<list1.size()<<endl; 
    cout<<"list1.empty(): "<<list1.empty()<<endl; 
    
    //list序列容器的運算 
    put_list(list1,"list1"); 
    put_list(list3,"list3"); 
    cout<<"list1>list3: "<<(list1>list3)<<endl; 
    cout<<"list1<list3: "<<(list1<list3)<<endl; 
    
    //對list1容器排序 
    list1.sort(); 
    put_list(list1,"list1"); 
    
    //結合處理 合並list
    list1.splice(++list1.begin(), list3); 
    put_list(list1,"list1"); 
    put_list(list3,"list3"); 
    return 0;
} 

  輸出結果如下:

The contents of list1 :
The contents of list2 : 6 6 6 6 6 6 6 6 6 6
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4
list1.insert(list1.begin()+1,3,9):
The contents of list1 : 7 9 9 9 5 2 4
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1
list1.max_size(): 1073741823
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1>list3: 1
list1<list3: 0
The contents of list1 : 2 5 9 9
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
The contents of list3 :

二、Vector使用

在c++中,vector是一個十分有用的容器,下面對這個容器做一下總結。

1 基本操作

(1)頭文件#include<vector>.

(2)創建vector對象,vector<int> vec;

(3)尾部插入數字:vec.push_back(a);

(4)使用下標訪問元素,cout<<vec[0]<<endl;記住下標是從0開始的。

(5)使用迭代器訪問元素.

vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
    cout<<*it<<endl;

(6)插入元素:    vec.insert(vec.begin()+i,a);在第i+1個元素前面插入a;

(7)刪除元素:    vec.erase(vec.begin()+2);刪除第3個元素

vec.erase(vec.begin()+i,vec.end()+j);刪除區間[i,j-1];區間從0開始

(8)向量大小:vec.size();

(9)清空:vec.clear();

2、結構體作為vector元素

vector的元素不僅僅可以使int,double,string,還可以是結構體,但是要注意:結構體要定義為全局的,否則會出錯。下面是一段簡短的程序代碼:

復制代碼
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

typedef struct rect
{
    int id;
    int length;
    int width;

  //對於向量元素是結構體的,可在結構體內部定義比較函數,下面按照id,length,width升序排序。
  bool operator< (const rect &a)  const
    {
        if(id!=a.id)
            return id<a.id;
        else
        {
            if(length!=a.length)
                return length<a.length;
            else
                return width<a.width;
        }
    } }Rect; int main() { vector<Rect> vec; Rect rect; rect.id=1; rect.length=2; rect.width=3; vec.push_back(rect); vector<Rect>::iterator it=vec.begin(); cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl; return 0; }
復制代碼

 3  算法

(1) 使用reverse將元素翻轉:需要頭文件#include<algorithm>

reverse(vec.begin(),vec.end());將元素翻轉(在vector中,如果一個函數中需要兩個迭代器,

一般后一個都不包含.)

(2)使用sort排序:需要頭文件#include<algorithm>,

sort(vec.begin(),vec.end());(默認是按升序排列,即從小到大).

可以通過重寫排序比較函數按照降序比較,如下:

定義排序比較函數:

bool Comp(const int &a,const int &b)
{
    return a>b;
}
調用時:sort(vec.begin(),vec.end(),Comp),這樣就降序排序。

4、lower_bound()與up_bound()使用

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 10); //          ^
  up= std::upper_bound (v.begin(), v.end(), 10); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
  std::cout << *low<<" "<<*up;
  return 0;
}

三、vector與list區別

c++標准庫中,容器vector和list都可以用來存放一組類型相同的數據。而且二者不同於數組的一點是,支持動態增長。但它們還是有有幾點不同

(1)  vector是順序表,表示的是一塊連續的內存,元素被順序存儲;list是雙向連接表,在內存中不一定連續。

(2)當數值內存不夠時,vector會重新申請一塊足夠大的連續內存,把原來的數據拷貝到新的內存里面;list因為不用考慮內存的連續,因此新增開銷比vector小。

(3)list只能通過指針訪問元素,隨機訪問元素的效率特別低,在需要頻繁隨機存取元素時,使用vector更加合適。

(4)當向vector插入或者刪除一個元素時,需要復制移動待插入元素右邊的所有元素;因此在有頻繁插入刪除操作時,使用list更加合適。

四、stack使用

引入頭文件
#include<stack>
stack<int> s
s.push()
s.pop()
s.top()
s.empty()
s.size()

五、Queue的使用

引入頭文件

#include<queue>
定義queue 對象的示例代碼如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入隊,如例:q.push(x); 將x 接到隊列的末端。
出隊,如例:q.pop(); 彈出隊列的第一個元素,注意,並不會返回被彈出元素的值
訪問隊首元素,如例:q.front(),即最早被壓入隊列的元素。
訪問隊尾元素,如例:q.back(),即最后被壓入隊列的元素。
判斷隊列空,如例:q.empty(),當隊列空時,返回true。
訪問隊列中的元素個數,如例:q.size()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM