标准库头文件

标准库头文件<vector>

C++

编译器支持

独立和宿主

语言

标准库

标准库头文件

具名要求

特性测试宏 (C++20)

语言支持库

概念库 (C++20)

诊断库

内存管理库

元编程库 (C++11)

通用工具库

容器库

迭代器库

范围库 (C++20)

算法库

字符串库

文本处理库

数值库

日期和时间库

输入/输出库

文件系统库 (C++17)

并发支持库 (C++11)

执行控制库 (C++26)

技术规范

符号索引

外部库

[编辑] 标准库头文件

语言支持

(C++20)

(C++26)

(C++20)

(C++11)

(C++11)

(C++20)

(C++23)

(C++20)

概念

(C++20)

诊断

(C++26)

(C++23)

(C++11)

内存管理

(C++17)

(C++11)

元编程

(C++11)

(C++11)

通用工具

(C++17)

(C++20)

(C++17)

(C++23)

(C++20)

(C++17)

(C++11)

(C++11)

(C++17)

容器

(C++11)

(C++23)

(C++23)

(C++11)

(C++26)

(C++26)

(C++23)

(C++20)

(C++11)

(C++11)

迭代器

范围

(C++23)

(C++20)

算法

字符串

(C++11)

(C++17)

文本处理

(C++11/17/26* 已弃用,将在 C++26 中移除)

(C++11)

(C++26)

数值

(C++11)

(C++26)

(C++20)

(C++11)

(C++26)

时间

(C++11)

C 兼容性

(C++11/17/20* 已弃用,将在 C++20 中移除)

(C++20 前)

(C++11/17/20* 已弃用,将在 C++20 中移除)

(C++11/17/20* 已弃用,将在 C++20 中移除)

(C++11/17/20* 已弃用,将在 C++20 中移除)

输入/输出

(C++11)

(C++17)

(C++23)

(C++23)

(C++98/26* 已弃用,将在 C++26 中移除)

(C++20)

并发支持

(C++11)

(C++20)

(C++11)

(C++11)

(C++26)

(C++20)

(C++11)

(C++26)

(C++20)

(C++14)

(C++23)

(C++20)

(C++11)

执行支持

(C++17)

[编辑]

此头文件是容器库的一部分。

目录

1 包含

2 类

2.1 前置声明

3 函数

3.1 范围访问

4 概要

4.1 类模板 std::vector

4.2 类模板 std::vector 对 bool 的特化

4.3 类模板 std::formatter 对 std::vector::reference 的特化

5 缺陷报告

包含

(C++20)

三路比较运算符 支持[编辑]

(C++11)

std::initializer_list 类模板[编辑]

vector

可调整大小的连续数组 (类模板) [编辑]

vector

节省空间的动态位集 (类模板特化) [编辑]

std::hash>(C++11)

对 std::vector 的哈希支持 (类模板特化)

前置声明

定义于头文件

hash(C++11)

哈希函数对象 (类模板) [编辑]

函数

operator==operator!=operatoroperator>=operator<=>(C++20 前移除)(C++20 前移除)(C++20 前移除)(C++20 前移除)(C++20 前移除)(C++20 起)

按字典序比较两个 vector 的值 (函数模板) [编辑]

std::swap(std::vector)

特化 std::swap 算法 (函数模板) [编辑]

erase(std::vector)erase_if(std::vector)(C++20)

移除所有满足特定标准的元素 (函数模板) [编辑]

范围访问

begincbegin(C++11 起)(C++14 起)

返回指向容器或数组开头的迭代器 (函数模板) [编辑]

endcend(C++11 起)(C++14 起)

返回指向容器或数组末尾的迭代器 (函数模板) [编辑]

rbegincrbegin(C++14)

返回指向容器或数组开头的反向迭代器 (函数模板) [编辑]

rendcrend(C++14)

返回容器或数组的反向末尾迭代器 (函数模板) [编辑]

sizessize(C++17 起)(C++20 起)

返回容器或数组的大小 (函数模板) [编辑]

empty(C++17)

检查容器是否为空 (函数模板) [编辑]

data(C++17)

获取指向底层数组的指针 (函数模板) [编辑]

[编辑] 概要

#include

#include

namespace std {

// class template vector

template>

class vector;

template

constexpr bool operator==(const vector& x, const vector& y);

template

constexpr /*synth-three-way-result*/ operator<=>(const vector& x,

const vector& y);

template

constexpr void swap(vector& x,

vector& y) noexcept(noexcept(x.swap(y)));

// erasure

template

constexpr typename vector::size_type erase(vector& c,

const U& value);

template

constexpr typename vector::size_type erase_if(vector& c,

Predicate pred);

namespace pmr {

template

using vector = std::vector>;

}

// specialization of vector for bool

// partial class template specialization vector

template

class vector;

template

constexpr bool /*is-vector-bool-reference*/ = /* see description */; // exposition only

// hash support

template

struct hash;

template

struct hash>;

// formatter specialization for vector

template

requires /*is-vector-bool-reference*/

struct formatter;

}

