Standard Template Library (STL) - std::queue::front

Standard Template Library (STL) - std::queue::front

public member function - 公开成员函数

1. std::queue::front

C++98
value_type& front();
const value_type& front() const;

C++11
reference& front();
const_reference& front() const;

access the first element - 访问第一个元素。

返回到 queue 中首元素的引用。此元素将是调用 pop() 时第一个移除的元素。

Access next element - 访问首元素

Returns a reference to the next element in the queue.
返回对队列中首元素的引用。

The next element is the “oldest” element in the queue and the same element that is popped out from the queue when queue::pop is called.
The next element 是队列中插入最早的元素,是调用 queue::pop 时从队列中弹出的元素。

This member function effectively calls member front of the underlying container object.
该成员函数有效地调用底层容器对象的成员前端。

2. Parameters

none

3. Return value

A reference to the next element in the queue.
queue 中首元素的引用。

C++98
Member type value_type is the type of the elements in the container (defined as an alias of the first class template parameter, T).
成员类型 value_type 是容器中元素的类型 (定义为第一类模板参数 T 的别名)。

C++11
Member types reference and const_reference are aliases of the underlying container’s types with the same name.
成员类型 reference and const_reference 是具有相同名称的底层容器类型的别名。

4. Examples

4.1 std::queue::front

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

#include <iostream>       // std::cout
#include <queue>          // std::queue

int main()
{
	std::queue<int> queue_data;

	queue_data.push(77);
	queue_data.push(16);

	queue_data.front() -= queue_data.back();    // 77-16=61

	std::cout << "queue_data.front() is now " << queue_data.front() << '\n';

	return 0;
}

queue_data.front() is now 61

5. Complexity - 复杂度

Constant (calling front on the underlying container).
常量 (在底层容器上调用 front)。

6. Data races - 数据竞争

The container is accessed (neither the const nor the non-const versions modify the container).
容器被访问 (const 和非 const 版本都不能修改容器)。

The reference returned can be used to access or modify the next element.
返回的引用可用于访问或修改首个元素。

7. Exception safety - 异常安全性

Provides the same level of guarantees as the operation performed on the container (no-throw guarantee for standard non-empty containers).
提供与在容器上执行的操作相同级别的保证 (对于标准非空容器,无抛出保证)。

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

References

http://www.cplusplus.com/reference/queue/queue/front/
https://en.cppreference.com/w/cpp/container/queue/front

发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

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