Iterator
advance
#include <iostream>
#include <iterator>
#include <list>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
template <class T>
void rDisplay(T start, T it)
{
std::advance(it, -1);
while(it != start)
{
std::cout<<*it<<" ";
std::advance(it, -1);
}
std::cout<<*it<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
display(l.begin(), l.end());// 1 2 3 4
rDisplay(l.begin(), l.end());// 4 3 2 1
return 0;
}
distance
#include <iostream>
#include <iterator>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
auto start = l.begin();
auto end = l.end();
std::advance(start, 1);
std::cout<<"Distance: "<<std::distance(start, end)<<std::endl;//3
return 0;
}
begin and end
#include <iostream>
#include <iterator>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
for(auto it = std::begin(l); it != std::end(l); ++it)// 1 2 3 4
std::cout<<*it<<" ";
std::cout<<std::endl;
return 0;
}
prev and next
#include <iostream>
#include <iterator>
#include <list>
int main(int argc, char *argv[])
{
std::list<int> l = {1, 2, 3, 4};
auto it = l.begin();
std::advance(it, 2);
std::cout<<"Prev: "<<*std::prev(it)<<std::endl;//2
std::cout<<"Current: "<<*it<<std::endl;//3
std::cout<<"Next: "<<*std::next(it)<<std::endl;//4
return 0;
}
back_inserter
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 = {10, 20, 30, 40};
std::copy(v2.begin(), v2.end(), std::back_inserter(v1));//1 2 3 4 10 20 30 40
std::fill_n(std::back_inserter(v1), 4, 100);//1 2 3 4 10 20 30 40 100 100 100 100
display(v1.begin(), v1.end());
return 0;
}
- back_insert iterator allows algorithmss that overwrite elements, such as copy, to insert new elements at the end of the container
- Container type needs push_back function, such as vector, deque, and list
front_inserter
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> v1 = {1, 2, 3, 4};
std::list<int> v2 = {10, 20, 30, 40};
std::copy(v2.begin(), v2.end(), std::front_inserter(v1));//40 30 20 10 1 2 3 4
std::fill_n(std::front_inserter(v1), 4, 100);//100 100 100 100 40 30 20 10 1 2 3 4
display(v1.begin(), v1.end());
return 0;
}
- front_insert iterator allows algorithmss that overwrite elements, such as copy, to insert new elements at the beginning of the container
- Container type needs push_front function, such as deque, and list
inserter
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> v1 = {1, 2, 3, 4};
std::list<int> v2 = {10, 20, 30, 40};
auto it = v1.begin();
std::advance(it, 1);
std::copy(v2.begin(), v2.end(), std::inserter(v1, it));//1 10 20 30 40 2 3 4
std::fill_n(std::inserter(v1, it), 4, 100);//1 10 20 30 40 100 100 100 100 2 3 4
display(v1.begin(), v1.end());
return 0;
}
- insert iterator allows algorithmss that overwrite elements, such as copy, to insert new elements at a specific position of the container
- Container type needs insert function, such as vector, deque, and list
make_move_inserter
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> v1 = {1, 2, 3, 4};
std::list<int> v2 = {10, 20, 30, 40, 50};
std::copy(std::make_move_iterator(v2.begin()), std::make_move_iterator(v2.end()), std::begin(v1));
display(v1.begin(), v1.end());//only the first four elements of v2 are moved to v1, since v1 only has four elements
v2.clear();//v2 has been moved to v1, clear it
return 0;
}
- make_move_iterator dereference an iterator to a rvalue reference
reverse_iterator
#include <iostream>
#include <iterator>
#include <list>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::list<int> v = {1, 2, 3, 4};
display(v.rbegin(), v.rend());//only the first four elements of v2 are moved to v1, since v1 only has four elements
auto from = v.begin();
auto until = v.end();
std::reverse_iterator<decltype(from)> rev_until(from);
std::reverse_iterator<decltype(until)> rev_from(until);
display(rev_from, rev_until);
return 0;
}
istream_iterator
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
int main(int argc, char *argv[])
{
std::ifstream inputFile("main.cpp");
std::vector<std::string> container;
std::istream_iterator<std::string> in(inputFile);
std::istream_iterator<std::string> eof;
while(in != eof)
{
std::cout<<*in<<std::endl;
++in;
}
inputFile.close();
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::ifstream inputFile("main.cpp");
std::vector<std::string> container;
std::istream_iterator<std::string> in(inputFile);
std::istream_iterator<std::string> eof;
std::copy(in, eof, std::back_inserter(container));
inputFile.close();
display(container.begin(), container.end());//display whole file word by word
return 0;
}
ostream_iterator
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include <algorithm>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
}
int main(int argc, char *argv[])
{
std::ofstream outputFile("temp.txt");
std::vector<int> container = {1, 2, 3, 4};
std::ostream_iterator<int> out(outputFile, ", ");
//std::ostream_iterator<int> out(std::cout, ", ");
std::copy(container.begin(), container.end(), out);
outputFile<<"Temp"<<std::endl;
outputFile.close();
return 0;
}
Reference