vector 類別

C++ 標準程式庫向量類別是序列容器的類別範本。 向量會將指定類型的專案儲存線上性排列中,並允許快速隨機存取任何專案。 當隨機存取效能處於進階階段時,向量是序列的慣用容器。

語法

template <class Type, class Allocator = allocator<Type>>
class vector

參數

Type
要儲存在向量中的項目資料類型

Allocator
代表預存配置器物件的類型,封裝有關向量之記憶體配置和解除配置的詳細資料。 這個引數是選擇性的,而且預設值是 allocator<Type>

備註

向量可在序列結尾處插入和刪除常數時間。 在向量中間插入或刪除項目需要線性時間。 類別 deque 容器在序列開頭和結尾的插入和刪除速度較快。 類別 list 容器在序列內的任何位置插入和刪除時會更快。

當成員函式必須將向量物件中包含的序列增加到超過其目前的儲存容量時,就會發生向量重新配置。 其他的插入和清除可能會改變序列中的各種儲存空間位址。 在這些情況下,指向改變之序列位置的迭代器或參考會變成無效。 如果沒有發生重新配置,只有插入/刪除點之前的迭代器和參考仍有效。

類別 vector<bool> 是 類型 bool 元素之類別樣板向量的完整特製化。 它有特製化所使用的基礎型別配置器。

vector<bool> 參考類別是巢狀類別 ,其物件可以提供物件內元素(單一 vector<bool> 位)的參考。

成員

建構函式

名稱 描述
vector 建構特定大小、具有特定值項目或具有特定 allocator 的向量,或將向量建構為其他一些向量的複本。

Typedefs

名稱 描述
[allocator_type](#allocator_type) 代表向量物件之 allocator 類別的類型。
const_iterator 提供可讀取向量中 const 項目之隨機存取迭代器的類型。
const_pointer 提供向量中 const 項目之指標的類型。
const_reference 型別,提供儲存在向量中之專案的參考 const 。 它用於讀取和執行 const 作業。
const_reverse_iterator 提供可讀取向量中任何 const 項目之隨機存取迭代器的類型。
difference_type 提供向量中兩個項目位址之間差異的類型。
iterator 提供可讀取或修改向量中任何項目之隨機存取迭代器的類型。
pointer 提供向量中某個項目指標的類型。
reference 提供儲存在向量中之項目參考的類型。
reverse_iterator 提供可讀取或修改反向向量中任何項目之隨機存取迭代器的類型。
size_type 計算向量中項目數的類型。
value_type 代表儲存在向量中之資料類型的類型。

函式

名稱 描述
assign 清除向量,並將指定的項目複製到空向量。
at 傳回向量中指定位置的項目參考。
back 傳回向量的最後一個項目參考。
begin 傳回向量中第一個項目的隨機存取迭代器。
capacity 傳回向量可包含而不需要配置更多儲存空間的項目數。
cbegin 傳回向量中第一個項目的隨機存取常數迭代器。
cend 傳回隨機存取常數迭代器,指向向量結尾的後一個項目。
crbegin 將常數迭代器傳回至反向向量中的第一個項目。
crend 將常數迭代器傳回至反向向量的結尾。
clear 清除向量的項目。
data 傳回向量中第一個項目的指標。
emplace 將就地建構的項目插入向量的指定位置。
emplace_back 將就地建構的項目加入向量的結尾。
empty 測試向量容器是否空白。
end 傳回隨機存取迭代器,指向向量的結尾。
erase 從向量的指定位置移除一個項目或一定範圍的項目。
front 傳回向量中第一個項目的參考。
get_allocator 將物件傳回至向量所使用的 allocator 類別。
insert 將專案或許多專案插入向量中指定的位置。
max_size 傳回向量的最大長度。
pop_back 刪除向量結尾的元素。
push_back 將項目加入向量的結尾。
rbegin 傳回反向向量中第一個項目的迭代器。
rend 將迭代器傳回至反向向量的結尾。
reserve 為向量物件保留最小儲存空間長度。
resize 指定向量的新大小。
shrink_to_fit 捨棄多餘的容量。
size 傳回向量中的項目數。
swap 交換兩個向量的項目。

操作員

名稱 描述
operator[] 傳回在指定位置上 vector 項目的參考。
operator= 以另一個向量的複本取代向量的項目。

allocator_type

代表向量物件之配置器類別的類型。

typedef Allocator allocator_type;

備註

allocator_type 與樣板參數 Allocator 同義。

範例

如需使用 allocator_type 的範例 get_allocator ,請參閱 範例。

assign

清除向量,並將指定的項目複製到空向量。

void assign(size_type count, const Type& value);
void assign(initializer_list<Type> init_list);

template <class InputIterator>
void assign(InputIterator first, InputIterator last);

參數

first
項目範圍中要複製的第一個項目位置。

last
項目範圍之外要複製的第一個項目位置。

count
插入向量的項目複本數目。

value
插入向量之項目的值。

init_list
包含要插入之項目的 initializer_list。

備註

首先, assign 清除向量中的任何現有專案。 然後, assign 將原始向量中的指定專案範圍插入向量,或是將新指定值專案的複本插入向量中。

範例

/ vector_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> v1, v2, v3;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    cout << "v1 = ";
    for (auto& v : v1){
        cout << v << " ";
    }
    cout << endl;

    v2.assign(v1.begin(), v1.end());
    cout << "v2 = ";
    for (auto& v : v2){
        cout << v << " ";
    }
    cout << endl;

    v3.assign(7, 4);
    cout << "v3 = ";
    for (auto& v : v3){
        cout << v << " ";
    }
    cout << endl;

    v3.assign({ 5, 6, 7 });
    for (auto& v : v3){
        cout << v << " ";
    }
    cout << endl;
}

