Skip to content

Commit

Permalink
added 10.26 ~ 28 to README.md and simplify 10.2728
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 13, 2014
1 parent 5789b1d commit ea7d4cf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 115 deletions.
11 changes: 11 additions & 0 deletions ch10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ The additional one is for the function to be bound itself.

##[Exercise 10.24](ex10_24.cpp)
##[Exercise 10.25](ex10_25.cpp)

## Exercise 10.26:
>Explain the differences among the three kinds of insert iterators.
- `back_inserter` uses `push_back`.
- `front_inserter` uses `push_front`.
- `insert` uses `insert`
>This function takes a second argument, which must be an iterator into the given container. Elements are inserted ahead of the element denoted by the given iterator.
## [Exercise 10.27](ex10_27.cpp)
## [Exercise 10.28](ex10_28.cpp)
115 changes: 0 additions & 115 deletions ch10/ex10_26_27_28.cpp

This file was deleted.

27 changes: 27 additions & 0 deletions ch10/ex10_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ex10_27.cpp
// Exercise 10.27
//
// Created by pezy on 12/13/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// In addition to unique, the library defines function named unique_copy that
// takes a third iterator denoting a destination into which to copy the unique elements.
// Write a program that uses unique_copy to copy the unique elements from
// a vector into an initially empty list.

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>

int main()
{
std::vector<int> vec{1,1,3,3,5,5,7,7,9};
std::list<int> lst;

std::unique_copy(vec.begin(), vec.end(), back_inserter(lst));
for (auto i : lst)
std::cout << i << " ";
std::cout << std::endl;
}
47 changes: 47 additions & 0 deletions ch10/ex10_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 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);
}

0 comments on commit ea7d4cf

Please sign in to comment.