Find an element of a struct in a list

1

given this structure:

struct Libro{
string nombre;
int codigo;
bool disp;
};

and this list list <Libro> libro;

I would like to know how to find a specific book by means of the code, I mean something so the user places the code and shows the name, the code and the availability .. I was told that with std: find_if and I have tried it with vectors of integers or integer lists .. but I can not find how to make it work when it comes to list of structs

    
asked by Marcel Salazar 31.07.2018 в 04:41
source

2 answers

1

In the next part I show you an example using the struct Book:

#include <iostream>
#include <algorithm>
#include <list>
#include <functional>

struct Libro{
    std::string nombre;
    int codigo;
    bool disp;
};

std::ostream &operator<<(std::ostream &o, const Libro &l)
{
    return o << l.nombre << ' ' << l.codigo << ' ' << l.disp;
}

static bool buscar_por_codigo(const Libro & libro, int n){
    return libro.codigo == n;
}

int main()
{
    std::list<Libro> libros = { {"libro1", 1, true},
                                {"libro2", 4, false},
                                {"libro3", 2, false},
                                {"libro4", 5, false}};

    int n = 2;
    std::list<Libro>::iterator it = std::find_if(libros.begin(),
                                                 libros.end(),
                                                 std::bind(&buscar_por_codigo,
                                                           std::placeholders::_1,
                                                           n));
    if(it != libros.end())
        std::cout << *it <<"\n";
    else
        std::cout << "No hay coincidencias\n";
    return 0;
}
    
answered by 31.07.2018 / 05:24
source
2
  

They commented to me that with std:find_if and I have tried it with vectors of integers or lists of integers .. but I can not find how to make it work when it comes to list of structs .

It is irrelevant to use std:find_if with vectors or lists of any type of data, the functions of the header <algorithm> are templates ready to receive any iterator . In particular, the most common way to use the std:find_if function is using the overhead that receives two iterators (start and end) and a predicate unary.

The predicate is a function, lambda or functor that expects to receive an instance of the data to look for and must return false if it does not match the search criteria or true otherwise; if it finds an element whose predicate returns true ends the search and returns the iterator that points to the found element or the final iterator otherwise.

Therefore, your search code might look like this:

std::list<Libro> libros{ /* rellenar con datos */ };
int codigo;

std::cout << "Código a buscar: ";
std::cin >> codigo;

auto l = std::find_if(libros.begin(), libros.end(), [&codigo](const Libro &l)
{
    return l.codigo == codigo;
});

if (l == libros.end())
    std::cout << "El libro con código " << codigo << " no ha sido encontrado.";
else
    std::cout << "Libro encontrado " << *l;
    
answered by 31.07.2018 в 08:17