at

傳回向量中指定位置的項目參考。

reference at(size_type position);

const_reference at(size_type position) const;

參數

position
向量中要參考之項目的註標或位置編號。

傳回值

引數中加上註標的項目參考。 如果 position 大於向量的大小, at 則會擲回例外狀況。

備註

如果 的傳回值 at 指派給 const_reference ,則無法修改向量物件。 如果 at 的傳回值指派給 reference,則可以修改向量物件。

範例

// vector_at.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 20 );

   const int &i = v1.at( 0 );
   int &j = v1.at( 1 );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;
}
The first element is 10
The second element is 20

back

傳回向量的最後一個項目參考。

reference back();

const_reference back() const;

傳回值

向量的最後一個項目。 如果向量是空的,則傳回值不明。

備註

如果 的傳回值 back 指派給 const_reference ,則無法修改向量物件。 如果 back 的傳回值指派給 reference,則可以修改向量物件。

使用 _ITERATOR_DEBUG_LEVEL 定義為 1 或 2 進行編譯時,如果您嘗試存取空向量中的專案,就會發生執行階段錯誤。 如需詳細資訊,請參閱 已檢查的反覆運算器

範例

// vector_back.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 11 );

   int& i = v1.back( );
   const int& ii = v1.front( );

   cout << "The last integer of v1 is " << i << endl;
   i--;
   cout << "The next-to-last integer of v1 is "<< ii << endl;
}

begin

傳回向量中第一個項目的隨機存取迭代器。

const_iterator begin() const;

iterator begin();

傳回值

隨機存取迭代器,定址對象是 vector 中的第一個元素,或空 vector 後的位置。 請一律比較傳 vector::end 回的值,以確保其有效。

備註

如果 的傳回值 begin 指派給 vector::const_iteratorvector 則無法修改 物件。 如果將 的 begin 傳回值指派給 vector::iteratorvector 則可以修改 物件。

範例

// vector_begin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> c1;
    vector<int>::iterator c1_Iter;
    vector<int>::const_iterator c1_cIter;

    c1.push_back(1);
    c1.push_back(2);

    cout << "The vector c1 contains elements:";
    c1_Iter = c1.begin();
    for (; c1_Iter != c1.end(); c1_Iter++)
    {
        cout << " " << *c1_Iter;
    }
    cout << endl;

    cout << "The vector c1 now contains elements:";
    c1_Iter = c1.begin();
    *c1_Iter = 20;
    for (; c1_Iter != c1.end(); c1_Iter++)
    {
        cout << " " << *c1_Iter;
    }
    cout << endl;

    // The following line would be an error because iterator is const
    // *c1_cIter = 200;
}
The vector c1 contains elements: 1 2
The vector c1 now contains elements: 20 2

capacity

傳回向量可包含而不需要配置更多儲存空間的項目數。

size_type capacity() const;

傳回值

目前配置給向量的儲存空間長度。

備註

如果配置足夠的記憶體來容納成員函式,則成員函 resize 式會更有效率。 使用成員函式 reserve 來指定配置的記憶體數量。

範例

// vector_capacity.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 1 );
   cout << "The length of storage allocated is "
        << v1.capacity( ) << "." << endl;

   v1.push_back( 2 );
   cout << "The length of storage allocated is now "
        << v1.capacity( ) << "." << endl;
}
The length of storage allocated is 1.
The length of storage allocated is now 2.

cbegin

傳回 const 迭代器,為範圍中的第一個項目定址。

const_iterator cbegin() const;

傳回值

const 隨機存取迭代器,指向範圍的第一個項目,或指向空白範圍結尾 (空白範圍 cbegin() == cend()) 之外的位置。

備註

使用 的 cbegin 傳回值,就無法修改範圍中的專案。

您可以使用此成員函式取代 begin() 成員函式,以確保傳回值是 const_iterator。 一般而言,它會與類型推算關鍵字搭配 auto 使用,如下列範例所示。 在此範例中,請考慮將 Container 視為任何支援 begin()cbegin() 且可修改 (非 const) 的容器類型。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

