欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ Vector用法詳解

 更新時(shí)間:2015年07月08日 10:45:14   投稿:junjie  
這篇文章主要介紹了C++ Vector用法詳解,vector是C++標(biāo)準(zhǔn)模版庫(STL,Standard Template Library)中的部分內(nèi)容,本文詳細(xì)介紹了它的方方面面,需要的朋友可以參考下

vector是C++標(biāo)準(zhǔn)模版庫(STL,Standard Template Library)中的部分內(nèi)容。之所以認(rèn)為是一個(gè)容器,是因?yàn)樗軌蛳袢萜饕粯哟娣鸥鞣N類型的對(duì)象,簡(jiǎn)單的說:vector是一個(gè)能夠存放任意類型的動(dòng)態(tài)數(shù)組,能夠增加和壓縮數(shù)據(jù)。

使用vector容器之前必須加上<vector>頭文件:#include<vector>;

vector屬于std命名域的內(nèi)容,因此需要通過命名限定:using std::vector;也可以直接使用全局的命名空間方式:using namespace std;

vector成員函數(shù)

c.push_back(elem)在尾部插入一個(gè)elem數(shù)據(jù)。

復(fù)制代碼 代碼如下:

vector<int> v;
    v.push_back(1);

c.pop_back()刪除末尾的數(shù)據(jù)。
復(fù)制代碼 代碼如下:

vector<int> v;
    v.pop_back();

c.assign(beg,end)將[beg,end)一個(gè)左閉右開區(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個(gè)elem的拷貝賦值給c。

復(fù)制代碼 代碼如下:

vector<int> v;
v.assign(5,10);//往v里放5個(gè)10

c.at(int index)傳回索引為index的數(shù)據(jù),如果index越界,拋出out_of_range異常。

vecto<int> v;
cout << v.at(2) << endl;//打印vector中下標(biāo)是2的數(shù)據(jù)

c.begin()返回指向第一個(gè)數(shù)據(jù)的迭代器。

c.end()返回指向最后一個(gè)數(shù)據(jù)之后的迭代器。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector<int>::iterator it;
for(it = v.begin();it!=v.end();it++){
  cout << *it << "\t";
}
cout << endl;

c.rbegin()返回逆向隊(duì)列的第一個(gè)數(shù)據(jù),即c容器的最后一個(gè)數(shù)據(jù)。

c.rend()返回逆向隊(duì)列的最后一個(gè)數(shù)據(jù)的下一個(gè)位置,即c容器的第一個(gè)數(shù)據(jù)再往前的一個(gè)位置。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector<int>::reverse_iterator it;
for(it = v.rbegin();it!=v.rend();it++){
 cout << *it << "\t";
}
cout << endl;

c.capacity()返回容器中數(shù)據(jù)個(gè)數(shù),翻倍增長(zhǎng)。

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

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;

c.empty()判斷容器是否為空。

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
if(!v.empty()){
 cout << "v is not empty!" << endl;  
}

c.erase(pos)刪除pos位置的數(shù)據(jù),傳回下一個(gè)數(shù)據(jù)的位置。

復(fù)制代碼 代碼如下:

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.erase(v.begin());

c.erase(beg,end)刪除[beg,end)區(qū)間的數(shù)據(jù),傳回下一個(gè)數(shù)據(jù)的位置。

復(fù)制代碼 代碼如下:

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.erase(v.begin(),v.end());

c.front()返回第一個(gè)數(shù)據(jù)。

c.back()傳回最后一個(gè)數(shù)據(jù),不檢查這個(gè)數(shù)據(jù)是否存在。

復(fù)制代碼 代碼如下:

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;
}

c.insert(pos,elem) 在pos位置插入一個(gè)elem的拷貝,返回插入的值的迭代器。

c.insert(pos,n,elem)在pos位置插入n個(gè)elem的數(shù)據(jù),無返回值。

c.insert(pos,beg,end)在pos位置插入在[beg,end)區(qū)間的數(shù)據(jù),無返回值。

復(fù)制代碼 代碼如下:

vector<int> v;
v.insert(v.begin(),10);
v.insert(v.begin(),2,20);
v.insert(v.begin(),v1.begin(),v1.begin()+2);

c.size()返回容器中實(shí)際數(shù)據(jù)的個(gè)數(shù)。

c.resize(num)重新指定隊(duì)列的長(zhǎng)度。(往往用來增加vector的長(zhǎng)度,小->大 ok 大->小 沒用!)

