Question : Initialize Vector Iterator
Answered by : softhunt
#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;
}
Source : https://softhunt.net/c-vector-with-examples/ | Last Update : Thu, 12 May 22
Question : Initialize Vector Iterator Through Vector Using Iterators
Answered by : softhunt
#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;
}
Source : https://softhunt.net/c-vector-with-examples/ | Last Update : Thu, 12 May 22
Question : Initialize Vector Iterator with begin() function
Answered by : softhunt
vector<int> num = {1, 2, 3, 4, 5};
vector<int>::iterator iter;
// iter points to num[0]
iter = num.begin();
Source : https://softhunt.net/c-vector-with-examples/ | Last Update : Thu, 12 May 22