User Tools

Site Tools


in202:seance_3:td_3:part_iii

This is an old revision of the document!


TD3 - Part 3 - Les itérateurs

Question n°1

Nous considérons la fonction find définie dans la première partie du TD :

template<class T>
T find(const T* theArray, int theArrayLength, T theValue)
{
    for(int index = 0; index < theArrayLength; index ++)
    {
        if(theArray[index] == theValue)
            return index;
    }
    return -1;
}           

Pouvez-vous la réécrire pour qu'elle fonctionne désormais avec des itérateurs, cette fonction devra avoir l'en-tête suivante :

template<class inputIterator>
inputIterator find(inputIterator first, inputIterator last, T theValue)

Question n° 2

Nous souhaitons utiliser la fonction précédente pour tester si l'élément 5 est dans le vecteur suivant :

#include<vector>
 
std::vector<int> vec = {3, 4, 8, 2, 4, 1 };

Ecrivez le code qui fait l'opération et déterminer si l'élément est présent ou n'est pas présent dans le vecteur.

in202/seance_3/td_3/part_iii.1617791824.txt.gz · Last modified: 2021/04/07 10:37 by bmonsuez