傳回指向向量最後一 const 個專案後面的元素的過去端反覆運算器。

const_iterator cend() const;

傳回值

const向量的過去反覆運算器。 它會指向向量最後一個專案後面的元素。 該專案是預留位置,不應該取值。 請只將其用於比較。 如果向量是空的,則 vector::cend() == vector::cbegin() 為 。

備註

cend 用來測試迭代器是否已超過其範圍結尾。

您可以使用此成員函式取代 end() 成員函式,以確保傳回值是 const_iterator。 一般而言,它會與類型推算關鍵字搭配 auto 使用,如下列範例所示。 在此範例中,請考慮將 Container 視為任何支援 end()cend() 且可修改 (非 const) 的容器類型。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

cend 傳回的值不應該取值。 請只將其用於比較。

clear

清除向量的項目。

void clear();

範例

// vector_clear.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );

   cout << "The size of v1 is " << v1.size( ) << endl;
   v1.clear( );
   cout << "The size of v1 after clearing is " << v1.size( ) << endl;
}
The size of v1 is 3
The size of v1 after clearing is 0

const_iterator

提供可讀取向量中 const 項目之隨機存取迭代器的類型。

typedef implementation-defined const_iterator;

備註

類型 const_iterator 無法用來修改專案的值。

範例

如需使用 const_iterator 的範例,請參閱 back 的範例。

const_pointer

提供向量中 const 項目之指標的類型。

typedef typename Allocator::const_pointer const_pointer;

備註

類型 const_pointer 無法用來修改專案的值。

迭代器通常可用來存取向量項目。

const_reference

型別,提供儲存在向量中之專案的參考 const 。 它用於讀取和執行 const 作業。

typedef typename Allocator::const_reference const_reference;

備註

類型 const_reference 無法用來修改專案的值。

範例

// vector_const_ref.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 20 );

   const vector <int> v2 = v1;
   const int &i = v2.front( );
   const int &j = v2.back( );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;

   // The following line would cause an error as v2 is const
   // v2.push_back( 30 );
}
The first element is 10
The second element is 20

const_reverse_iterator

提供可讀取向量中任何 const 項目之隨機存取迭代器的類型。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

備註

類型 const_reverse_iterator 無法修改專案的值,並且用來反向逐一查看向量。

範例

如需如何宣告和使用反覆運算器的範例,請參閱 rbegin

crbegin

將常數迭代器傳回至反向向量中的第一個項目。

const_reverse_iterator crbegin() const;

傳回值

常數反向隨機存取反覆運算器,定址反轉 vector 中的第一個專案,或定址未反 vector 轉 中的最後一個專案。

備註

使用 的 crbegin 傳回值, vector 就無法修改 物件。

範例

// vector_crbegin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator v1_Iter;
   vector <int>::const_reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   v1_Iter = v1.begin( );
   cout << "The first element of vector is "
        << *v1_Iter << "." << endl;

   v1_rIter = v1.crbegin( );
   cout << "The first element of the reversed vector is "
        << *v1_rIter << "." << endl;
}
The first element of vector is 1.
The first element of the reversed vector is 2.

crend

傳回指向反轉向量最後一 const 個專案後面的元素的過去反向反覆運算器。

const_reverse_iterator crend() const;

傳回值

const反向向量的反向過去反覆運算器。 它會指向反轉向量最後一個專案後面的元素,這與非反轉向量的第一個專案之前的專案相同。 該專案是預留位置,不應該取值。 請只將其用於比較。

備註

crend與 反轉 vector 搭配使用,就像 搭配 使用一 vectorvector::cend

使用 的傳回值 crend (適當遞減), vector 就無法修改物件。

crend 可以用來測試反轉迭代器是否已到達其 vector 的結尾。

crend 傳回的值不應該取值。 請只將其用於比較。

範例

// vector_crend.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::const_reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   for ( v1_rIter = v1.rbegin( ) ; v1_rIter != v1.rend( ) ; v1_rIter++ )
      cout << *v1_rIter << endl;
}
2
1

data

傳回向量中第一個項目的指標。

const_pointer data() const;

pointer data();

傳回值

中第一個專案的 vector 指標,或是空 vector 的 後置位置指標。

範例

// vector_data.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> c1;
    vector<int>::pointer c1_ptr;
    vector<int>::const_pointer c1_cPtr;

    c1.push_back(1);
    c1.push_back(2);

    cout << "The vector c1 contains elements:";
    c1_cPtr = c1.data();
    for (size_t n = c1.size(); 0 < n; --n, c1_cPtr++)
    {
        cout << " " << *c1_cPtr;
    }
    cout << endl;

    cout << "The vector c1 now contains elements:";
    c1_ptr = c1.data();
    *c1_ptr = 20;
    for (size_t n = c1.size(); 0 < n; --n, c1_ptr++)
    {
        cout << " " << *c1_ptr;
    }
    cout << endl;
}
The vector c1 contains elements: 1 2
The vector c1 now contains elements: 20 2

