C++ vector find()

Description

C++ vector find()

#include <iostream> 
#include <array> 
#include <vector> 
#include <ios> 


using namespace std; 

int main(int argc, char *argv [])
{   /*from w w  w  . j a v a  2 s . co m*/

    using MyVector = vector<int>; 

        for (unsigned int i = 0; i < myVector.size(); ++i) 
        { 
                if (myVector[i] == 2) 
                { 
                        cout << "Found: " << myVector[i] 
                                << " at position: " << i << endl; 
                } 
        } 
  
        for (auto iter = myVector.begin(); iter != myVector.end(); ++iter) 
        { 
                if (*iter == 2) 
                { 
                        cout << "Found: " << *iter << endl; 
                } 
        } 
  
        for (auto& number : myVector) 
        { 
                if (number == 2) 
                { 
                        cout << "Found : " << number << endl; 
                } 
        } 
  
        MyVector::iterator found = find(myVector.begin(), myVector.end(), 2); 
        if (found != myVector.end()) 
        { 
                cout << "Found : " << *found << endl; 
        } 
        return 0; 
}



PreviousNext

Related