[编辑] 类模板 std::vector

namespace std {

template>

class vector

{

public:

// types

using value_type = T;

using allocator_type = Allocator;

using pointer = typename allocator_traits::pointer;

using const_pointer = typename allocator_traits::const_pointer;

using reference = value_type&;

using const_reference = const value_type&;

using size_type = /* implementation-defined */;

using difference_type = /* implementation-defined */;

using iterator = /* implementation-defined */;

using const_iterator = /* implementation-defined */;

using reverse_iterator = std::reverse_iterator;

using const_reverse_iterator = std::reverse_iterator;

// construct/copy/destroy

constexpr vector() noexcept(noexcept(Allocator()))

: vector(Allocator())

{

}

constexpr explicit vector(const Allocator&) noexcept;

constexpr explicit vector(size_type n, const Allocator& = Allocator());

constexpr vector(size_type n, const T& value, const Allocator& = Allocator());

template

constexpr vector(InputIter first, InputIter last, const Allocator& = Allocator());

template R>

constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator());

constexpr vector(const vector& x);

constexpr vector(vector&&) noexcept;

constexpr vector(const vector&, const type_identity_t&);

constexpr vector(vector&&, const type_identity_t&);

constexpr vector(initializer_list, const Allocator& = Allocator());

constexpr ~vector();

constexpr vector& operator=(const vector& x);

constexpr vector& operator=(vector&& x) noexcept(

allocator_traits::propagate_on_container_move_assignment::value ||

allocator_traits::is_always_equal::value);

constexpr vector& operator=(initializer_list);

template

constexpr void assign(InputIter first, InputIter last);

template R>

constexpr void assign_range(R&& rg);

constexpr void assign(size_type n, const T& u);

constexpr void assign(initializer_list);

constexpr allocator_type get_allocator() const noexcept;

// iterators

constexpr iterator begin() noexcept;

constexpr const_iterator begin() const noexcept;

constexpr iterator end() noexcept;

constexpr const_iterator end() const noexcept;

constexpr reverse_iterator rbegin() noexcept;

constexpr const_reverse_iterator rbegin() const noexcept;

constexpr reverse_iterator rend() noexcept;

constexpr const_reverse_iterator rend() const noexcept;

constexpr const_iterator cbegin() const noexcept;

constexpr const_iterator cend() const noexcept;

constexpr const_reverse_iterator crbegin() const noexcept;

constexpr const_reverse_iterator crend() const noexcept;

// capacity

constexpr bool empty() const noexcept;

constexpr size_type size() const noexcept;

constexpr size_type max_size() const noexcept;

constexpr size_type capacity() const noexcept;

constexpr void resize(size_type sz);

constexpr void resize(size_type sz, const T& c);

constexpr void reserve(size_type n);

constexpr void shrink_to_fit();

// element access

constexpr reference operator[](size_type n);

constexpr const_reference operator[](size_type n) const;

constexpr const_reference at(size_type n) const;

constexpr reference at(size_type n);

constexpr reference front();

constexpr const_reference front() const;

constexpr reference back();

constexpr const_reference back() const;

// data access

constexpr T* data() noexcept;

constexpr const T* data() const noexcept;

// modifiers

template

constexpr reference emplace_back(Args&&... args);

constexpr void push_back(const T& x);

constexpr void push_back(T&& x);

template R>

constexpr void append_range(R&& rg);

constexpr void pop_back();

template

constexpr iterator emplace(const_iterator position, Args&&... args);

constexpr iterator insert(const_iterator position, const T& x);

constexpr iterator insert(const_iterator position, T&& x);

constexpr iterator insert(const_iterator position, size_type n, const T& x);

template

constexpr iterator insert(const_iterator position, InputIter first, InputIter last);

template R>

constexpr iterator insert_range(const_iterator position, R&& rg);

constexpr iterator insert(const_iterator position, initializer_list il);

constexpr iterator erase(const_iterator position);

constexpr iterator erase(const_iterator first, const_iterator last);

constexpr void swap(vector&) noexcept(

allocator_traits::propagate_on_container_swap::value ||

allocator_traits::is_always_equal::value);

constexpr void clear() noexcept;

};

template>>

vector(InputIter, InputIter, Allocator = Allocator())

-> vector, Allocator>;

template>>

vector(from_range_t, R&&, Allocator = Allocator())

-> vector, Allocator>;

}

[编辑] 类模板 std::vector 对 bool 的特化

