std::is_partitioned en C++

std::is_partitioned se usa para encontrar si el rango [primero, último] está particionado o no. Se dice que un rango se divide con respecto a una condición si todos los elementos para los que la condición se evalúa como verdadera preceden a aquellos para los que es falsa.

Se define en el archivo de cabecera. Si el rango en el que queremos verificar si está particionado o no está vacío, entonces esta función devuelve verdadero.
Sintaxis:

 bool is_partitioned (InputIterator first, 
                      InputIterator last, UnaryPredicate pred);

first: Input iterator to the first element in the range.
last: Input iterator to the last element in the range.
pred: Unary function that accepts an element in the 
range as argument, and returns a value convertible to bool. 
The value returned indicates whether the element belongs to
the first group (if true, the element is expected before all
the elements for which it returns false).
The function shall not modify its argument.
This can either be a function pointer or a function object.


Returns: It returns true if all the elements in the range [first, last)
for which pred returns true precede those for which it returns false.
Otherwise it returns false.
If the range is empty, the function returns true.

// C++ program to demonstrate the use of std::is_partitioned
#include <iostream>
#include <algorithm>
#include <vector>
  
// Defining the BinaryFunction
bool pred(int a)
{
    return (a % 3 == 0);
}
  
using namespace std;
int main()
{
    // Declaring first vector
    vector<int> v1 = { 3, 6, 9, 10, 11, 13 };
  
    // Using std::is_partitioned
    bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
  
    if (b == 1) {
        cout << "It is partitioned";
    } else {
        cout << "It is not partitioned.";
    }
    return 0;
}

Producción:

It is partitioned

Explicación: Aquí, en este programa primero, hemos almacenado elementos en un vector, y luego estamos comprobando si todos los elementos divisibles por 3 están presentes antes que aquellos que no son divisibles por 3. Dado que esta condición se evalúa como verdadera para los elementos tomados vector por lo tanto, esta función devuelve 1 aquí, ya que está particionado.

Otro ejemplo

  • Para verificar si todos los elementos pares e impares están particionados

    // C++ program to demonstrate the use of std::is_partitioned
    #include <iostream>
    #include <algorithm>
    #include <vector>
      
    // Defining the BinaryFunction
    bool pred(int a)
    {
        return (a % 2 == 0);
    }
      
    using namespace std;
    int main()
    {
        // Declaring first vector
        vector<int> v1 = { 2, 4, 6, 3, 5, 7, 9 };
      
        // Using std::is_partitioned
        bool b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "All the even no. are present before odd no.";
        } else {
            cout << "All the even no. are not present before odd no.";
        }
      
        // Inserting an even no. at the end of v1
        // so std::is_partitioned returns false
        v1.push_back(16);
      
        // Now again using std::is_partitioned
        b = std::is_partitioned(v1.begin(), v1.end(), pred);
      
        if (b == 1) {
            cout << "\nAll the even no. are present before odd no.";
        } else {
            cout << "\nAll the even no. are not present before odd no.";
        }
      
        return 0;
    }

    Producción:

    All the even no. are present before odd no.
    All the even no. are not present before odd no.
    

Este artículo es una contribución de Mrigendra Singh . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *