-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
97 lines (81 loc) · 2.56 KB
/
map.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
93
94
95
//
// main.cpp
// map
//
// Created by Angela Fox on 3/1/16.
// Copyright © 2016 Angela Fox. All rights reserved.
//
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm> //for std::find()
using namespace std;
//Number of integers to be inserted into list
#define NUMBER 4
int main() {
//Random Number Generator SetUp - uniform distribution
//between 0 and 9 using a time seed
typedef chrono::high_resolution_clock myClock;
myClock::time_point beginning = myClock::now();
myClock::duration d = myClock::now()-beginning;
unsigned seed = (unsigned)d.count();
default_random_engine generator(seed);
uniform_int_distribution<int> insertDist(0,9);
//make list of N distinct random integers
vector<int> randomInt;
while (randomInt.size() < NUMBER) {
int j = insertDist(generator);
bool found = (find(randomInt.begin(), randomInt.end(), j) != randomInt.end());
if (!found) {
randomInt.push_back(j);
}
}
//Create list myList
vector<int> myVector;
int k;
//Fill myList with random ints, inserting each in order
myVector.push_back(randomInt.back());
cout << ' ' << myVector.back() << endl;
randomInt.pop_back();
while(!randomInt.empty()) {
k = randomInt.back();
randomInt.pop_back();
if (k > myVector.back()) {
myVector.push_back(k);
} else {
for (vector<int>::iterator it1 = myVector.begin(); it1 != myVector.end(); ++it1) {
if (k < *it1) {
myVector.insert(it1, k);
break;
}
}
}
//Print out list
for (vector<int>::iterator it2 = myVector.begin(); it2 != myVector.end(); ++it2)
{
cout << ' ' << *it2;
}
cout << '\n';
}
//Generate random remove sequence and remove at that point
vector<int>::iterator it3;
for(int num = NUMBER-1; num >= 0; --num){
uniform_int_distribution<int> removeDist(0,num);
int removeIndex = removeDist(generator);
cout << "remove position: " << removeIndex << endl;
it3 = myVector.begin();
for (int n = 0; n<removeIndex; n++)
{
it3++;
}
it3 = myVector.erase(it3);
//Print out list
for (vector<int>::iterator it4 = myVector.begin(); it4 != myVector.end(); ++it4)
{
cout << ' ' << *it4;
}
cout << '\n';
}
return 0;
}