序列型容器之vector

https://en.cppreference.com/w/cpp/container

  • 头文件

<vector>

  • 声明

1)

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

2)从C++17开始是
namespace pmr {
    template <class T>
    using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;
}
说明:

1) std::vector是一个顺序容器,它封装了动态大小的数组。

2)  std::pmr::vector 是使用“ polymorphic allocator”的一个模板别称。

  • 描述

vector内的所有元素是内存连续存储的,也就是说,这些元素不仅可以通过迭代器访问,也可以普通的指针偏移来访问。这意味着,一个vector内指向一个元素的指针可以作为参数被传递到任何需要这个数组的函数。

从C++03

如果需要扩展或连接,vector的存储空间是自动处理的。vector通常要比静态数组占用更多的内存空间,因为需要分配更多的内存来处理以后可能的增长;这样,在进行insert操作时,不需要每次重分配内存,只有当现有的内存空间没有剩余时才进行重分配。整个已分配的内存的大小可以通过使用capacity()函数来获取。可以通过调用 shrink_to_fit() 来获取额外的内存。

从C++11

因为重分配是非常耗时的操作。 如果我们已知数组内最大元素个数时,reserve()函数可以用于消除重分配。

下面是vector的操作的复杂度描述:

1)随机访问--固定 O(1)

2)在尾部插入或删除元素---amortized constant O(1)

3)插入或删除到尾部固定距离的元素----O(n)

std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer , ContiguousContainer (since C++17) and ReversibleContainer.

  • 模板参数

The type of the elements.
T must meet the requirements of CopyAssignable and CopyConstructible.    (until C++11)
The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.    (since C++11) (until C++17)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.    (since C++17)

AllocatorAn allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T.
  • 特例--bool类型

The standard library provides a specialization of std::vector for the type bool, which may be optimized for space efficiency.

vector<bool>  space-efficient dynamic bitset  (class template specialization)

  • 迭代器操作和限制
操作非法情况
所有的读操作无非法情况
swap, std::swapend()
clear, operator=, assign 所有都非法
reserve, shrink_to_fit If the vector changed capacity, all of them. If not, none.
erase Erased elements and all elements after them (including end())
push_back, emplace_back If the vector changed capacity, all of them. If not, none.
insert, emplace   If the vector changed capacity, all of them. If not, only those at or after the insertion point (including end()).
resizeIf the vector changed capacity, all of them. If not, only end() and any elements erased.
pop_back The element erased and end().
  • 成员类型
成员类型定义
value_type T
allocator_typeAllocator
size_type Unsigned integer type (usually std::size_t)
difference_typeSigned integer type (usually std::ptrdiff_t)
reference  Allocator::reference    (until C++11)    value_type&    (since C++11) 
const_referenceAllocator::const_reference    (until C++11)    const value_type&    (since C++11) 
pointerAllocator::pointer    (until C++11)    std::allocator_traits<Allocator>::pointer    (since C++11) 
const_pointer  Allocator::const_pointer    (until C++11)    std::allocator_traits<Allocator>::const_pointer    (since C++11) 
iteratorLegacyRandomAccessIterator
const_iteratorConstant LegacyRandomAccessIterator
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iterator std::reverse_iterator<const_iterator>
  • 元素访问
ataccess specified element with bounds checking
(public member function)
operator[]access specified element
(public member function)
frontaccess the first element
(public member function)
backaccess the last element
(public member function)
data(C++11)
 direct access to the underlying array
(public member function)
  • 迭代器操作
begin
cbegin
(C++11)returns an iterator to the beginning
(public member function)
rbegin
crbegin
(C++11)rreturns a reverse iterator to the beginning
(public member function)
end
cend
(C++11)returns an iterator to the end
(public member function)
rend
crend
(C++11)returns a reverse iterator to the end
(public member function)
  • 获取能力指标
emptychecks whether the container is empty
(public member function)
sizereturns the number of elements
(public member function)
max_sizereturns the maximum possible number of elements
(public member function)
reservereserves storage
(public member function)
capacityreturns the number of elements that can be held in currently allocated storage
(public member function)
shrink_to_fit(C++11)reduces memory usage by freeing unused memory
(public member function)
  • 修改操作
clearvoid clear();(until C++11)
void clear() noexcept;(since C++11)
clears the contents
(public member function)
insert

1)(until C++11) iterator insert( iterator pos, const T& value );

(since C++11)iterator insert( const_iterator pos, const T& value );
(2)  (since C++11)
iterator insert( const_iterator pos, T&& value );
  (3)    (until C++11)void insert( iterator pos, size_type count, const T& value );
(since C++11)iterator insert( const_iterator pos, size_type count, const T& value );
(4)    (until C++11)template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last);
template< class InputIt >
(since C++11)iterator insert( const_iterator pos, InputIt first, InputIt last );
(5)(since C++11)iterator insert( const_iterator pos, std::initializer_list<T> ilist );  

inserts elements
(public member function)
emplacetemplate< class... Args >
iterator emplace( const_iterator pos, Args&&... args );(since C++11)
(C++11)constructs element in-place
(public member function)
erase(1)    (until C++11)iterator erase( iterator pos );
(since C++11)iterator erase( const_iterator pos );
(2)    (until C++11)iterator erase( iterator first, iterator last );
(since C++11)iterator erase( const_iterator first, const_iterator last );
 
erases elements
(public member function)
push_back(1) void push_back( const T& value );
(2)    (since C++11)void push_back( T&& value );
adds an element to the end
(public member function)
emplace_back

(since C++11)(until C++17)

template< class... Args >
void emplace_back( Args&&... args );
(since C++17)
template< class... Args >
reference emplace_back( Args&&... args );

(C++11)constructs an element in-place at the end
(public member function)
pop_backvoid pop_back();removes the last element
(public member function)
resize

(until C++11)

void resize( size_type count, T value = T() );

 

(since C++11)

1)void resize( size_type count );
(2) void resize( size_type count, const value_type& value );

changes the number of elements stored
(public member function)
swap(until C++17)void swap( vector& other );
(since C++17)(until C++20)void swap( vector& other ) noexcept(/* see below */);
(since C++20)constexpr void swap( vector& other ) noexcept(/* see below */);
swaps the contents
(public member function)

 

  • 举例

#include <iostream>
#include <vector>
 
int main()
{
    // Create a vector containing integers
    std::vector<int> v = {7, 5, 16, 8};
 
    // Add two more integers to vector
    v.push_back(25);
    v.push_back(13);
 
    // Iterate and print values of vector
    for(int n : v) {
        std::cout << n << '\n';
    }
}
Output:

7
5
16
8
25
13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值