c.reserve()保留適當(dāng)?shù)娜萘俊?/p>

  針對(duì)resize()和reserver()做一點(diǎn)分析:

  reserve是容器預(yù)留空間,但并不真正創(chuàng)建元素對(duì)象,在創(chuàng)建對(duì)象之前,不能引用容器內(nèi)的元素,因此當(dāng)加入新的元素時(shí),需要用push_back()/insert()函數(shù)。

  resize是改變?nèi)萜鞯拇笮?,并且?chuàng)建對(duì)象,因此,調(diào)用這個(gè)函數(shù)之后,就可以引用容器內(nèi)的對(duì)象了,因此當(dāng)加入新的元素時(shí),用operator[]操作符,或者用迭代器來引用元素對(duì)象。

  再者,兩個(gè)函數(shù)的形式是有區(qū)別的,reserve函數(shù)之后一個(gè)參數(shù),即需要預(yù)留的容器的空間;resize函數(shù)可以有兩個(gè)參數(shù),第一個(gè)參數(shù)是容器新的大小,第二個(gè)參數(shù)是要加入容器中的新元素,如果這個(gè)參數(shù)被省略,那么就調(diào)用元素對(duì)象的默認(rèn)構(gòu)造函數(shù)。

  reserve只是保證vector的空間大小(capacity)最少達(dá)到它的參數(shù)所指定的大小n。在區(qū)間[0, n)范圍內(nèi),如果下標(biāo)是index,vector[index]這種訪問有可能是合法的,也有可能是非法的,視具體情況而定。
     resize和reserve接口的共同點(diǎn)是它們都保證了vector的空間大小(capacity)最少達(dá)到它的參數(shù)所指定的大小。

c.max_size()返回容器能容量的最大數(shù)量。

c1.swap(c2)將c1和c2交換。

swap(c1,c2)同上。

復(fù)制代碼 代碼如下:

vector<int> v1,v2,v3;
v1.push_back(10);
v2.swap(v1);
swap(v3,v1);

vector<type>c;創(chuàng)建一個(gè)空的vector容器。

vector<type> c1(c2);復(fù)制一個(gè)vector。

vector<type> c(n);創(chuàng)建一個(gè)vector,含有n個(gè)數(shù)據(jù),數(shù)據(jù)均以缺省構(gòu)造產(chǎn)生,即全0;

vector<type> c(n,elem)創(chuàng)建一個(gè)vector,含有n個(gè)elem的拷貝數(shù)據(jù)。

vector<type> c(beg,end)創(chuàng)建一個(gè)以[beg,end)區(qū)間的vector。

~vector<type>()   銷毀所有數(shù)據(jù),施放內(nèi)存。

壓縮一個(gè)臃腫的vector

很多時(shí)候大量的刪除數(shù)據(jù),或者通過使用reserver(),結(jié)果vector的空間遠(yuǎn)遠(yuǎn)大于實(shí)際的需要。所以需要壓縮vector到它的實(shí)際大小。resize()能增加vector的大小。clear()僅僅移除容器內(nèi)的數(shù)據(jù),不能改變capacity()的大小,所以對(duì)vector進(jìn)行壓縮非常重要。

測(cè)試一下clear()函數(shù):

復(fù)制代碼 代碼如下:

//
//  vector.cpp
//  vector
//
//  Created by scandy_yuan on 13-1-7.
//  Copyright (c) 2013年 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char * argv[])
{

    // insert code here...
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    vector<int>::iterator it;
    cout << "clear before:" << " ";
    for(it=v.begin();it!=v.end();it++){
        cout << *it << "\t";
    }
    cout << endl;
    cout << "clear before capacity:" << v.capacity() << endl;
    v.clear();
    cout << "after clear:" << " ";
    for(it=v.begin();it!=v.end();it++){
        cout << *it << "\t";
    }
    cout << endl;
    cout << "after clear capacity:" << v.capacity() << endl;
    return 0;
}

結(jié)果:

復(fù)制代碼 代碼如下:

clear before: 1    2    3   
clear before capacity:4
after clear:
after clear capacity:4

為什么這里打印的capacity()的結(jié)果是4不做詳細(xì)解釋,請(qǐng)參考上面關(guān)于capacity的介紹。 通過結(jié)果,我們可以看到clear()之后數(shù)據(jù)全部清除了,但是capacity()依舊是4。

假設(shè):我們通過原本的vector來創(chuàng)建一個(gè)新的vector,讓我們看看將會(huì)發(fā)生什么?

復(fù)制代碼 代碼如下:

//
//  vector.cpp
//  vector
//
//  Created by scandy_yuan on 13-1-7.
//  Copyright (c) 2013年 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char * argv[])
{

    // insert code here...
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    cout << "v.capacity()" << v.capacity() << endl;
   
    vector<int> v1(v);
    cout << "v1.capacity()" << v1.capacity() << endl;
    return 0;
}

結(jié)果:

復(fù)制代碼 代碼如下:

v.capacity()4
v1.capacity()3

可以看出,v1的capacity()是v的實(shí)際大小,因此可以達(dá)到壓縮vector的目的。但是我們不想新建一個(gè),我們想在原本的vector(即v)上進(jìn)行壓縮,那么借鑒上面的方式思考另一種方式。

假設(shè):我們通過swap函數(shù)把v1交換回v,看看會(huì)發(fā)生什么?

復(fù)制代碼 代碼如下:

//
//  vector.cpp
//  vector
//
//  Created by scandy_yuan on 13-1-7.
//  Copyright (c) 2013年 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char * argv[])
{

    // insert code here...
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    cout << "v.capacity()" << v.capacity() << endl;
   
    vector<int> v1(v);
    cout << "v1.capacity()" << v1.capacity() << endl;
   
    v.swap(v1);
    cout << "v.swap(v1).capacity()" << v.capacity() << endl;
    return 0;
}

