1#include <iostream>
2#include <vector>
3using namespace std;
4
5vector<int> myvector;
6
7for (vector<int>::iterator it = myvector.begin();
8 it != myvector.end();
9 ++it)
10 cout << ' ' << *it;
11cout << '\n';
12
1// EXAMPLE
2vector<string> vData;
3vData.push_back("zeroth");
4vData.push_back("first");
5vData.push_back("second");
6vData.push_back("third");
7
8std::vector<string>::iterator itData;
9
10for (itData = vData.begin(); itData != vData.end() ; itData++)
11{
12 auto ElementIndex = itData-vData.begin();
13 auto ElementValue = vData[ElementIndex]; // vData[ElementIndex] = *itData
14 cout << "[ElementIndex:" << ElementIndex << "][ElementValue:" << ElementValue << "]\n";
15}
16
17/* HEADER(S)
18#include <vector>
19#include <iostream>
20using namespace std;
21*/
1#include <iostream>
2#include <algorithm>
3
4template<long FROM, long TO>
5class Range {
6public:
7 // member typedefs provided through inheriting from std::iterator
8 class iterator: public std::iterator<
9 std::input_iterator_tag, // iterator_category
10 long, // value_type
11 long, // difference_type
12 const long*, // pointer
13 long // reference
14 >{
15 long num = FROM;
16 public:
17 explicit iterator(long _num = 0) : num(_num) {}
18 iterator& operator++() {num = TO >= FROM ? num + 1: num - 1; return *this;}
19 iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
20 bool operator==(iterator other) const {return num == other.num;}
21 bool operator!=(iterator other) const {return !(*this == other);}
22 reference operator*() const {return num;}
23 };
24 iterator begin() {return iterator(FROM);}
25 iterator end() {return iterator(TO >= FROM? TO+1 : TO-1);}
26};
27
28int main() {
29 // std::find requires an input iterator
30 auto range = Range<15, 25>();
31 auto itr = std::find(range.begin(), range.end(), 18);
32 std::cout << *itr << '\n'; // 18
33
34 // Range::iterator also satisfies range-based for requirements
35 for(long l : Range<3, 5>()) {
36 std::cout << l << ' '; // 3 4 5
37 }
38 std::cout << '\n';
39}