Trouver l’index d’une valeur dans un vecteur

Author:

 vecteur, iterator
Download

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
   int tab[ 10 ] = { 8, 2, 16, 5, 22, 8, 13, 11, 20, 7 };
   vector< int > v( tab, tab + 10 );
   ostream_iterator< int > output( cout, " " );

   cout << "Le vecteur contient: ";
   copy( v.begin(), v.end(), output );
   cout<::iterator indexof;
   //Trouver l'index de la valeur 13
   indexof = find( v.begin(), v.end(), 13 );

   if ( indexof != v.end() )
      cout << "13 trouvé à l'index " << ( indexof - v.begin() );
   else // 16 not found
      cout << "13 n'existe pas dans le vecteur";

   cout << endl;
   return 0;
}
/*
Le vecteur contient: 8 2 16 5 22 8 13 11 20 7
13 trouvé à l'index 6

 */

Leave a Reply

Your email address will not be published. Required fields are marked *