C++ vector loop via for statement

Description

C++ vector loop via for statement

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


using namespace std; 

int main(int argc, char *argv [])
{   /*from  w ww .j  av a  2  s.  c  om*/

    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