Namespaces
Variants

std::queue<T,Container>:: swap

From cppreference.net

void swap ( queue & other ) noexcept ( /* see below */ ) ;
(C++11以降)
Exchanges the contents of the container adaptor with those of other . Effectively calls
using std::swap;
swap(c, other.c);

目次

パラメータ

other - コンテンツを交換するためのコンテナアダプタ

戻り値

(なし)

例外

noexcept 指定:
noexcept ( noexcept ( swap ( c, other. c ) ) )

上記の式において、識別子 swap はC++17の std::is_nothrow_swappable 特性で使用されるものと同じ方法で検索されます。

(C++11以降)
(C++17まで)
noexcept 指定:
noexcept ( std:: is_nothrow_swappable_v < Container > )
(C++17以降)

計算量

基となるコンテナと同じ(通常は定数)。

注記

一部の実装(例: libc++)では、C++11以前のモードでも拡張機能として swap メンバー関数を提供しています。

#include <iostream>
#include <concepts>
#include <queue>
#include <string>
#include <string_view>
#include <vector>
template<typename Adaptor>
requires (std::ranges::input_range<typename Adaptor::container_type>)
void print(std::string_view name, const Adaptor& adaptor)
{
    struct Printer : Adaptor // to use protected Adaptor::Container c;
    {
        void print(std::string_view name) const
        {
            std::cout << name << " [" << std::size(this->c) << "]: ";
            for (auto const& elem : this->c)
                std::cout << elem << ' ';
            std::cout << '\n';
        }
    };
    static_cast<Printer const&>(adaptor).print(name);
}
int main()
{
    std::vector<std::string> v1{"1","2","3","4"},
                             v2{"Ɐ","B","Ɔ","D","Ǝ"};
    std::queue s1(std::move(v1));
    std::queue s2(std::move(v2));
    print("s1", s1);
    print("s2", s2);
    s1.swap(s2);
    print("s1", s1);
    print("s2", s2);
}

出力:

s1 [4]: 4 3 2 1
s2 [5]: Ǝ D Ɔ B Ɐ
s1 [5]: Ǝ D Ɔ B Ɐ
s2 [4]: 4 3 2 1

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2456 C++11 the noexcept specification is ill-formed 動作するように修正

関連項目

std::swap アルゴリズムを特殊化する
(関数テンプレート)