difference_type

提供參考相同向量中項目之兩個迭代器間差異的類型。

typedef typename Allocator::difference_type difference_type;

備註

difference_type 也可以描述為兩個指標之間的項目數,因為項目的指標包含其位址。

迭代器通常可用來存取向量項目。

範例

// vector_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <vector>
#include <algorithm>

int main( )
{
   using namespace std;

   vector <int> c1;
   vector <int>::iterator c1_Iter, c2_Iter;

   c1.push_back( 30 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 10 );
   c1.push_back( 30 );
   c1.push_back( 20 );

   c1_Iter = c1.begin( );
   c2_Iter = c1.end( );

   vector <int>::difference_type   df_typ1, df_typ2, df_typ3;

   df_typ1 = count( c1_Iter, c2_Iter, 10 );
   df_typ2 = count( c1_Iter, c2_Iter, 20 );
   df_typ3 = count( c1_Iter, c2_Iter, 30 );
   cout << "The number '10' is in c1 collection " << df_typ1 << " times.\n";
   cout << "The number '20' is in c1 collection " << df_typ2 << " times.\n";
   cout << "The number '30' is in c1 collection " << df_typ3 << " times.\n";
}
The number '10' is in c1 collection 1 times.
The number '20' is in c1 collection 2 times.
The number '30' is in c1 collection 3 times.

emplace

將就地建構的項目插入向量的指定位置。

template <class... Types>
iterator emplace(
    const_iterator position,
    Types&&... args);

參數

position
插入第一個專案的位置 vector

args
建構函式引數。 函式會根據所提供的引數推斷要叫用的建構函式多載。

傳回值

函式會傳回迭代器,指向新項目插入 vector 的位置。

備註

任何插入作業都可能很昂貴,請參閱 vector 類別 以取得效能的討論 vector

範例

// vector_emplace.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter;

   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );

   cout << "v1 =" ;
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

// initialize a vector of vectors by moving v1
   vector < vector <int> > vv1;

   vv1.emplace( vv1.begin(), move( v1 ) );
   if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
      {
      cout << "vv1[0] =";
      for (Iter = vv1[0].begin( ); Iter != vv1[0].end( ); Iter++ )
         cout << " " << *Iter;
      cout << endl;
      }
}
v1 = 10 20 30
vv1[0] = 10 20 30

emplace_back

將就地建構的項目加入向量的結尾。

template <class... Types>
void emplace_back(Types&&... args);

參數

args
建構函式引數。 函式會根據所提供的引數推斷要叫用的建構函式多載。

範例

#include <vector>
struct obj
{
   obj(int, double) {}
};

int main()
{
   std::vector<obj> v;
   v.emplace_back(1, 3.14); // obj in created in place in the vector
}

empty

測試是否為空的向量。

bool empty() const;

傳回值

true 如果向量是空的,則為 ; false 如果向量不是空的,則為 。

範例

// vector_empty.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );

   if ( v1.empty( ) )
      cout << "The vector is empty." << endl;
   else
      cout << "The vector is not empty." << endl;
}
The vector is not empty.

end

傳回指向向量最後一個專案後面的元素的過去端反覆運算器。

iterator end();

const_iterator end() const;

傳回值

向量的過去反覆運算器。 它會指向向量最後一個專案後面的元素。 該專案是預留位置,不應該取值。 請只將其用於比較。 如果向量是空的,則 vector::end() == vector::begin() 為 。

備註

如果 的傳回值 end 指派給 類型的 const_iterator 變數,則無法修改向量物件。 如果將 的 end 傳回值指派給 類型的 iterator 變數,則可以修改向量物件。

範例

// vector_end.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator v1_Iter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   for ( v1_Iter = v1.begin( ) ; v1_Iter != v1.end( ) ; v1_Iter++ )
      cout << *v1_Iter << endl;
}
1
2

erase

從向量的指定位置移除一個項目或一定範圍的項目。

iterator erase(
    const_iterator position);

iterator erase(
    const_iterator first,
    const_iterator last);

參數

position
要從向量中移除之項目的位置。

first
從向量中移除之第一個項目的位置。

last
從向量中移除的最後一個項目之後的位置。

傳回值

迭代器,指定任何移除的項目之後的第一個剩餘項目;如果沒有這個項目,則為向量結尾的指標。

範例

// vector_erase.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter;

   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );
   v1.push_back( 40 );
   v1.push_back( 50 );

   cout << "v1 =" ;
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   v1.erase( v1.begin( ) );
   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   v1.erase( v1.begin( ) + 1, v1.begin( ) + 3 );
   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;
}
v1 = 10 20 30 40 50
v1 = 20 30 40 50
v1 = 20 50

