Biblioteca C ++ Deque - función swap ()

Descripción

La función C ++ std::deque::swap()intercambia el contenido del primer deque con otro. Esta función cambia el tamaño de deque si es necesario.

Declaración

A continuación se muestra la declaración de la función std :: deque :: swap () del encabezado std :: deque.

C ++ 98

void swap (deque& x);

C ++ 11

void swap (deque& x);

Parámetros

x - Otro objeto deque del mismo tipo.

Valor devuelto

Ninguna.

Excepciones

Esta función miembro nunca lanza una excepción.

Complejidad del tiempo

Constante es decir O (1)

Ejemplo

El siguiente ejemplo muestra el uso de la función std :: deque :: swap ().

#include <iostream>
#include <deque>

using namespace std;

int main(void) {

   deque<int> d1 = {1, 2, 3, 4, 5};
   deque<int> d2 = {50, 60, 70};

   cout << "Content of d1 before swap operation" << endl;
   for (int i = 0; i < d1.size(); ++i)
      cout << d1[i] << endl;

   cout << "Content of d2 before swap operation" << endl;
   for (int i = 0; i < d2.size(); ++i)
      cout << d2[i] << endl;

   cout << endl;

   d1.swap(d2);

   cout << "Content of d1 after swap operation" << endl;
   for (int i = 0; i < d1.size(); ++i)
      cout << d1[i] << endl;

   cout << "Content of d2 after swap operation" << endl;
   for (int i = 0; i < d2.size(); ++i)
      cout << d2[i] << endl;

   return 0;
}

Compilemos y ejecutemos el programa anterior, esto producirá el siguiente resultado:

Content of d1 before swap operation
1
2
3
4
5
Content of d2 before swap operation
50
60
70

Content of d1 after swap operation
50
60
70
Content of d2 after swap operation
1
2
3
4
5