//
// main.cpp
// Iterating over a Vector
//
// Created by Himanshu on 11/12/22.
//
#include <iostream>
#include <vector>
using namespace std;
void printVectorUsingIndex ( vector< int > v) {
int n = ( int ) v.size ( ) ;
for ( int i= 0 ; i< n; i++ ) {
cout << v[ i] << " " ;
}
cout << endl;
}
void printVectorUsingIterator ( vector< int > v) {
vector< int > :: iterator it;
for ( it = v.begin ( ) ; it ! = v.end ( ) ; it++ ) {
cout << ( * it) << " " ;
}
cout << endl;
}
void printVector ( vector< int > v) {
for ( auto val : v) {
cout << val<< " " ;
}
cout << endl;
}
int main ( ) {
// Initialisation
vector< int > vec( { 10 , 15 , 25 , 40 , 50 } ) ;
cout << "Iterating over vector using index:" << endl;
printVectorUsingIndex( vec) ;
cout << "Iterating over vector using iterator:" << endl;
printVectorUsingIterator( vec) ;
cout << "Iterating over vector using auto:" << endl;
printVector( vec) ;
return 0 ;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBJdGVyYXRpbmcgb3ZlciBhIFZlY3RvcgovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxMS8xMi8yMi4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp2b2lkIHByaW50VmVjdG9yVXNpbmdJbmRleCAodmVjdG9yPGludD4gdikgewogICAgaW50IG4gPSAoaW50KSB2LnNpemUoKTsKICAgIAogICAgZm9yIChpbnQgaT0wOyBpPG47IGkrKykgewogICAgICAgIGNvdXQ8PHZbaV08PCIgIjsKICAgIH0KICAgIAogICAgY291dDw8ZW5kbDsKfQoKdm9pZCBwcmludFZlY3RvclVzaW5nSXRlcmF0b3IgKHZlY3RvcjxpbnQ+IHYpIHsKICAgIHZlY3RvcjxpbnQ+OjppdGVyYXRvciBpdDsKICAgIAogICAgZm9yIChpdCA9IHYuYmVnaW4oKTsgaXQgIT0gdi5lbmQoKTsgaXQrKykgewogICAgICAgIGNvdXQ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