結(jié)果:

復(fù)制代碼 代碼如下:

v.capacity()4
v1.capacity()3
v.swap(v1).capacity()3

可以看出,v.capacity()變成了3,目的達(dá)到。但是代碼給人感覺繁瑣臃腫,我們從新考慮一種新的寫法,采用匿名對(duì)象來代替v1這個(gè)中間對(duì)象:vector<int> (v).swap(v);

測(cè)試:

復(fù)制代碼 代碼如下:

//
//  vector.cpp
//  vector
//
//  Created by scandy_yuan on 13-1-7.
//  Copyright (c) 2013年 Sam. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char * argv[])
{

    // insert code here...
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    cout << "v.capacity()" << v.capacity() << endl;
   
    vector<int> (v).swap(v);
    cout << "v.capacity()" << v.capacity() << endl;
    return 0;
}

結(jié)果:

復(fù)制代碼 代碼如下:

v.capacity()4
v.capacity()3

可以看到 v.capacity()由4編程了3,目的達(dá)到。

之前沒有關(guān)注C++11,感謝@egmkang,確實(shí)在C++11中已經(jīng)提供了shrink_to_fit()函數(shù)實(shí)現(xiàn)vector的壓縮。

如下:

復(fù)制代碼 代碼如下:

#include <iostream>
#include <vector>
int main()
{
    std::vector<int> v;
    std::cout << "Default-constructed capacity is " << v.capacity() << '\n';
    v.resize(100);
    std::cout << "Capacity of a 100-element vector is " << v.capacity() << '\n';
    v.clear();
    std::cout << "Capacity after clear() is " << v.capacity() << '\n';
    v.shrink_to_fit();
    std::cout << "Capacity after shrink_to_fit() is " << v.capacity() << '\n';
}

結(jié)果:

復(fù)制代碼 代碼如下:

Default-constructed capacity is 0
Capacity of a 100-element vector is 100
Capacity after clear() is 100
Capacity after shrink_to_fit() is 0

相關(guān)文章

  • 詳解C語言之單鏈表

    詳解C語言之單鏈表

    這篇文章主要為大家介紹了C語言的單鏈表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • c++實(shí)現(xiàn)值機(jī)系統(tǒng)

    c++實(shí)現(xiàn)值機(jī)系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了c++實(shí)現(xiàn)在線值機(jī)系統(tǒng)程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++實(shí)現(xiàn)冒泡排序(BubbleSort)

    C++實(shí)現(xiàn)冒泡排序(BubbleSort)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)冒泡排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C++面向?qū)ο笾惡蛯?duì)象那些你不知道的細(xì)節(jié)原理詳解

    C++面向?qū)ο笾惡蛯?duì)象那些你不知道的細(xì)節(jié)原理詳解

    C++是面向?qū)ο缶幊痰?這也是C++與C語言的最大區(qū)別,下面這篇文章主要給大家介紹了關(guān)于C++面向?qū)ο笾惡蛯?duì)象的細(xì)節(jié)原理的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • C++?primer超詳細(xì)講解關(guān)聯(lián)容器

    C++?primer超詳細(xì)講解關(guān)聯(lián)容器

    兩個(gè)主要的關(guān)聯(lián)容器為map和set,map中元素是一些關(guān)鍵字—值對(duì),關(guān)鍵字起索引的作用,值則表示與索引相關(guān)聯(lián)的數(shù)據(jù)。set中每個(gè)元素只包含一個(gè)關(guān)鍵字,set支持高效的關(guān)鍵字查詢操作——檢查一個(gè)給定關(guān)鍵字是否在set中
    2022-07-07
  • C語言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法

    C語言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法

    這篇文章主要介紹了C語言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法,涉及C語言針對(duì)字符串的簡(jiǎn)單遍歷與判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C++與C語言的區(qū)別你知道嗎

    C++與C語言的區(qū)別你知道嗎

    這篇文章主要為大家詳細(xì)介紹了C++與C的區(qū)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 利用Qt自帶的媒體模塊實(shí)現(xiàn)播放mp4文件

    利用Qt自帶的媒體模塊實(shí)現(xiàn)播放mp4文件

    這篇文章主要為大家詳細(xì)介紹了如何使用Qt自帶的媒體模塊,播放mp4等媒體文件功能,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下
    2024-04-04
  • win10中的dlib庫安裝過程

    win10中的dlib庫安裝過程

    這篇文章主要介紹了win10中dlib庫的安裝過程,本文通過實(shí)例圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • C++中std::ios_base::floatfield報(bào)錯(cuò)已解決

    C++中std::ios_base::floatfield報(bào)錯(cuò)已解決

    在C++編程中,設(shè)置浮點(diǎn)數(shù)輸出格式時(shí)可能遇到std::ios_base::floatfield錯(cuò)誤,解決方法包括使用正確的格式化標(biāo)志組合,避免沖突的格式化設(shè)置,以及檢查流狀態(tài)標(biāo)志是否正確,通過這些方法可以有效避免浮點(diǎn)數(shù)格式化錯(cuò)誤,并確保輸出精確
    2024-09-09

最新評(píng)論