CppDS.com

C++ 98 11 14 17 20 手册

std::forward_list<T,Allocator>::begin, std::forward_list<T,Allocator>::cbegin

来自cppreference.com

 
 
 
 
iterator begin() noexcept;
(C++11 起)
const_iterator begin() const noexcept;
(C++11 起)
const_iterator cbegin() const noexcept;
(C++11 起)

返回指向 forward_list 首元素的迭代器。

forward_list 为空,则返回的迭代器将等于 end()

range-begin-end.svg

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。


示例

#include <iostream>
#include <numeric>
#include <forward_list>
#include <string>
 
int main()
{
	std::forward_list<int> nums {1, 2, 4, 8, 16};
	std::forward_list<std::string> fruits {"orange", "apple", "raspberry"};
	std::forward_list<char> empty;
 
	// 求和 forward_list nums 中的所有整数(若存在),仅打印结果。
	std::cout << "Sum of nums: " <<
                std::accumulate(nums.begin(), nums.end(), 0) << "\n";
 
	// 打印 forward_list fruits 中的首个 fruis ,不检查是否有一个。
	std::cout << "First fruit: " << *fruits.begin() << "\n";
 
	if (empty.begin() == empty.end())
		std::cout << "forward_list 'empty' is indeed empty.\n";
}

输出:

Sum of nums: 31
First fruit: orange
forward_list 'empty' is indeed empty.

参阅

返回指向末尾的迭代器
(公开成员函数)
关闭