fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void printVec(vector<int> fv){
  5. for(int i=0;i<fv.size();i++){
  6. cout << fv[i] << " ";
  7. }
  8. cout << endl;
  9. }
  10. void print_it_of_vec(vector<int> fv1){
  11. vector<int> ::iterator fit;
  12. for(fit = fv1.begin();fit != fv1.end();++fit){
  13. cout << *fit << " ";
  14. }
  15. cout << endl;
  16. }
  17. /*
  18. ITERATORS: Pointer like structures, helps in accessing
  19. elements of containers(e.g. vector, array, lists, etc)
  20. using indexation just like Pointers are used to indicate
  21. an element in an array. Some iterators are discussed below-
  22. ###Syntax : container_name<data_type> ::iterator it_name;
  23. ###Purpose : To iterate through a container that doenst
  24. support indexing, unlike vectors.
  25. 1.begin(): Points to the first element;
  26. 2.end(): Points to the last element.
  27.  
  28.  
  29. */
  30. int main(){
  31. vector<int> v1 = {2,3,5,6,7};
  32. printVec(v1);
  33. vector<int> ::iterator it1 = v1.begin();
  34. cout << "value at iterator " << *it1 << endl;
  35. cout << *(it1 + 1) << endl;
  36. /*for(it1 = v1.begin(); it1 != v1.end();++it1){
  37. cout << (*(it1)) << " ";
  38. }
  39. cout << endl;*/
  40. print_it_of_vec(v1);
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5284KB
stdin
1
3
1 2 2

stdout
2 3 5 6 7 
value at iterator 2
3
2 3 5 6 7