forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_28.cpp
50 lines (43 loc) · 1.17 KB
/
ex10_28.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
//
// ex10_28.cpp
// Exercise 10.28
//
// Created by pezy on 12/13/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Copy a vector that holds the values from 1 to 9 inclusive, into three other
// containers.
// Use an inserter, a back_inserter, and a front_inserter, respectivly to add
// elements to these containers.
// Predict how the output sequence varies by the kind of inserter and verify
// your predictions
// by running your programs.
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using std::list;
using std::copy;
using std::cout;
using std::endl;
template <typename Sequence> void print(Sequence const& seq)
{
for (const auto& i : seq) std::cout << i << " ";
std::cout << std::endl;
}
int main()
{
std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
// uses inserter
list<int> lst1;
copy(vec.cbegin(), vec.cend(), inserter(lst1, lst1.begin()));
print(lst1);
// uses back_inserter
list<int> lit2;
copy(vec.cbegin(), vec.cend(), back_inserter(lit2));
print(lit2);
// uses front_inserter
list<int> lst3;
copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
print(lst3);
}