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

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

public member function - 公开成员函数

mutex:n. 互斥,互斥元,互斥体,互斥量
synchronization [ˌsɪŋkrənaɪˈzeɪʃn]:n. 同步,同时性
primitive [ˈprɪmətɪv]:adj. 原始的,远古的,简单的,粗糙的 n. 原始人
simultaneously [ˌsɪmlˈteɪniəsli]:adv. 同时地
exclusive [ɪkˈskluːsɪv]:adj. 独有的,排外的,专一的 n. 独家新闻,独家经营的项目,排外者
semantics [sɪˈmæntɪks]:n. 语义学,语义论
recursive [rɪˈkɜːsɪv]:adj. 递归的,循环的

1. std::queue::swap

void swap (queue& x) noexcept(/*see below*/);

Swap contents - 交换内容

Exchanges the contents of the container adaptor (*this) by those of x.
x 交换容器适配器 (*this) 的内容。

This member function calls the non-member function swap (unqualified) to swap the underlying containers.
该成员函数调用非成员函数 swap (unqualified) 来交换底层容器。

The noexcept specifier matches the swap operation on the underlying container.
noexcept 说明符匹配底层容器上的 swap 操作。

2. Parameters

x
Another queue container adaptor of the same type (i.e., instantiated with the same template parameters, T and Container). Sizes may differ.
另一个相同类型的 queue 容器适配器 (即,使用相同的模板参数 T and Container 实例化)。大小可能有所不同。

3. Return value

none

4. Example - 示例

4.1 std::queue::swap

//============================================================================
// Name        : std::queue::swap
// 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> foo, bar;

	foo.push(10);
	foo.push(20);
	foo.push(30);

	bar.push(111);
	bar.push(222);

	std::cout << "size of foo: " << foo.size() << '\n';
	std::cout << "size of bar: " << bar.size() << '\n';

	foo.swap(bar);

	std::cout << "size of foo: " << foo.size() << '\n';
	std::cout << "size of bar: " << bar.size() << '\n';

	return 0;
}

size of foo: 3
size of bar: 2
size of foo: 2
size of bar: 3

5. Complexity

Constant. 常数。

6. Data races - 数据竞争

Both *this and x are modified.

7. Exception safety - 异常安全性

Provides the same level of guarantees as the operation performed on the underlying container objects.
提供与对底层容器对象执行的操作相同级别的保证。

Reference

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

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

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104593546
今日推荐