namespace std {

template

class vector

{

public:

// types

using value_type = bool;

using allocator_type = Allocator;

using pointer = /* implementation-defined */;

using const_pointer = /* implementation-defined */;

using const_reference = bool;

using size_type = /* implementation-defined */;

using difference_type = /* implementation-defined */;

using iterator = /* implementation-defined */;

using const_iterator = /* implementation-defined */;

using reverse_iterator = std::reverse_iterator;

using const_reverse_iterator = std::reverse_iterator;

// bit reference

class reference

{

public:

constexpr reference(const reference&) = default;

constexpr ~reference();

constexpr operator bool() const noexcept;

constexpr reference& operator=(bool x) noexcept;

constexpr reference& operator=(const reference& x) noexcept;

constexpr const reference& operator=(bool x) const noexcept;

constexpr void flip() noexcept; // flips the bit

};

// construct/copy/destroy

constexpr vector() noexcept(noexcept(Allocator()))

: vector(Allocator())

{

}

constexpr explicit vector(const Allocator&) noexcept;

constexpr explicit vector(size_type n, const Allocator& = Allocator());

constexpr vector(size_type n, const bool& value, const Allocator& = Allocator());

template

constexpr vector(InputIter first, InputIter last, const Allocator& = Allocator());

template R>

constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator());

constexpr vector(const vector& x);

constexpr vector(vector&& x) noexcept;

constexpr vector(const vector&, const type_identity_t&);

constexpr vector(vector&&, const type_identity_t&);

constexpr vector(initializer_list, const Allocator& = Allocator());

constexpr ~vector();

constexpr vector& operator=(const vector& x);

constexpr vector& operator=(vector&& x) noexcept(

allocator_traits::propagate_on_container_move_assignment::value ||

allocator_traits::is_always_equal::value);

constexpr vector& operator=(initializer_list);

template

constexpr void assign(InputIter first, InputIter last);

template R>

constexpr void assign_range(R&& rg);

constexpr void assign(size_type n, const bool& t);

constexpr void assign(initializer_list);

constexpr allocator_type get_allocator() const noexcept;

// iterators

constexpr iterator begin() noexcept;

constexpr const_iterator begin() const noexcept;

constexpr iterator end() noexcept;

constexpr const_iterator end() const noexcept;

constexpr reverse_iterator rbegin() noexcept;

constexpr const_reverse_iterator rbegin() const noexcept;

constexpr reverse_iterator rend() noexcept;

constexpr const_reverse_iterator rend() const noexcept;

constexpr const_iterator cbegin() const noexcept;

constexpr const_iterator cend() const noexcept;

constexpr const_reverse_iterator crbegin() const noexcept;

constexpr const_reverse_iterator crend() const noexcept;

// capacity

constexpr bool empty() const noexcept;

constexpr size_type size() const noexcept;

constexpr size_type max_size() const noexcept;

constexpr size_type capacity() const noexcept;

constexpr void resize(size_type sz, bool c = false);

constexpr void reserve(size_type n);

constexpr void shrink_to_fit();

// element access

constexpr reference operator[](size_type n);

constexpr const_reference operator[](size_type n) const;

constexpr const_reference at(size_type n) const;

constexpr reference at(size_type n);

constexpr reference front();

constexpr const_reference front() const;

constexpr reference back();

constexpr const_reference back() const;

// modifiers

template

constexpr reference emplace_back(Args&&... args);

constexpr void push_back(const bool& x);

template R>

constexpr void append_range(R&& rg);

constexpr void pop_back();

template

constexpr iterator emplace(const_iterator position, Args&&... args);

constexpr iterator insert(const_iterator position, const bool& x);

constexpr iterator insert(const_iterator position, size_type n, const bool& x);

template

constexpr iterator insert(const_iterator position, InputIter first, InputIter last);

template R>

constexpr iterator insert_range(const_iterator position, R&& rg);

constexpr iterator insert(const_iterator position, initializer_list il);

constexpr iterator erase(const_iterator position);

constexpr iterator erase(const_iterator first, const_iterator last);

constexpr void swap(vector&) noexcept(

allocator_traits::propagate_on_container_swap::value ||

allocator_traits::is_always_equal::value);

static constexpr void swap(reference x, reference y) noexcept;

constexpr void flip() noexcept; // flips all bits

constexpr void clear() noexcept;

};

}

[编辑] 类模板 std::formatter 对 std::vector::reference 的特化

namespace std {

template

requires /*is-vector-bool-reference*/

struct formatter

{

private:

formatter /*underlying_*/; // exposition only

public:

template

constexpr typename ParseContext::iterator parse(ParseContext& ctx);

template

typename FormatContext::iterator format(const T& ref, FormatContext& ctx) const;

};

}

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR

应用于

已发布行为

正确行为

LWG 4140

C++98

概要包含默认构造函数的声明对于 std::vector::reference

已移除声明

相关推荐

玩遍10家直播答题App,我总结了35个运营方法
国内在365投注

玩遍10家直播答题App,我总结了35个运营方法

📅 06-28 👁️ 1184
邮储银行悦享分期卡怎么使用?邮储悦享分期卡申请条件资料
三唑仑片
365bet-亚洲版官网

三唑仑片

📅 07-24 👁️ 3150