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 :: dequeue c++ 
Cpp :: char array declaration c++ 
Cpp :: what is the default include path in ubuntu c++ 
Cpp :: how to fill vector from inputs c++ 
Cpp :: remove comments c++ 
Cpp :: c++ formatting 
Cpp :: how to use for c++ 
Cpp :: minheap cpp stl 
Cpp :: 344. reverse string c++ 
Cpp :: c++98 check if character is integer 
Cpp :: iomanip header file in c++ 
Cpp :: statements 
Cpp :: c++ - 
Cpp :: string append at position c++ 
Cpp :: delete c++ 
Cpp :: order 2d array in c++ 
Cpp :: find maximum sum of circular subarray 
Cpp :: 1. Two Sum 
Cpp :: kadane algorithm with negative numbers included as sum 
Cpp :: compile and run cpp file on mac c++ 
Cpp :: vector of vectors c++ 
Cpp :: check if cin got the wrong type 
Cpp :: read a file line by line c++ struct site:stackoverflow.com 
Cpp :: graph colouring 
Cpp :: C++ Initializing a thread with a class/object 
Cpp :: C++ Changing Default Value of Enums 
Cpp :: windows servis from console app 
Cpp :: c to assembly mips converter 
Cpp :: default parameter c++ a field 
Cpp :: sjfoajf;klsjflasdkfjk;lasjfjajkf;dslafjdjalkkkjakkkkkkkkkkkkkkkkfaWZdfbhjkkkk gauds 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =