std::vector<T,Allocator>::end, std::vector<T,Allocator>::cend
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    
|   iterator end();  | 
(C++11 前) | |
|   iterator end() noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr iterator end() noexcept;  | 
(C++20 起) | |
|   const_iterator end() const;  | 
(C++11 前) | |
|   const_iterator end() const noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr const_iterator end() const noexcept;  | 
(C++20 起) | |
|   const_iterator cend() const noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr const_iterator cend() const noexcept;  | 
(C++20 起) | |
返回指向 vector 末元素后一元素的迭代器。
此元素表现为占位符;试图访问它导致未定义行为。
参数
(无)
返回值
指向后随最后元素的迭代器。
复杂度
常数。
示例
运行此代码
#include <iostream> #include <numeric> #include <vector> #include <string> int main() { std::vector<int> nums {1, 2, 4, 8, 16}; std::vector<std::string> fruits {"orange", "apple", "raspberry"}; std::vector<char> empty; // 求和 vector nums 中的所有整数(若存在),仅打印结果。 std::cout << "Sum of nums: " << std::accumulate(nums.begin(), nums.end(), 0) << "\n"; // 打印 vector fruits 中的首个 fruis ,不检查是否有一个。 std::cout << "First fruit: " << *fruits.begin() << "\n"; if (empty.begin() == empty.end()) std::cout << "vector 'empty' is indeed empty.\n"; }
输出:
Sum of nums: 31 First fruit: orange vector 'empty' is indeed empty.
参阅
|    (C++11)  | 
   返回指向起始的迭代器   (公开成员函数)  |