Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::priority_queue<T,Container,Compare>::swap

      From cppreference.com
      <cpp‎ |container‎ |priority queue

      [edit template]
       
       
       
       
      void swap( priority_queue& other)noexcept(/* see below */);
      (since C++11)
      Exchanges the contents of the container adaptor with those ofother. Effectively calls
      usingstd::swap;swap(c, other.c);swap(comp, other.comp);

      Contents

      [edit]Parameters

      other - container adaptor to exchange the contents with

      [edit]Return value

      (none)

      [edit]Exceptions

      noexcept specification:  
      noexcept(noexcept(swap(c, other.c))&&noexcept(swap(comp, other.comp)))

      In the expression above, the identifierswap is looked up in the same manner as the one used by the C++17std::is_nothrow_swappable trait.

      (since C++11)
      (until C++17)
      noexcept specification:  
      noexcept(std::is_nothrow_swappable_v<Container>&&
               std::is_nothrow_swappable_v<Compare>)
      (since C++17)

      [edit]Complexity

      Same as underlying container (typically constant).

      Notes

      Some implementations (e.g. libc++) provide theswap member function as an extension to pre-C++11 modes.

      [edit]Example

      Run this code
      #include <iostream>#include <concepts>#include <functional>#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(autoconst& elem: this->c)std::cout<< elem<<' ';std::cout<<'\n';}}; static_cast<Printerconst&>(adaptor).print(name);} int main(){std::vector<std::string> v1{"1","2","3","4"},                             v2{"Ɐ","B","Ɔ","D","Ǝ"}; std::priority_queue s1(std::less<>(), std::move(v1));std::priority_queue s2(std::less<>(), std::move(v2));     print("s1", s1);    print("s2", s2);     s1.swap(s2);     print("s1", s1);    print("s2", s2);}

      Output:

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

      Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2456C++11thenoexcept specification is ill-formedmade to work

      [edit]See also

      specializes thestd::swap algorithm
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/priority_queue/swap&oldid=133089"

      [8]ページ先頭

      ©2009-2025 Movatter.jp