-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterators.cpp
More file actions
80 lines (66 loc) · 1.77 KB
/
iterators.cpp
File metadata and controls
80 lines (66 loc) · 1.77 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
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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
#define N 20
int check(pair<int,int> pair1, pair<int,int> pair2)
{
return pair1.second > pair2.second;
}
int main()
{
//create list of random values
vector<int> nums;
srand(time(NULL));
for(int i = 0; i < N; i++)
nums.push_back(rand()%N);
//prints values
for(auto item : nums) //easy way to print
cout << item << " ";
//for(vector<int>::iterator iter = nums.begin(); iter!=nums.end();iter++) //hard way to print
//cout << *iter << " ";
cout << endl;
cout << "len=" << nums.size() << endl;
//remove 0's in vector
int targ = 0;
vector<int>::iterator pos = find(nums.begin(),nums.end(),targ);
while(pos!=nums.end())
{
cout << "erasing " << targ << " at position " << pos-nums.begin()+1 << endl;
nums.erase(pos);
pos = find(nums.begin(),nums.end(),targ);
}
cout << "len=" << nums.size() << endl;
//print sorted
sort(nums.begin(),nums.end());
for(auto item : nums)
cout << item << " ";
cout << endl;
//print reserve order
for(vector<int>::reverse_iterator x = nums.rbegin(); x!=nums.rend(); x++)
cout << *x << " ";
cout << endl;
//calc frequency
unordered_map<int,int> hash;
for(auto item : nums)
{
if(hash.find(item)==hash.end())
hash[item] = 1;
else
hash[item]++;
}
vector<pair<int,int> > pairs(hash.begin(),hash.end());
sort(pairs.begin(),pairs.end());
cout << "Num, Freq" << endl;
for(int i = 0; i < pairs.size(); i++)
{
cout << pairs[i].first << " " << pairs[i].second << endl;
}
sort(pairs.begin(),pairs.end(),check);
cout << "Sorted by freq" << endl;
for(int i = 0; i < pairs.size(); i++)
{
cout << pairs[i].first << " " << pairs[i].second << endl;
}
}