名前空間
変種
操作

std::swap(std::queue)

From cppreference.com
< cpp‎ | container‎ | queue
 
 
 
 
ヘッダー<queue>で定義されています
template< class T, class Container >

void swap( std::queue<T, Container>& lhs,

           std::queue<T, Container>& rhs );
(C++11以降)
(C++17まで)
template< class T, class Container >

void swap( std::queue<T, Container>& lhs,
           std::queue<T, Container>& rhs )

               noexcept(/* 以下を参照 */);
(C++17以降)
std::swap アルゴリズムの std::queue に対する特殊化。 lhsrhs の内容を交換します。 lhs.swap(rhs) を呼び出します。

このオーバーロードは、 std::is_swappable_v<Container>true の場合にのみ、オーバーロード解決に参加します。

(C++17以降)

目次

[編集] パラメータ

lhs, rhs - 交換するコンテナ

[編集] 戻り値

(なし)

[編集] 計算量

基になるコンテナを交換するのと同じです。

例外

noexcept 指定:  
noexcept(noexcept(lhs.swap(rhs)))
(C++17以降)

注釈

コンテナアダプタの std::swap 用オーバーロードは C++11 で導入されましたが、 C++98 でもコンテナアダプタは std::swap によって交換可能でした。そのような std::swap への呼び出しは通常、線形時間計算量ですが、より良い計算量が提供される場合もあります。

[編集]

#include <algorithm>
#include <iostream>
#include <queue>
 
int main()
{
    std::queue<int> alice;
    std::queue<int> bob;
 
    auto print = [](const auto& title, const auto& cont)
    {
        std::cout << title << " size=" << cont.size();
        std::cout << " front=" << cont.front();
        std::cout << " back=" << cont.back() << '\n';
    };
 
    for (int i = 1; i < 4; ++i)
        alice.push(i);
    for (int i = 7; i < 11; ++i)
        bob.push(i);
 
    // Print state before swap
    print("Alice:", alice);
    print("Bobby:", bob);
 
    std::cout << "-- SWAP\n";
    std::swap(alice, bob);
 
    // Print state after swap
    print("Alice:", alice);
    print("Bobby:", bob);
}

出力

Alice: size=3 front=1 back=3
Bobby: size=4 front=7 back=10
-- SWAP
Alice: size=4 front=7 back=10
Bobby: size=3 front=1 back=3

[編集] 関連項目

(C++11)
内容を交換する
(public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)