-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch11_Sec2.cpp
92 lines (90 loc) · 3.51 KB
/
Ch11_Sec2.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// C++ Primer 4th Edition Chapter 11 Section 11.2 Self-Practise
#include<iostream>
#include<vector>
#include<list>
#include<algorithm> /* count, fill_n, copy, sort, unique, distance, find_first_of */
#include<numeric> /* accumlate */
#include<iterator> /* back_inserter */
#include<cstdlib> /* srand, rand */
#include<ctime> /* time */
using namespace std;
int main()
{
int val;
vector<int> ivec; // empty vector
// initialize random seed:
srand( time(NULL) );
// Part 1
for ( int i = 0; i != 10; ++i )
{
ivec.push_back( rand() % 10 + 1 );
}
fill_n( back_inserter( ivec ), 5, rand() % 10 );
vector<int>::iterator iter;
cout << "Elements in ivec are:" << endl;
for ( iter = ivec.begin(); iter != ivec.end(); ++iter )
{
cout << *iter << "\t" ;
if ( (iter-ivec.begin()+1) % 5 == 0)
cout <<endl;
}
val = rand() % 10 + 1;
cout << count( ivec.begin(), ivec.end(), val ) << " elements equal to " << val << endl;
cout << "The summation of elements in ivec is: " << accumulate( ivec.begin(), ivec.end(), 0.0 ) << endl;
cout << endl;
// Part 2
list<int> ilst(14);
copy( ivec.begin(), ivec.end(), ilst.begin() ); // less effective than constructor
/* when the target container is not empty, with different last(3rd) argument leading to different results:
begin() makes a front-to-last copy, eg: vec_original = 0 1 2 3 4 5; vec_copy = 0 1 2 3
end() makes a last-to-front copy, eg: vec_original = 0 1 2 3 4 5; vec_copy = 2 3 4 5
it is a circular copy, eg: when 3rd agr is ++begin(),we get vec_copy = 3 0 1 2
and --end(),we get vec_copy = 3 4 5 0*/
cout << "Elements in ivec2 or ilst are:" << endl;
for ( list<int>::iterator ilter = ilst.begin(); ilter != ilst.end(); ++ilter )
{
cout << *ilter << " " ;
}
cout <<endl;
vector<int> ivec2(ivec);
sort( ivec2.begin(), ivec2.end() ); // list cannot be used to sort for there is a iterator subtraction
cout << "After sorting:" << endl;
for ( iter = ivec2.begin(); iter != ivec2.end(); ++iter )
{
cout << *iter << " " ;
}
cout <<endl;
iter = unique( ivec2.begin(), ivec2.end() );
/*template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last)
//template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
{ // std::unique is equivalent to this:
if (first==last)
return last;
ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}*/
ivec2.erase( iter, ivec2.end() );
//ivec2.resize( distance(ivec2.begin(),iter) ); // does the same thing as erase
cout << "After eliminating duplicates:" << endl;
for ( iter = ivec2.begin(); iter != ivec2.end(); ++iter )
{
cout << *iter << " " ;
}
cout <<endl << endl;
// Part 3
ilst.resize( ivec2.size() );
copy( ivec2.begin(), ivec2.end(), ilst.begin() );
list<int>::iterator it = ilst.begin();
val = 0;
while ( ( it = find_first_of(it,ilst.end(), ivec.begin(), ivec.end()) ) != ilst.end() )
{
++ val; ++it;
}
cout << "Found " << val << " same values both in ivec and ilst(or ivec2)." << endl;
return 0;
}