首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::vector

Defined in header <vector>

?

?

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

(1)

?

namespace pmr { template <class T> using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>; }

(2)

(since C++17)

1%29std::vector封装动态大小数组的序列容器。

2%29std::pmr::vector是使用多态分配器

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.

(since C++03)

矢量的存储是自动处理的,根据需要进行扩展和压缩。向量通常比静态数组占用更多的空间,因为分配了更多的内存来处理未来的增长。这样,向量就不需要每次插入元素时重新分配,而是只在额外内存耗尽时才重新分配。分配的内存总量可以使用capacity()功能。可以通过调用shrink_to_fit().%28自C++11%29。

重新分配通常在性能方面是昂贵的操作。reserve()如果预先知道元素的数量,则可以使用函数消除重新分配。

向量上常见操作的复杂度%28效率%29如下:

  • 随机存取-常数O%281%29
  • 在终末摊销常数O%281%29处插入或除去元素
  • 元素的插入或移除.距矢量O%28N%29的直线距离

std::vector满足…的要求Container,,,AllocatorAwareContainer,,,SequenceContainer,,,ContiguousContainer28%Tbool%29%28自C++17%29和ReversibleContainer...

模板参数

T

-

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)

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)

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)

Allocator

-

An 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.

专门性

标准库提供了std::vector类型bool,对空间效率进行了优化。

vector<bool>

space-efficient dynamic bitset (class template specialization)

迭代器失效

这一节还有几处不准确之处。有关详细信息,请参阅单个成员函数页。

Operations

Invalidated

All read only operations, swap, std::swap

Never

clear, operator=, assign

Always

reserve, shrink_to_fit

If the vector changed capacity, all of them. If not, none.

erase

Erased elements + all elements after them (including end())

push_back, emplace_back

If the vector changed capacity, all of them. If not, only end().

insert, emplace, resize

If the vector changed capacity, all of them. If not, only those after the insertion point.

pop_back

The element erased and end().

成员类型

Member type

Definition

value_type

T

allocator_type

Allocator

size_type

Unsigned integer type (usually std::size_t)

difference_type

Signed integer type (usually std::ptrdiff_t)

reference

Allocator::reference (until C++11) value_type& (since C++11)

Allocator::reference

(until C++11)

value_type&

(since C++11)

Allocator::reference

(until C++11)

value_type&

(since C++11)

const_reference

Allocator::const_reference (until C++11) const value_type& (since C++11)

Allocator::const_reference

(until C++11)

const value_type&

(since C++11)

Allocator::const_reference

(until C++11)

const value_type&

(since C++11)

pointer

Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11)

Allocator::pointer

(until C++11)

std::allocator_traits<Allocator>::pointer

(since C++11)

Allocator::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)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

iterator

RandomAccessIterator

const_iterator

Constant random access iterator

reverse_iterator

std::reverse_iterator<iterator>

const_reverse_iterator

std::reverse_iterator<const_iterator>

成员函数

(constructor)

constructs the vector (public member function)

(destructor)

destructs the vector (public member function)

operator=

assigns values to the container (public member function)

assign

assigns values to the container (public member function)

get_allocator

returns the associated allocator (public member function)

元素存取

在访问指定元素时,使用边界检查%28公共成员函数%29

操作者。[]访问指定元素%28公共成员函数%29

前端访问第一个元素%28公共成员函数%29

返回访问最后一个元素%28公共成员函数%29

数据%28C++11%29直接访问底层数组%28公共成员函数%29

迭代器

BEGINCBEGIN将迭代器返回到开头%28的公共成员函数%29

End cend将迭代器返回到End%28公共成员函数%29

将反向迭代器返回到开头%28的公共成员函数%29

rend crend将反向迭代器返回到End%28公共成员函数%29

容量

空检查容器是否为空%28公共成员函数%29

Size返回元素数%28公共成员函数%29

马克斯[医]Size返回元素的最大可能数%28公共成员函数%29

储备储备储备%28公众会员功能%29

容量返回当前分配的存储%28公共成员函数%29中可以保存的元素数。

收缩[医]到[医]FIT%28C++11%29通过释放未使用内存%28公共成员函数%29来减少内存使用

修饰符

清除内容%28公共成员功能%29

插入元素%28公共成员函数%29

嵌入%28C++11%29构造元素就地%28公共成员函数%29

擦除元素%28公共成员函数%29

推[医]向末尾%28公共成员函数%29添加一个元素

座落[医]Back%28C++11%29在%28公共成员函数%29的末尾构造一个就地元素。

波普[医]回移除最后一个元素%28公共成员函数%29

调整大小更改存储的元素数%28公共成员函数%29

交换交换内容%28公共成员函数%29

非会员职能

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the vector (function template)

std::swap(std::vector)

specializes the std::swap algorithm (function template)

二次

代码语言:javascript
复制
#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';
    }
}

二次

产出:

二次

代码语言:javascript
复制
7
5
16
8
25
13

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com