Iterator
- Operators
- *, dereferenceing the iterator
- ++/--, moves the iterator to the next/previous element
- ==/!=, comparison operators to determine if two iterators point to the same element
- =, assign the iterator to a new position
- Data Types
- iterator, begin()/end(), begin points to the beginning of the elements in the container, end doesn't point to the last element in the list
- const_iterator, cbegin()/cend()
- Iterators are a generalization of pointers, a pointer is an iterator, but not necessarily the other way round
begin() and end()
- begin(), returns an iterator pointing to the first element in the sequence
- end(), returns an iterator pointing to the past-the-end element in the sequence
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
//iterator points to elements in the list
for(auto it = std::begin(c); it != std::end(c); ++it)
*it = *it*10;
//const_iterator points to const value
for(auto it = std::begin(c); it != std::end(c); ++it)
std::cout<<*it<<std::endl;
return 0;
}
Iterating through by Index
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
for(int i = 0; i < c.size(); i++)
c.begin()[i] *= 10;
for(int i = 0; i < c.size(); i++)
std::cout<<c.begin()[i]<<std::endl;
return 0;
}
Iterating through a vector
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
//iterator points to elements in the list
for(std::vector<int>::iterator it = c.begin(); it != c.end(); ++it)
*it = *it*10;
//const_iterator points to const value
for(std::vector<int>::const_iterator it = c.cbegin(); it != c.cend(); ++it)
std::cout<<*it<<std::endl;
return 0;
}
Iterating through a list
#include <iostream>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
for(std::list<int>::iterator it = l.begin(); it != l.end(); ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
return 0;
}
Iterating through a set
#include <iostream>
#include <set>
int main(int argc, char *argv[])
{
std::set<int> l = {1, 2, 3, 4};
for(std::set<int>::iterator it = l.begin(); it != l.end(); ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
return 0;
}
Iterating through a map
#include <iostream>
#include <map>
#include <string>
#include <utility>
int main(int argc, char *argv[])
{
std::map<std::string, int> m;
m.insert(std::make_pair("apple", 4));
m.insert(std::make_pair("orange", 2));
m.insert(std::make_pair("banana", 1));
m.insert(std::make_pair("grapes", 3));
m.insert(std::make_pair("mango", 6));
m.insert(std::make_pair("peach", 5));
for(std::map<std::string, int>::iterator it = m.begin(); it != m.end(); ++it)
std::cout<<it->first<<" "<<it->second<<std::endl;
return 0;
}
Container Template
#include <iostream>
#include <vector>
template <class Iter>
void display(Iter it, Iter end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
display(c.begin(), c.end());
//display(&c[0], c.size());
return 0;
}
Convert Iterator to Pointer
#include <iostream>
#include <vector>
void display(int *ptr, int size)
{
for(int i = 0; i < size; i++)
std::cout<<*ptr++<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
std::vector<int>::iterator it = c.begin();
decltype(&*it) ptr;
//int *ptr;
ptr = &*it;
if(ptr == nullptr)
std::cout<<"nullptr ..."<<std::endl;
display(ptr, c.size());
return 0;
}