2013-10-10 11 views
13

Per esempio, io ho questa matrice:Come posso verificare se l'int specificato esiste nell'array?

int myArray[] = { 3, 6, 8, 33 }; 

Come controllare se dato variabile x è in esso?

Devo scrivere la mia funzione ed eseguire il ciclo della matrice, oppure c'è in C++ moderno equivalente a in_array in PHP?

+1

Guarda l'esempio per [ 'std :: find'] (http://www.cplusplus.com/reference/algorithm/find/). – DCoder

risposta

25

È possibile utilizzare std::find per questo:

#include <algorithm> // for std::find 
#include <iterator> // for std::begin, std::end 

int main() 
{ 
    int a[] = {3, 6, 8, 33}; 
    int x = 8; 
    bool exists = std::find(std::begin(a), std::end(a), x) != std::end(a); 
} 

std::find restituisce un iteratore alla prima occorrenza di x, o di un iteratore a un oltre la fine della gamma, se x non viene trovato.

+2

O semplicemente: 'bool esiste = std :: any_of (std :: begin (array), std :: end (array), [&] (int i) {return i == x;});' –

+2

'std :: find' è bello se non si è su C++ 11. Quindi di nuovo 'std: begin' e' std: end' sono solo C++ 11. –

11

Penso che si sta cercando std::any_of, che restituirà una risposta vera/falsa per rilevare se un elemento è in un contenitore (array, vector, deque, ecc)

int val = SOME_VALUE; // this is the value you are searching for 
bool exists = std::any_of(std::begin(myArray), std::end(myArray), [&](int i) 
{ 
    return i == val; 
}); 

Se si desidera sapere dove si trova l'elemento, std::find restituirà un iteratore al primo elemento corrispondente ai criteri che fornisci (o un predicato che gli dai).

int val = SOME_VALUE; 
int* pVal = std::find(std::begin(myArray), std::end(myArray), val); 
if (pVal == std::end(myArray)) 
{ 
    // not found 
} 
else 
{ 
    // found 
} 
-2

È necessario eseguirne il ciclo. C++ non implementa alcun modo più semplice per farlo quando si ha a che fare con matrici di tipi primitivi.

vedere anche questa risposta: C++ check if element exists in array

+1

Sì, sì - std :: find. È ancora un loop alla fine, ma un loop che è già stato scritto. –

+0

La domanda collegata ha anche una risposta che coinvolge std :: find_first_of ... – badgerr

+0

'std :: find',' std :: find_first_of', 'std :: any_of'. Tutti implementano cicli, ma il linguaggio li ha forniti per voi (quindi non è necessario scrivere i propri). In C++, la maggior parte delle volte raramente dovrai scrivere il tuo loop. –

2

È quasi mai a scrivere i propri loop in C++. Qui puoi usare std::find.

const int toFind = 42; 
int* found = std::find (myArray, std::end (myArray), toFind); 
if (found != std::end (myArray)) 
{ 
    std::cout << "Found.\n" 
} 
else 
{ 
    std::cout << "Not found.\n"; 
} 

std::end richiede C++ 11. Senza di essa, è possibile trovare il numero di elementi nella matrice con:

const size_t numElements = sizeof (myArray)/sizeof (myArray[0]); 

... e alla fine con:

int* end = myArray + numElements; 
1
int index = std::distance(std::begin(myArray), std::find(begin(myArray), end(std::myArray), VALUE)); 

restituisce un indice valido (lunghezza della matrice) se non trovato

2

Prova questa

#include <iostream> 
#include <algorithm> 


int main() { 
    int myArray[] = { 3 ,6 ,8, 33 }; 
    int x = 8; 

    if (std::any_of(std::begin(myArray), std::end(myArray), [=](int n){return n == x;})) { 
     std::cout << "found match/" << std::endl; 
    } 

    return 0; 

}

Problemi correlati