front

傳回向量中第一個項目的參考。

reference front();

const_reference front() const;

傳回值

向量物件中第一個項目的參考。 如果向量是空的,則傳回不明。

備註

如果 的傳回值 front 指派給 const_reference ,則無法修改向量物件。 如果 front 的傳回值指派給 reference,則可以修改向量物件。

使用 _ITERATOR_DEBUG_LEVEL 定義為 1 或 2 進行編譯時,如果您嘗試存取空向量中的專案,就會發生執行階段錯誤。 如需詳細資訊,請參閱 已檢查的反覆運算器

範例

// vector_front.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 11 );

   int& i = v1.front( );
   const int& ii = v1.front( );

   cout << "The first integer of v1 is "<< i << endl;
   // by incrementing i, we move the front reference to the second element
   i++;
   cout << "Now, the first integer of v1 is "<< i << endl;
}

get_allocator

傳回一份用來建構向量的配置器物件複本。

Allocator get_allocator() const;

傳回值

向量所使用的配置器。

備註

供 vector 類別用於指定類別如何管理儲存的配置器。 C++ 標準程式庫容器類別隨附的預設配置器,足以滿足大多數程式設計需求。 撰寫和使用您自己的配置器類別是進階 C++ 功能。

範例

// vector_get_allocator.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   // The following lines declare objects that use the default allocator.
   vector<int> v1;
   vector<int, allocator<int> > v2 = vector<int, allocator<int> >(allocator<int>( )) ;

   // v3 will use the same allocator class as v1
   vector <int> v3( v1.get_allocator( ) );

   vector<int>::allocator_type xvec = v3.get_allocator( );
   // You can now call functions on the allocator class used by vec
}

insert

將專案或許多專案或專案範圍插入向量中指定的位置。

iterator insert(
    const_iterator position,
    const Type& value);

iterator insert(
    const_iterator position,
    Type&& value);

void insert(
    const_iterator position,
    size_type count,
    const Type& value);

template <class InputIterator>
void insert(
    const_iterator position,
    InputIterator first,
    InputIterator last);

參數

position
第一個項目插入向量中的位置。

value
插入向量之項目的值。

count
插入向量的項目數。

first
要複製的元素範圍中第一個元素的位置。

last
超出要複製之元素範圍的第一個元素的位置。

傳回值

前兩個 insert 函式會傳回迭代器,指向新項目插入向量的位置。

備註

前提是 firstlast 不得為向量中的迭代器,否則行為未定義。 任何插入作業都可能很昂貴,請參閱 vector 類別 以取得效能的討論 vector

範例

// vector_insert.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter;

   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );

   cout << "v1 =" ;
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   v1.insert( v1.begin( ) + 1, 40 );
   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;
   v1.insert( v1.begin( ) + 2, 4, 50 );

   cout << "v1 =";
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

   const auto v2 = v1;
   v1.insert( v1.begin( )+1, v2.begin( )+2, v2.begin( )+4 );
   cout << "v1 =";
   for (Iter = v1.begin( ); Iter != v1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

// initialize a vector of vectors by moving v1
   vector < vector <int> > vv1;

   vv1.insert( vv1.begin(), move( v1 ) );
   if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
      {
      cout << "vv1[0] =";
      for (Iter = vv1[0].begin( ); Iter != vv1[0].end( ); Iter++ )
         cout << " " << *Iter;
      cout << endl;
      }
}
v1 = 10 20 30
v1 = 10 40 20 30
v1 = 10 40 50 50 50 50 20 30
v1 = 10 50 50 40 50 50 50 50 20 30
vv1[0] = 10 50 50 40 50 50 50 50 20 30

iterator

提供可讀取或修改向量中任何項目之隨機存取迭代器的類型。

typedef implementation-defined iterator;

備註

iterator 別可用來修改專案的值。

範例

請參閱 的 begin 範例。

max_size

傳回向量的最大長度。

size_type max_size() const;

傳回值

向量的最大可能長度。

範例

// vector_max_size.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::size_type i;

   i = v1.max_size( );
   cout << "The maximum possible length of the vector is " << i << "." << endl;
}

operator[]

傳回在指定位置上 vector 項目的參考。

reference operator[](size_type position);

const_reference operator[](size_type position) const;

參數

position
vector 項目的位置。

傳回值

如果指定的位置大於或等於容器大小,則結果是未定義。

備註

如果 的傳回值 operator[] 指派給 const_reference ,則無法修改向量物件。 如果 operator[] 的傳回值已指派給參考,則可以修改向量物件。

使用 定義為 1 或 2 進行 _ITERATOR_DEBUG_LEVEL 編譯時,如果您嘗試存取向量界限外的元素,就會發生執行階段錯誤。 如需詳細資訊,請參閱 已檢查的反覆運算器

