Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Initialize Vector Iterator

#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> num {1, 2, 3, 4, 5};

  // declare iterator
  vector<int>::iterator iter;

  // initialize the iterator with the first element
  iter = num.begin();

  // print the vector element
  cout << "num[0] = " << *iter << endl;

  // iterator points to the 4th element
  iter = num.begin() + 3;
  cout << "num[3] = " << *iter << endl;

  // iterator points to the last element
  iter = num.end() - 1;
  cout << "num[4] = " << *iter;

  return 0;
}
Comment

Initialize Vector Iterator Through Vector Using Iterators

#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> num {1, 2, 3, 4, 5};

  // declare iterator
  vector<int>::iterator iter;

  // use iterator with for loop
  for (iter = num.begin(); iter != num.end(); ++iter) {
    cout << *iter << "  ";
  }

  return 0;
}
Comment

Initialize Vector Iterator with begin() function

vector<int> num = {1, 2, 3, 4, 5};
vector<int>::iterator iter;

// iter points to num[0]
iter = num.begin();
Comment

Initialize Vector Iterator with end() function

// iter points to the last element of num
iter = num.end() - 1;
Comment

PREVIOUS NEXT
Code Example
Cpp :: printing sub arrays 
Cpp :: auto keyword 
Cpp :: std::throw_with_nested 
Cpp :: determining whether a array is a subsequence of another array 
Cpp :: C++ Features 
Cpp :: clang does not recognize std::cout 
Cpp :: minimum no of jump required to reach end of arry 
Cpp :: how to fixed how many digit will be after point in c++ 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: sin trigonometric function 
Cpp :: 1281. Subtract the Product and Sum of Digits of an Integer leetcode solution in c++ 
Cpp :: Codeforces Round #376 (Div. 2), problem: (A) Night at the Museum 
Cpp :: how to use comparitor in priority queu in c++ 
Cpp :: how to get the last digit of a number 
Cpp :: return value optimization example 
Cpp :: delete node in a linked list leetcode 
Cpp :: declare a structer in cpp 
Cpp :: Max / Min Stack in c++ using stl in O(1) time and space 
Cpp :: c++ is nan 
Cpp :: constants in cpp 
Cpp :: loop in c++ 
Cpp :: aliasing c++ 
Cpp :: flags of open operation c++ 
C :: c colourful text 
C :: boolean in c 
C :: octave square each element matrix 
C :: curl authorization header 
C :: first program in c 
C :: c 2d array dimensions 
C :: c convert number to string 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =