C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- 容器库
- std::array
- std::vector
- std::vector<T,Allocator>::push_back
- std::vector<T,Allocator>::assign
- std::vector<T,Allocator>::get_allocator
- std::vector<T,Allocator>::operator[]
- std::vector<T,Allocator>::front
- std::vector<T,Allocator>::at
- std::vector<T,Allocator>::pop_back
- std::vector<T,Allocator>::end, std::vector<T,Allocator>::cend
- std::vector<T,Allocator>::vector
- std::vector<T,Allocator>::~vector
- std::vector<T,Allocator>::operator=
- std::vector<T,Allocator>::back
- std::vector<T,Allocator>::data
- std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin
- std::vector<T,Allocator>::rbegin, std::vector<T,Allocator>::crbegin
- std::vector<T,Allocator>::rend, std::vector<T,Allocator>::crend
- std::vector<T,Allocator>::empty
- std::vector<T,Allocator>::size
- std::vector<T,Allocator>::max_size
- std::vector<T,Allocator>::reserve
- std::vector<T,Allocator>::capacity
- std::vector<T,Allocator>::shrink_to_fit
- std::vector<T,Allocator>::clear
- std::vector<T,Allocator>::insert
- std::vector<T,Allocator>::emplace
- std::vector<T,Allocator>::erase
- std::vector<T,Allocator>::emplace_back
- std::vector<T,Allocator>::resize
- std::vector<T,Allocator>::swap
- std::swap(std::vector)
- std::erase, std::erase_if (std::vector)
- operator==,!=,<,<=,>,>=,<=>(std::vector)
- std::vector 的推导指引
- std::map
- std::unordered_map
- std::priority_queue
- std::span
- std::forward_list
- std::deque
- std::list
- std::set
- std::multiset
- std::multimap
- std::unordered_set
- std::unordered_multiset
- std::unordered_multimap
- std::stack
- std::queue
- std::vector<bool>
- 结点把柄 (C++17)
- 注释
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >容器库 >std::vector > std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin
std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin
iterator begin(); |
(C++11 前) | |
iterator begin() noexcept; |
(C++11 起) | |
const_iterator begin() const; |
(C++11 前) | |
const_iterator begin() const noexcept; |
(C++11 起) | |
const_iterator cbegin() const noexcept; |
(C++11 起) | |
返回指向 vector
首元素的迭代器。
若 vector
为空,则返回的迭代器将等于 end() 。
参数
(无)
返回值
指向首元素的迭代器。
复杂度
常数。
示例
运行此代码
#include <iostream> #include <vector> #include <string> int main() { std::vector<int> ints {1, 2, 4, 8, 16}; std::vector<std::string> fruits {"orange", "apple", "raspberry"}; std::vector<char> empty; // 求和 vector ints 中的所有整数(若存在),仅打印结果。 int sum = 0; for (auto it = ints.cbegin(); it != ints.cend(); it++) sum += *it; std::cout << "Sum of ints: " << sum << "\n"; // 打印 vector fruits 中的首个水果,而不检查是否有一个。 std::cout << "First fruit: " << *fruits.begin() << "\n"; if (empty.begin() == empty.end()) std::cout << "vector 'empty' is indeed empty.\n"; }
输出:
Sum of ints: 31 First fruit: orange vector 'empty' is indeed empty.
参阅
返回指向容器尾端的迭代器 (公开成员函数) |