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

PREVIOUS NEXT
Code Example
Cpp :: program to swap max and min in matrix 
Cpp :: c++ multiply char 
Cpp :: hierarchical inheritance in c++ employee 
Cpp :: What is a ~ in c++ 
Cpp :: full implementation of binary search tree in C++ 
Cpp :: programs using vectors in c++ 
Cpp :: converting char to integer c++ 
Cpp :: C++ cout iostream 
Cpp :: . Write a C++ program to calculate area of a Triangle 
Cpp :: c++ random int troll 
Cpp :: arduino falling edge 
Cpp :: log base e synthax c++ 
Cpp :: if else in c++ 
Cpp :: c++ classes 
Cpp :: Start mongodb community server 
Cpp :: or in c++ 
Cpp :: c++ hash 
Cpp :: c++ function pointer variable 
Cpp :: c++ copy constructor 
Cpp :: Maximum element in a map c++ 
Cpp :: c++ structs 
Cpp :: is there garbage collection in c++ 
Cpp :: ue4 endoverlap c++ 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: how to do if command in c++ 
Cpp :: new expression 
Cpp :: how to pronounce beaucoup 
Cpp :: C++ float and double simple example 
Cpp :: int and char in c++ compiler 
Cpp :: I/O Redirection in C++ 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =