vector

vector使用

​ 对于vector,它的存储结构和数组一样,连续空间。对于vector类型,它的数据成员有三个:

iterator start;
iterator finish;
iterator end_of_storage;

​ 其增长方式是两倍增长。由于存储空间连续,vector的迭代器本质上仍然是类型的指针。明白这点,以上三个数据成员就非常好理解。start,finsh分别表示正在使用的空间指针。而end_of_storage则表示已经申请到的空间的末尾。

构造、析构、以及赋值

  • (constructor)
    Construct vector (public member function )构造函数

    依据函数参数的分类 具体函数
    default (1) vector();
    explicit vector (const allocator_type& alloc);
    fill (2) explicit vector (size_type n, const allocator_type& alloc = allocator_type());
    vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
    range (3) template < class InputIterator>
    vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
    copy (4) vector (const vector& x);
    vector (const vector& x, const allocator_type& alloc);
    move (5) vector (vector&& x);
    vector (vector&& x, const allocator_type& alloc);
    initializer list (6) vector (initializer_list<value_type> il,const allocator_type& alloc = allocator_type());

    Note:

    • 拷贝构造函数(copy,构造函数参数包含vector)需要定义的值类型与拷贝的值类型一致,并且是深拷贝,会开辟新空间。
    • move型构造函数,移动构造函数,C++11新特性,可以减少内存的分配与释放。
  • (destructor)
    Vector destructor (public member function )

  • operator=
    Assign content (public member function )
    copy (1) vector& operator= (const vector& x);
    move (2) vector& operator= (vector&& x);
    initializer list (3) vector& operator= (initializer_list<value_type> il);

  • assign
    range (1) template
    void assign (InputIterator first, InputIterator last);
    fill (2) void assign (size_type n, const value_type& val);
    initializer list(3) void assign (initializer_list<value_type> il);

    普普通通的操作符重载以及赋值函数

元素访问

访问方式 具体函数
at() //带有边界检查的元素访问方式
reference at( size_type pos );
const_reference at( size_type pos ) const;
operator[] reference operator[ ] ( size_type pos );
const_reference operator[ ] ( size_type pos ) const;
front // 访问第一个元素
reference front();
const_reference front() const;
back //访问最后一个元素
reference back();
const_reference back() const;
data //直接访问隐藏的数组,注意与string不同这里有T * 的重载,而string只有返回const T* 的重载
T* data(); (since C++11)
const T* data() const;(since C++11)

迭代器

begin()
cbegin() //const iterator
returns an iterator to the beginning (public member function)
end()
cend() //const iterator
returns an iterator to the end (public member function)
rbegin()
crbegin() //const iterator
returns a reverse iterator to the beginning (public member function)
rend()
crend() //const iterator
returns a reverse iterator to the end (public member function)

容量与大小

Capacity
bool empty() const; checks whether the container is empty (public member function)
检验是否存储的数据为0个,若为0,返回true,否则返回false
size_type size() const; returns the number of elements (public member function)
当前存储的数据大小(个数)
size_type max_size() const; returns the maximum possible number of elements (public member function)
存储该类型的最大数目
void reserve( size_type new_cap ); reserves storage (public member function)
size_type capacity() const; returns the number of elements that can be held in currently allocated storage (public member function)
当前所申请的内存空间大小
void shrink_to_fit();
//(C++11)
reduces memory usage by freeing unused memory (public member function)
这个函数会申请当前大小的空间,并转移数据

vector的修改

Modifiers
void clear(); clears the contents (public member function)
void clear() noexcept;
清除过后,beign()==end(),但是size()==0,capacity()等于原来的空间
insert() inserts elements (public member function)
返回的是插入元素的位置(迭代器)
single element (1)
iterator insert (const_iterator position, const value_type& val);
fill (2)
iterator insert (const_iterator position, size_type n, const value_type& val);
range (3)
template < class InputIterator>
iterator insert (const_iterator position, InputIterator first,InputIterator last)
move (4)
iterator insert (const_iterator position, value_type&& val);
initializer list (5)
iterator insert (const_iterator position, initializer_list<value_type> il);
emplace()
//C++11
constructs element in-place (public member function)
根据要插入的位置,以及各参数,构造一个新元素插入,返回的是插入元素的位置(迭代器)
template< class… Args >
iterator emplace( const_iterator pos, Args&&… args );
erase() erases elements (public member function)
返回的是最后一个被删除元素后一个元素
1.single element
iterator erase( iterator pos );
iterator erase( const_iterator pos );
2.range
iterator erase (const_iterator first, const_iterator last);
iterator erase (const_iterator first, const_iterator last);
push_back() adds an element to the end (public member function)
void push_back (const value_type& val);
void push_back (value_type&& val);
emplace_back()
//C++11
constructs an element in-place at the end (public member function)
template <class… Args>
void emplace_back (Args&&… args);
pop_back() removes the last element (public member function)
void pop_back();
resize() changes the number of elements stored (public member function)
void resize (size_type n);
void resize (size_type n, const value_type& val)
swap() swaps the contents (public member function)
交换两个vector的内容
void swap (vector& x);

Reference

[1]https://www.bilibili.com/video/av45108908

[2]http://www.cplusplus.com/reference/vector/vector/

[3]https://en.cppreference.com/w/cpp/container/vector

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值