forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added 10.26 ~ 28 to README.md and simplify 10.2728
- Loading branch information
Showing
4 changed files
with
85 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |