0
点赞
收藏
分享

微信扫一扫

stl vector 笔记


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;

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

其他 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.



举报

相关推荐

STL--vector笔记

STL Vector

STL--vector

stl---vector

STL之vector

STL List and Vector 总结

0 条评论