Standard Template Library (STL) - std::vector::cbegin

Standard Template Library (STL) - std::vector::cbegin

public member function - 公开成员函数

1. std::vector::cbegin

const_iterator cbegin() const noexcept;

Return const_iterator to beginning - 返回指向容器第一个元素的迭代器

在这里插入图片描述

返回指向容器首元素的迭代器。
若容器为空,则返回的迭代器将等于 end()

const_iterator 是一个指向 const 内容的迭代器。你可以对这个迭代器进行加减操作,和一般的迭代器一样。但是你不能通过这个迭代器来修改其所指向的内容。

begin 返回的是一个一般迭代器,可以通过该迭代器进行读写操作。cbegin 返回的迭代器不能进行写操作,只可以读。

Returns a const_iterator pointing to the first element in the container.
返回指向容器中第一个元素的 const_iterator

A const_iterator is an iterator that points to const content. This iterator can be increased and decreased (unless it is itself also const), just like the iterator returned by vector::begin, but it cannot be used to modify the contents it points to, even if the vector object is not itself const.
const_iterator 是指向 const 内容的迭代器。可以增加和减少此迭代器 (除非它本身也是 const),就像 vector::begin 返回的迭代器一样,但是即使向量对象本身不是 const,也不能用来修改其指向的内容。

If the container is empty, the returned iterator value shall not be dereferenced.
如果容器为空,则返回的迭代器值不得解引用。

dereference [ˌdiːˈrefrəns]:v.	 间接引用,间接访问,解引用

2. Parameters

none - 无

3. Return value

A const_iterator to the beginning of the sequence.
序列开头的 const_iterator

指向首元素的迭代器。

Member type const_iterator is a random access iterator type that points to a const element.
成员类型 const_iterator 是指向 const 元素的随机访问迭代器类型。

4. Examples

4.1 std::vector::cbegin

//============================================================================
// Name        : std::vector::cbegin/cend
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>

int main()
{
	std::vector<int> vector_data = {10, 20, 30, 40, 50};

	std::cout << "vector_data contains:";

	for (auto it = vector_data.cbegin(); it != vector_data.cend(); ++it)
	{
		std::cout << ' ' << *it;
	}

	std::cout << '\n';

	return 0;
}

vector_data contains: 10 20 30 40 50

4.2 std::vector::cbegin

//============================================================================
// Name        : std::vector::cbegin/cend
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#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;

	// Sums all integers in the vector ints (if any), printing only the result.
	int sum = 0;
	for (auto it = ints.cbegin(); it != ints.cend(); it++)
	{
		sum += *it;
	}
	std::cout << "Sum of ints: " << sum << "\n";

	// Prints the first fruit in the vector fruits, without checking if there is one.
	std::cout << "First fruit: " << *(fruits.begin()) << "\n";
	std::cout << "First fruit: " << *fruits.begin() << "\n";

	if (empty.begin() == empty.end())
	{
		std::cout << "vector 'empty' is indeed empty.\n";
	}

	return 0;
}

Sum of ints: 31
First fruit: orange
First fruit: orange
vector 'empty' is indeed empty.

5. Complexity - 复杂度

Constant. - 常数。

destruction [dɪˈstrʌkʃn]:n. 破坏,毁灭,摧毁

6. Iterator validity - 迭代器有效性

No changes.

7. Data races - 数据竞争

The container is accessed.
容器被访问。

No contained elements are accessed by the call, but the iterator returned can be used to access them. Concurrently accessing or modifying different elements is safe.
调用不会访问任何包含的元素,但是返回的迭代器可用于访问它们。同时访问或修改不同的元素是安全的。

8. Exception safety - 异常安全性

No-throw guarantee: this member function never throws exceptions.
无抛出保证:此成员函数从不抛出异常。

The copy construction or assignment of the returned iterator is also guaranteed to never throw.
返回的迭代器的拷贝构造或赋值也保证永远不会抛出。

assignment [ə'saɪnmənt]:n. 任务,布置,赋值

References

http://www.cplusplus.com/reference/vector/vector/cbegin/

发布了443 篇原创文章 · 获赞 1685 · 访问量 101万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104250423