C++ Unordered_set Library - swap


Advertisements

Description

It exchanges the content of the container by the content of ust, which is another unordered_set object containing elements of the same type. Sizes may differ.

Declaration

Following is the declaration for std::unordered_set::swap.

C++11

void swap ( unordered_set& ust );

Parameters

ust − Another un order set.

Return value

none

Exceptions

Exception is thrown if any element comparison object throws exception.

Please note that invalid arguments cause undefined behavior.

Time complexity

constant time.

Example

The following example shows the usage of std::unordered_set::swap.

#include <iostream>
#include <string>
#include <unordered_set>

int main () {
   std::unordered_set<std::string>
      first = {"sai","ram","krishna"},
      second  = {"tutorials","point",".com"};

   first.swap(second);

   std::cout << "first:";
   for (const std::string& x: first) std::cout << " " << x;
   std::cout << std::endl;

   std::cout << "second:";
   for (const std::string& x: second) std::cout << " " << x;
   std::cout << std::endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

first: .com tutorials point
second: sai krishna ram
unordered_set.htm
Advertisements