stl vector 筆記

來源:互聯網
上載者:User

csdn    lidp    http://blog.csdn.net/perfectpdl 

Vector 為類模板

使用例子:

#include <iostream>#include <vector>#include <string>using namespace std;int main(int argc , char *argv[]){   vector<string> SS;   SS.push_back("The number is 10");   SS.push_back("The number is 20");   SS.push_back("The number is 30");   cout << "Loop by index:" << endl;   int ii;   for(ii=0; ii < SS.size(); ii++)   {      cout << SS[ii] << endl;   }   cout << endl << "Constant Iterator:" << endl;   vector<string>::const_iterator cii;   for(cii=SS.begin(); cii!=SS.end(); cii++)   {      cout << *cii << endl;   }   cout << endl << "Reverse Iterator:" << endl;   vector<string>::reverse_iterator rii;   for(rii=SS.rbegin(); rii!=SS.rend(); ++rii)   {      cout << *rii << endl;   }   cout << endl << "Sample Output:" << endl;   cout << SS.size() << endl;   cout << SS[2] << endl;   swap(SS[0], SS[2]);   cout << SS[2] << endl;   return 0;}

編譯: g++ example.cpp 

需要注意的是 end() 指向最後一個資料的下一個指標,所以不能指向最後一個元素,如:

iter_jj = SS.end();cout << *iter_jj << endl;

這會導致  "Segmentation fault" error. 端錯誤。

stl vector 實現了動態數組功能,可以儲存 基本資料,對象,與list同屬於線性容器,優點是擷取對象快,缺點是尋找慢,

stl vector 模板提供了大量的操作vetror 方法, 包括 初始化,插入,刪除,容量相關的方法, 另一塊為遍曆vector介面iterator.

下面附上 vector模板提供的介面:

 

建構函式:

Method/operator Description
vector<T> v; Vector declaration of data type "T".
vector<T> v(size_type n); Declaration of vector containing type "T" and of size "n" (quantity).
vector<T> v(size_type n,const T& t); Declaration of vector containing type "T", of size "n" (quantity) containing value "t".
Declaration: vector(size_type n, const T& t)
vector<T> v(begin_iterator,end_iterator); Copy of Vector of data type "T" and range begin_iterator to end_iterator.
Declaration: template vector(InputIterator, InputIterator)

與 Size 相關的 methods/operators:

Method/operator Description
empty() Returns bool (true/false). True if empty.
Declaration: bool empty() const
size() Number of elements of vector.
Declaration: size_type size() const
resize(n, t=T()) Adjust by adding or deleting elements of vector so that its size is "n".
Declaration: void resize(n, t = T())
capacity() Max number of elements of vector before reallocation.
Declaration: size_type capacity() const
reserve(size_t n) Max number of elements of vector set to "n" before reallocation.
Declaration: void reserve(size_t)
max_size() Max number of elements of vector possible.
Declaration: size_type max_size() const

Note: size_type is an unsigned integer.

其他 methods/operators:

Method/operator Description
erase()
clear()
Erase all elements of vector.
Declaration: void clear()
erase(iterator)
erase(begin_iterator,end_iterator)
Erase element of vector. Returns iterator to next element.
Erase element range of vector. Returns iterator to next element.
Declarations:

  • iterator erase(iterator pos)
  • iterator erase(iterator first, iterator last)
=
Example: X=Y()
Assign/copy entire contents of one vector into another.
Declaration: vector& operator=(const vector&)
< Comparison of one vector to another.
Declaration: bool operator<(const vector&, const vector&)
== Returns bool. True if every element is equal.
Declaration: bool operator==(const vector&, const vector&)
at(index)
v[index]
Element of vector. Left and Right value assignment: v.at(i)=e; and e=v.at(i);
Declaration: reference operator[](size_type n)
front()
v[0]
First element of vector. (Left and Right value assignment.)
Declaration: reference front()
back() Last element of vector. (Left and Right value assignment.)
Declaration: reference back()
push_back(const T& value) Add element to end of vector.
Declaration: void push_back(const T&)
pop_back() Remove element from end of vector.
Declaration: void pop_back()
assign(size_type n,const T& t) Assign first n elements a value "t".
assign(begin_iterator,end_iterator) Replace data in range defined by iterators.
Declaration:
insert(iterator, const T& t) Insert at element "iterator", element of value "t".
Declaration: iterator insert(iterator pos, const T& x)
insert(iterator pos, size_type n, const T& x) Starting before element "pos", insert first n elements of value "x".
Declaration: void insert(iterator pos, size_type n, const T& x)
insert(iterator pos, begin_iterator,end_iterator) Starting before element "pos", insert range begin_iterator to end_iterator.
Declaration: void insert(iterator pos, InputIterator f, InputIterator l)
swap(vector& v2) Swap contents of two vectors.
Declaration: void swap(vector&)

遍曆vector(Iterator) methods/operators: 迭代器

Method/operator Description
begin() Return iterator to first element of vector.
Declaration: const_iterator begin() const
end() Return iterator to end of vector (not last element of vector but past last element)
Declaration: const_iterator end() const
rbegin() Return iterator to first element of vector (reverse order).
Declaration: const_reverse_iterator rbegin() const
rend() Return iterator to end of vector (not last element but past last element) (reverse order).
Declaration: const_reverse_iterator rend() const
++ Increment iterator.
-- Decrement iterator.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.