-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcounting.cpp
More file actions
53 lines (43 loc) · 1.52 KB
/
counting.cpp
File metadata and controls
53 lines (43 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <boost/iterator/counting_iterator.hpp>
#include <boost/iterator/indirect_iterator.hpp>
#include <boost/cstdlib.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
using namespace boost;
int main(int, char*[])
{
// Example of using counting_iterator
cout << "counting from 0 to 4:" << endl;
counting_iterator<int> first(0), last(4);
copy(first, last, ostream_iterator<int>(cout, " "));
cout << endl;
// Example of using make_counting_iterator()
cout << "counting from -5 to 4:" << endl;
copy(make_counting_iterator(-5), make_counting_iterator(5),
ostream_iterator<int>(cout, " "));
cout << endl;
// Example of using counting iterator to create an array of pointers.
int N = 7;
vector<int> numbers;
// Fill "numbers" array with [0,N)
copy(counting_iterator<int>(0), counting_iterator<int>(N),
back_inserter(numbers));
vector<vector<int>::iterator> pointers;
// Use counting iterator to fill in the array of pointers.
// causes an ICE with MSVC6
copy(make_counting_iterator(numbers.begin()),
make_counting_iterator(numbers.end()),
back_inserter(pointers));
// Use indirect iterator to print out numbers by accessing
// them through the array of pointers.
cout << "indirectly printing out the numbers from 0 to "
<< N << endl;
copy(make_indirect_iterator(pointers.begin()),
make_indirect_iterator(pointers.end()),
ostream_iterator<int>(cout, " "));
cout << endl;
return boost::exit_success;
}