// Drill on page 753
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
template <class Iter1, class Iter2> Iter2 mycopy(Iter1 f1, Iter1 e1, Iter2 f2) {
while (f1 != e1) {
*f2 = *f1;
f1++;
f2++;
}
return f2;
}
int main() {
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> ell = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int a2[10];
for (int i; i < 10; i++)
a2[i] = a[i];
vector<int> v2 = v;
list<int> ell2 = ell;
for (int i; i < 10; i++)
a2[i] += 2;
for_each(v2.begin(), v2.end(), [](int &val) { val += 3; });
for_each(ell2.begin(), ell2.end(), [](int &val) { val += 4; });
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
cout << endl;
mycopy(v2.begin(), v2.end(), &a2[0]);
for (int i; i < 10; i++)
cout << a2[i] << ", ";
vector<int>::iterator p = find(v2.begin(), v2.end(), 9);
if (p == v2.end())
cout << "\nelement not found!\n";
else
cout << "\nit is the element in position: "
<< " " << (p - v2.begin()) << endl;
}
// ex 20 on page 755
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <random>
#include <string>
#include <vector>
// timing
#include <chrono>
using namespace std::chrono;
using namespace std;
template<class T>
void arrange(T container, vector<int> &numbers) {
container.push_back(numbers[0]);
auto pt=container.begin();
for (int i = 1; i < numbers.size(); i++) {
pt = container.begin();
/*pt = find_if(container.begin(), container.end(),
[i, &numbers](int &n) { return n > numbers[i]; }); */
while (pt!=container.end() && *pt<numbers[i])
pt++;
if (pt == container.end())
container.push_back(numbers[i]);
else {
container.insert(pt, numbers[i]);
}
}
//copy(v.begin(), v.end(), ostream_iterator<int>(cout, ", "));
}
int main() {
static default_random_engine ran;
int iterations = 100000;
vector<int> numbers(iterations); //(iterations);
for (int i = 0; i < iterations; i++) {
numbers[i] = uniform_int_distribution<>{1, 100}(ran);
}
auto t1 = system_clock::now();
vector<int> v;
arrange(v,numbers);
auto t2 = system_clock::now();
cout << iterations << " iterations took "
<< duration_cast<milliseconds>(t2 - t1).count() << " milliseconds\n";
t1 = system_clock::now();
list<int> ell;
arrange(ell,numbers);
t2 = system_clock::now();
cout << iterations << " iterations took "
<< duration_cast<milliseconds>(t2 - t1).count() << " milliseconds\n";
}