Si la cantidad de elementos es de cientos, puede usar la búsqueda secuencial simple. Este algoritmo está incorporado en C++ como la función find()
:
#include <algorithm> // for find()
typedef std::vector<Cell> CellList;
CellList checked_cells;
// .....
Cell cellToSearch;
if (is_in_checked_cells (cellToSearch, cells))
{
// .....
}
// Makes a sequential search using find().
static bool
is_in_checked_cells (const Cell &cell, const CellList &cells)
{
CellList::const_iterator end = cells.end();
CellList::const_iterator item = std::find (cells.begin(), end, cell);
return (item != end);
}
Asegúrese Cell
ha anulado operator<
.
Si la lista es muy grande, es posible que desee utilizar la búsqueda binaria, que también viene incluido con C++:
#include <algorithm> // for sort() and binary_search()
CellList checked_cells;
// Make sure the cells are sorted.
checked_cells.sort (checked_cells.begin(), checked_cells.end());
Cell cellToSearch;
if (is_in_checked_cells (cellToSearch, cells))
{
// .....
}
// Searches using binary_search().
static bool
is_in_checked_cells (const Cell &cell, const CellList &cells)
{
return std::binary_search (cells.begin(), cells.end(), cell);
}
Puede aceptar respuestas para sus preguntas anteriores, marcando la casilla correspondiente a la izquierda de respuestas – Amsakanna
Ja, gracias, nunca se dio cuenta. :) – Befall