Namespace
Varianti

std::find, std::find_if, std::find_if_not

Da cppreference.com.
< cpp‎ | algorithm


 
 
Algoritmo libreria
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Non modifica le operazioni di sequenza
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
find
find_if
find_if_not


(C++11)
find_end
find_first_of
adjacent_find
search
search_n
Modifica delle operazioni di sequenza
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Partizionamento operazioni
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ordinamento delle operazioni (su intervalli ordinati)
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Binarie (le operazioni di ricerca sui campi ordinati)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Impostare le operazioni (su intervalli ordinati)
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Heap operazioni
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Minimo / massimo le operazioni
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Operazioni numeriche
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Libreria C
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );
(1)
template< class InputIt, class UnaryPredicate >

InputIt find_if( InputIt first, InputIt last,

                 UnaryPredicate p );
(2)
template< class InputIt, class UnaryPredicate >

InputIt find_if_not( InputIt first, InputIt last,

                     UnaryPredicate q );
(3) (dal C++11)
Queste funzioni trovare il primo elemento nella [first, last) gamma che soddisfa criteri specifici:
Original:
These functions find the first element in the range [first, last) that satisfies specific criteria:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1. find ricerche di un elemento pari a value
Original:
1. find searches for an element equal to value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2. find_if cerca un elemento per il quale predicato p ritorna true
Original:
2. find_if searches for an element for which predicate p returns true
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3. find_if_not cerca elemento per il quale predicato q ritorna false
Original:
3. find_if_not searches for element for which predicate q returns false
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Indice

[modifica] Parametri

first, last -
la gamma di elementi da esaminare
Original:
the range of elements to examine
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
value -
valore per confrontare gli elementi
Original:
value to compare the elements to
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
p - unary predicate which returns ​true
per l'elemento richiesto
Original:
for the required element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

q - unary predicate which returns ​false
per l'elemento richiesto
Original:
for the required element
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.

[modifica] Valore di ritorno

Iteratore al primo elemento che soddisfa la condizione o last se tale elemento non viene trovato.
Original:
Iterator to the first element satisfying the condition or last if no such element is found.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Complessità

Al massimo last - first applicazioni del predicato
Original:
At most last - first applications of the predicate
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Possibile implementazione

First version
template<class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first) {
        if (*first == value) {
            return first;
        }
    }
    return last;
}
Second version
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (p(*first)) {
            return first;
        }
    }
    return last;
}
Third version
template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    for (; first != last; ++first) {
        if (!q(*first)) {
            return first;
        }
    }
    return last;
}
Se non si dispone di C + +11, l'equivalente di std::find_if_not è quello di utilizzare std::find_if con il predicato negato.
Original:
If you do not have C++11, an equivalent to std::find_if_not is to use std::find_if with the negated predicate.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    return std::find_if(first, last, std::not1(q));
}

[modifica] Esempio

Nell'esempio seguente vengono individuati un numero intero in un vettore di interi .
Original:
The following example finds an integer in a vector of integers.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <iostream>
#include <algorithm>
#include <vector>
 
int main()
{
    int n1 = 3;
    int n2 = 5;
 
    std::vector<int> v{0, 1, 2, 3, 4};
 
    auto result1 = std::find(v.begin(), v.end(), n1);
    auto result2 = std::find(v.begin(), v.end(), n2);
 
    if (result1 != v.end()) {
        std::cout << "v contains: " << n1 << '\n';
    } else {
        std::cout << "v does not contain: " << n1 << '\n';
    }
 
    if (result2 != v.end()) {
        std::cout << "v contains: " << n2 << '\n';
    } else {
        std::cout << "v does not contain: " << n2 << '\n';
    }
}

Output:

v contains: 3
v does not contain: 5

[modifica] Vedi anche

trova due identici (o qualche altra relazione) elementi adiacenti l'uno all'altro
Original:
finds two identical (or some other relationship) items adjacent to each other
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione di modello) [edit]
trova l'ultima sequenza di elementi di un certo intervallo
Original:
finds the last sequence of elements in a certain range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione di modello) [edit]
searches for any one of a set of elements
(funzione di modello) [edit]
trova la prima posizione in cui due intervalli diversi
Original:
finds the first position where two ranges differ
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione di modello) [edit]
searches for a range of elements
(funzione di modello) [edit]