iterator c++ code example

Example 1: c++ vector iterator

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

vector<int> myvector;

for (vector<int>::iterator it = myvector.begin();
     it != myvector.end();
     ++it)
   cout << ' ' << *it;
cout << '\n';

Example 2: c++ array interator

#include <array>
#include <vector>

#include <iterator>

int main()
{
    int c_array[5] = {};
    std::array<int, 5> cpp_array = {};
    std::vector<int> cpp_dynarray(5);

    auto c_array_begin = std::begin(c_array); // = c_array + 0
    auto c_array_end = std::end(c_array);     // = c_array + 5

    auto cpp_array_begin = std::begin(cpp_array); // = cpp_array.begin()
    auto cpp_array_end = std::end(cpp_array);     // = cpp_array.end()

    auto cpp_dynarray_begin = std::begin(cpp_dynarray); // = cpp_dynarray.begin()
    auto cpp_dynarray_end = std::end(cpp_dynarray);     // = cpp_dynarray.end()
}

Example 3: c++ vector iterator

// EXAMPLE
vector<string> vData;
vData.push_back("zeroth");
vData.push_back("first");
vData.push_back("second");
vData.push_back("third");

std::vector<string>::iterator itData;

for (itData = vData.begin(); itData != vData.end() ; itData++)
{
  auto ElementIndex = itData-vData.begin();
  auto ElementValue = vData[ElementIndex]; // vData[ElementIndex] = *itData
  cout << "[ElementIndex:" << ElementIndex << "][ElementValue:" << ElementValue << "]\n";
}

/* HEADER(S)
#include <vector>
#include <iostream>
using namespace std;
*/

Example 4: declaring iterator in cpp

vector<int>::iterator ptr;

Example 5: reverse iterator c++

// A reverse_iterator example using vectors

#include <iostream>
#include <vector>

int main() {
	std::vector<int> vec = {1, 2, 3, 4, 5};
  	std::vector<int>::reverse_iterator r_iter;
  
    // rbegin() points to the end of the vector, and rend()
    // points to the front. Use crbegin() and crend() for
  	// the const versions of these interators.
    for (r_iter = vec.rbegin(); r_iter != vec.rend(); r_iter++) {
        std::cout << *r_iter << std::endl;
    }
  	
  	return 0;
}

Example 6: stl iterator

#include <iostream>
#include <algorithm>
 
template<long FROM, long TO>
class Range {
public:
    // member typedefs provided through inheriting from std::iterator
    class iterator: public std::iterator<
                        std::input_iterator_tag,   // iterator_category
                        long,                      // value_type
                        long,                      // difference_type
                        const long*,               // pointer
                        long                       // reference
                                      >{
        long num = FROM;
    public:
        explicit iterator(long _num = 0) : num(_num) {}
        iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
        iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
        bool operator==(iterator other) const {return num == other.num;}
        bool operator!=(iterator other) const {return !(*this == other);}
        reference operator*() const {return num;}
    };
    iterator begin() {return iterator(FROM);}
    iterator end() {return iterator(TO >= FROM? TO+1 : TO-1);}
};
 
int main() {
    // std::find requires an input iterator
    auto range = Range<15, 25>();
    auto itr = std::find(range.begin(), range.end(), 18);
    std::cout << *itr << '\n'; // 18
 
    // Range::iterator also satisfies range-based for requirements
    for(long l : Range<3, 5>()) {
        std::cout << l << ' '; // 3 4 5
    }
    std::cout << '\n';
}

Tags:

Cpp Example