範例

// vector_op_ref.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;

   v1.push_back( 10 );
   v1.push_back( 20 );

   int& i = v1[1];
   cout << "The second integer of v1 is " << i << endl;
}

operator=

以另一個向量的複本取代向量的項目。

vector& operator=(const vector& right);

vector& operator=(vector&& right);

參數

right
vector要複製到 的 vector

備註

清除 vector 中的任何現有項目之後,operator= 會將 right 的內容複製或移到 vector 中。

範例

// vector_operator_as.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector<int> v1, v2, v3;
   vector<int>::iterator iter;

   v1.push_back(10);
   v1.push_back(20);
   v1.push_back(30);
   v1.push_back(40);
   v1.push_back(50);

   cout << "v1 = " ;
   for (iter = v1.begin(); iter != v1.end(); iter++)
      cout << *iter << " ";
   cout << endl;

   v2 = v1;
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;

// move v1 into v2
   v2.clear();
   v2 = move(v1);
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;
}

pointer

提供向量中某個項目指標的類型。

typedef typename Allocator::pointer pointer;

備註

pointer 別可用來修改專案的值。

範例

// vector_pointer.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
    using namespace std;
    vector<int> v;
    v.push_back( 11 );
    v.push_back( 22 );

    vector<int>::pointer ptr = &v[0];
    cout << *ptr << endl;
    ptr++;
    cout << *ptr << endl;
    *ptr = 44;
    cout << *ptr << endl;
}
11
22
44

pop_back

刪除向量結尾的元素。

void pop_back();

備註

如需程式碼範例,請參閱 vector::push_back()

push_back

將元素加入至向量結尾。

void push_back(const T& value);

void push_back(T&& value);

參數

value
要指派給加入到向量結尾之元素的值。

範例

// compile with: /EHsc /W4
#include <vector>
#include <iostream>

using namespace std;

template <typename T> void print_elem(const T& t) {
    cout << "(" << t << ") ";
}

template <typename T> void print_collection(const T& t) {
    cout << "  " << t.size() << " elements: ";

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

int main()
{
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(10 + i);
    }

    cout << "vector data: " << endl;
    print_collection(v);

    // pop_back() until it's empty, printing the last element as we go
    while (v.begin() != v.end()) {
        cout << "v.back(): "; print_elem(v.back()); cout << endl;
        v.pop_back();
    }
}

rbegin

傳回反向向量中第一個項目的迭代器。

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

傳回值

反向隨機存取迭代器,其定址反向向量中的第一個項目,或定址未反向向量中的最後一個項目。

備註

如果 的傳回值 rbegin 指派給 const_reverse_iterator ,則無法修改向量物件。 如果 rbegin 的傳回值指派給 reverse_iterator,則可以修改向量物件。

範例

// vector_rbegin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator v1_Iter;
   vector <int>::reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   v1_Iter = v1.begin( );
   cout << "The first element of vector is "
        << *v1_Iter << "." << endl;

   v1_rIter = v1.rbegin( );
   cout << "The first element of the reversed vector is "
        << *v1_rIter << "." << endl;
}
The first element of vector is 1.
The first element of the reversed vector is 2.

reference

提供儲存在向量中之項目參考的類型。

typedef typename Allocator::reference reference;

範例

如需如何在向量類別中使用的 reference 範例,請參閱 at

rend

傳回指向反轉向量最後一個專案後面的元素的過去反向反覆運算器。

const_reverse_iterator rend() const;
reverse_iterator rend();

傳回值

反向向量的反向過去反覆運算器。 它會指向反轉向量最後一個專案後面的元素,這與非反轉向量的第一個專案之前的專案相同。 該專案是預留位置,不應該取值。 請只將其用於比較。

備註

rend 與反向向量搭配使用,就像與向量一樣 end

如果 的傳回值 rend 指派給 const_reverse_iterator ,則無法修改向量物件。 如果 rend 的傳回值指派給 reverse_iterator,則可以修改向量物件。

rend 可以用來測試反轉迭代器是否已到達其陣列的結尾。

rend 傳回的值不應該取值。 請只將其用於比較。

範例

// vector_rend.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   for ( v1_rIter = v1.rbegin( ) ; v1_rIter != v1.rend( ) ; v1_rIter++ )
      cout << *v1_rIter << endl;
}
2
1

reserve

為向量物件保留最小儲存空間長度,並在必要時配置空間。

void reserve(size_type count);

參數

count
配置給向量的最小儲存空間長度。

範例

// vector_reserve.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   //vector <int>::iterator Iter;

   v1.push_back( 1 );
   cout << "Current capacity of v1 = "
      << v1.capacity( ) << endl;
   v1.reserve( 20 );
   cout << "Current capacity of v1 = "
      << v1.capacity( ) << endl;
}
Current capacity of v1 = 1
Current capacity of v1 = 20

resize

指定向量的新大小。

void resize(size_type new_size);
void resize(size_type new_size, Type value);

參數

new_size
向量的新大小。

value
如果新的大小大於原始大小,則已將新元素的初始化值加入至向量。 如果省略此值,則新的物件會使用其預設建構函式。

備註

如果容器的大小小於要求的大小,請將 new_sizeresize 專案加入向量,直到達到要求的大小為止。 當容器的大小大於要求的大小時, resize 會刪除最接近容器結尾的專案,直到達到大小 new_size 為止。 如果容器的目前大小與要求的大小相同,則不會採取任何動作。

size 反映向量目前的大小。

範例

// vectorsizing.cpp
// compile with: /EHsc /W4
// Illustrates vector::reserve, vector::max_size,
// vector::resize, vector::resize, and vector::capacity.
//
// Functions:
//
//    vector::max_size - Returns maximum number of elements vector could
//                       hold.
//
//    vector::capacity - Returns number of elements for which memory has
//                       been allocated.
//
//    vector::size - Returns number of elements in the vector.
//
//    vector::resize - Reallocates memory for vector, preserves its
//                     contents if new size is larger than existing size.
//
//    vector::reserve - Allocates elements for vector to ensure a minimum
//                      size, preserving its contents if the new size is
//                      larger than existing size.
//
//    vector::push_back - Appends (inserts) an element to the end of a
//                        vector, allocating memory for it if necessary.
//
//////////////////////////////////////////////////////////////////////

// The debugger cannot handle symbols more than 255 characters long.
// The C++ Standard Library often creates symbols longer than that.
// The warning can be disabled:
//#pragma warning(disable:4786)

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c) {
    cout << s;

    for (const auto& e : c) {
        cout << e << " ";
    }
    cout << endl;
}

void printvstats(const vector<int>& v) {
    cout << "   the vector's size is: " << v.size() << endl;
    cout << "   the vector's capacity is: " << v.capacity() << endl;
    cout << "   the vector's maximum size is: " << v.max_size() << endl;
}

int main()
{
    // declare a vector that begins with 0 elements.
    vector<int> v;

    // Show statistics about vector.
    cout << endl << "After declaring an empty vector:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    // Add one element to the end of the vector.
    v.push_back(-1);
    cout << endl << "After adding an element:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    for (int i = 1; i < 10; ++i) {
        v.push_back(i);
    }
    cout << endl << "After adding 10 elements:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(6);
    cout << endl << "After resizing to 6 elements without an initialization value:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(9, 999);
    cout << endl << "After resizing to 9 elements with an initialization value of 999:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    v.resize(12);
    cout << endl << "After resizing to 12 elements without an initialization value:" << endl;
    printvstats(v);
    print("   the vector's contents: ", v);

    // Ensure there's room for at least 1000 elements.
    v.reserve(1000);
    cout << endl << "After vector::reserve(1000):" << endl;
    printvstats(v);

    // Ensure there's room for at least 2000 elements.
    v.resize(2000);
    cout << endl << "After vector::resize(2000):" << endl;
    printvstats(v);
}

reverse_iterator

提供可讀取或修改反向向量中任何項目之隨機存取迭代器的類型。

typedef std::reverse_iterator<iterator> reverse_iterator;

備註

類型 reverse_iterator 可用來反向逐一查看向量。

範例

請參閱 的 rbegin 範例。

shrink_to_fit

捨棄多餘的容量。

void shrink_to_fit();

範例

// vector_shrink_to_fit.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   //vector <int>::iterator Iter;

   v1.push_back( 1 );
   cout << "Current capacity of v1 = "
      << v1.capacity( ) << endl;
   v1.reserve( 20 );
   cout << "Current capacity of v1 = "
      << v1.capacity( ) << endl;
   v1.shrink_to_fit();
   cout << "Current capacity of v1 = "
      << v1.capacity( ) << endl;
}
Current capacity of v1 = 1
Current capacity of v1 = 20
Current capacity of v1 = 1

size

傳回向量中的項目數。

size_type size() const;

傳回值

向量的目前長度。

範例

// vector_size.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::size_type i;

   v1.push_back( 1 );
   i = v1.size( );
   cout << "Vector length is " << i << "." << endl;

   v1.push_back( 2 );
   i = v1.size( );
   cout << "Vector length is now " << i << "." << endl;
}
Vector length is 1.
Vector length is now 2.

size_type

計算向量中項目數的類型。

typedef typename Allocator::size_type size_type;

範例

請參閱 的 capacity 範例。

swap

交換兩個向量的項目。

void swap(
    vector<Type, Allocator>& right);

friend void swap(
    vector<Type, Allocator>& left,
    vector<Type, Allocator>& right);

參數

right
提供要交換之元素的向量。 或者,向量中的專案要與 向量 中的專案交換的向量 left

left
向量,其專案要與向量 right 中的專案交換。

範例

// vector_swap.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector <int> v1, v2;

   v1.push_back( 1 );
   v1.push_back( 2 );
   v1.push_back( 3 );

   v2.push_back( 10 );
   v2.push_back( 20 );

   cout << "The number of elements in v1 = " << v1.size( ) << endl;
   cout << "The number of elements in v2 = " << v2.size( ) << endl;
   cout << endl;

   v1.swap( v2 );

   cout << "The number of elements in v1 = " << v1.size( ) << endl;
   cout << "The number of elements in v2 = " << v2.size( ) << endl;
}
The number of elements in v1 = 3
The number of elements in v2 = 2

The number of elements in v1 = 2
The number of elements in v2 = 3

value_type

代表儲存在向量中之資料類型的類型。

typedef typename Allocator::value_type value_type;

備註

value_type 與樣板參數 Type 同義。

範例

// vector_value_type.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;
   vector<int>::value_type AnInt;
   AnInt = 44;
   cout << AnInt << endl;
}
44

vector

建構向量。 多載會建構特定大小的向量,或使用特定值的元素。 或者,做為其他向量之所有或部分的複本。 某些多載也可讓您指定要使用的配置器。

vector();
explicit vector(const Allocator& allocator);
explicit vector(size_type count);
vector(size_type count, const Type& value);
vector(size_type count, const Type& value, const Allocator& allocator);

vector(const vector& source);
vector(vector&& source);
vector(initializer_list<Type> init_list, const Allocator& allocator);

template <class InputIterator>
vector(InputIterator first, InputIterator last);
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& allocator);

參數

allocator
搭配這個物件使用的配置器類別。 get_allocator 會傳回 物件的配置器類別。

count
已建構向量中的項目數。

value
已建構向量中的項目值。

source
已建構向量為其複本的向量。

first
項目範圍中要複製的第一個項目位置。

last
項目範圍之外要複製的第一個項目位置。

init_list
initializer_list,包含要複製的專案。

備註

所有建構函式都會儲存配置器物件 ( allocator ) 並初始化向量。

前兩個建構函式會指定空的初始向量。 第二個建構函式會明確指定要使用的配置器類型 ( allocator )。

第三個建構函式會指定類別 count 之指定數目 (Type) 的項目重複預設值。

第四個和第五個建構函式會指定 值 的重複專案 countvalue

第六個建構函式會指定向量 source 的複本。

第七個建構函式會移動向量 source

第八個建構函式使用 initializer_list 來指定元素。

第九個和第十個建構函式會複製向量的範圍 [first, last)。

範例

// vector_ctor.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector <int>::iterator v1_Iter, v2_Iter, v3_Iter, v4_Iter, v5_Iter, v6_Iter;

    // Create an empty vector v0
    vector <int> v0;

    // Create a vector v1 with 3 elements of default value 0
    vector <int> v1(3);

    // Create a vector v2 with 5 elements of value 2
    vector <int> v2(5, 2);

    // Create a vector v3 with 3 elements of value 1 and with the allocator
    // of vector v2
    vector <int> v3(3, 1, v2.get_allocator());

    // Create a copy, vector v4, of vector v2
    vector <int> v4(v2);

    // Create a new temporary vector for demonstrating copying ranges
    vector <int> v5(5);
    for (auto i : v5) {
        v5[i] = i;
    }

    // Create a vector v6 by copying the range v5[ first,  last)
    vector <int> v6(v5.begin() + 1, v5.begin() + 3);

    cout << "v1 =";
    for (auto& v : v1){
        cout << " " << v;
    }
    cout << endl;

    cout << "v2 =";
    for (auto& v : v2){
        cout << " " << v;
    }
    cout << endl;

    cout << "v3 =";
    for (auto& v : v3){
        cout << " " << v;
    }
    cout << endl;
    cout << "v4 =";
    for (auto& v : v4){
        cout << " " << v;
    }
    cout << endl;

    cout << "v5 =";
    for (auto& v : v5){
        cout << " " << v;
    }
    cout << endl;

    cout << "v6 =";
    for (auto& v : v6){
        cout << " " << v;
    }
    cout << endl;

    // Move vector v2 to vector v7
    vector <int> v7(move(v2));
    vector <int>::iterator v7_Iter;

    cout << "v7 =";
    for (auto& v : v7){
        cout << " " << v;
    }
    cout << endl;

    cout << "v8 =";
    vector<int> v8{ { 1, 2, 3, 4 } };
    for (auto& v : v8){
        cout << " " << v ;
    }
    cout << endl;
}
v1 = 0 0 0
v2 = 2 2 2 2 2
v3 = 1 1 1
v4 = 2 2 2 2 2
v5 = 0 0 0 0 0
v6 = 0 0
v7 = 2 2 2 2 2
v8 = 1 2 3 4

另請參閱

C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考