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.
1. split off 29 30 31 from ex10_29_30_31.cpp 2. added 32 3. simplify 10.33
- Loading branch information
Showing
8 changed files
with
145 additions
and
140 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 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,28 @@ | ||
// | ||
// ex10_29.cpp | ||
// Exercise 10.29 | ||
// | ||
// Created by pezy on 12/13/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Write a program using stream iterators to read a text file into a vector of strings. | ||
|
||
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <vector> | ||
#include <string> | ||
#include <iterator> | ||
|
||
using std::string; | ||
|
||
int main() | ||
{ | ||
std::ifstream ifs("../data/book.txt"); | ||
std::istream_iterator<string> in(ifs), eof; | ||
std::vector<string> vec; | ||
std::copy(in, eof, back_inserter(vec)); | ||
|
||
// output | ||
std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator<string>(std::cout, "\n")); | ||
} |
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,25 @@ | ||
// | ||
// ex10_30.cpp | ||
// Exercise 10.30 | ||
// | ||
// Created by pezy on 12/13/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Use stream iterators, sort, and copy to read a sequence of integers from the standard input, | ||
// sort them, and then write them back to the standard output. | ||
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <iterator> | ||
|
||
int main() | ||
{ | ||
std::istream_iterator<int> in_iter(std::cin), eof; | ||
std::vector<int> vec; | ||
while (in_iter != eof) | ||
vec.push_back(*in_iter++); | ||
std::sort(vec.begin(), vec.end()); | ||
std::copy(vec.cbegin(), vec.cend(), std::ostream_iterator<int>(std::cout, " ")); | ||
} |
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,25 @@ | ||
// | ||
// ex10_31.cpp | ||
// Exercise 10.31 | ||
// | ||
// Created by pezy on 12/13/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Update the program from the previous exercise so that it prints only the unique elements. | ||
// Your program should use unqiue_copy | ||
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <iterator> | ||
|
||
int main() | ||
{ | ||
std::istream_iterator<int> in_iter(std::cin), eof; | ||
std::vector<int> vec; | ||
while (in_iter != eof) | ||
vec.push_back(*in_iter++); | ||
std::sort(vec.begin(), vec.end()); | ||
std::unique_copy(vec.cbegin(), vec.cend(), std::ostream_iterator<int>(std::cout, " ")); | ||
} |
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,32 @@ | ||
// | ||
// ex10_32.cpp | ||
// Exercise 10.32 | ||
// | ||
// Created by pezy on 12/13/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Rewrite the bookstore problem from 1.6 (p. 24) using a vector to hold the transactions | ||
// and various algorithms to do the processing. | ||
// Use sort with your compareIsbn function from 10.3.1 (p. 387) to arrange the transactions in order, | ||
// and then use find and accumulate to do the sum. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <numeric> | ||
#include "../include/Sales_item.h" | ||
|
||
int main() | ||
{ | ||
std::istream_iterator<Sales_item> in_iter(std::cin), in_eof; | ||
std::vector<Sales_item> vec; | ||
|
||
while (in_iter != in_eof) | ||
vec.push_back(*in_iter++); | ||
sort(vec.begin(), vec.end(), compareIsbn); | ||
for (auto beg = vec.cbegin(), end = beg; beg != vec.cend(); beg = end) { | ||
end = find_if(beg, vec.cend(), [beg](const Sales_item &item){return item.isbn() != beg->isbn();}); | ||
std::cout << std::accumulate(beg, end, Sales_item(beg->isbn())) << 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 |
---|---|---|
@@ -1,47 +1,35 @@ | ||
//! @Alan | ||
//! | ||
//! Exercise 10.33: | ||
//! Write a program that takes the names of an input file and two output files. | ||
//! The input file should hold integers. Using an istream_iterator read the | ||
//! input file. Using ostream_iterators, write the odd numbers into the first | ||
//! output file. Each value should be followed by a space. Write the even numbers | ||
//! into the second file. Each of these values should be placed on a separate line. | ||
// note : many algorithms can be used directly on iostream iterators. | ||
// This excerise can be seen as an example. | ||
//! | ||
|
||
// | ||
// ex10_33.cpp | ||
// Exercise 10.33 | ||
// | ||
// Created by pezy on 12/13/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// Write a program that takes the names of an input file and two output files. | ||
// The input file should hold integers. Using an istream_iterator read the input file. | ||
// Using ostream_iterators, write the odd numbers into the first output file. | ||
// Each value should be followed by a space.Write the even numbers into the second file. | ||
// Each of these values should be placed on a separate line. | ||
// | ||
// Run: ./a.out "../data/input.txt" "../data/odd.txt" "../data/even.txt" | ||
|
||
#include <fstream> | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <algorithm> | ||
|
||
|
||
void | ||
one2two(const std::string &Ifnm, const std::string &OoddFnm, const std::string &OevenFnm); | ||
int main() | ||
{ | ||
one2two("test_int.txt","odd.txt","even.txt"); | ||
return 0; | ||
} | ||
|
||
|
||
void | ||
one2two(const std::string &Ifnm, const std::string &OoddFnm, const std::string &OevenFnm) | ||
int main(int argc, char **argv) | ||
{ | ||
//! build input file stream and its iterators. | ||
std::ifstream fin(Ifnm); | ||
std::istream_iterator<int> fin_iter(fin), eof; | ||
|
||
//! build output file streams and their iterators. | ||
std::ofstream fOddOutput(OoddFnm), fEvenOutput(OevenFnm); | ||
std::ostream_iterator<int> fOddOutput_iter(fOddOutput, " "), fEvenOutput_iter(fEvenOutput, "\n"); | ||
|
||
|
||
//! using for_each algorithm to do operation on every element via a lambda | ||
std::for_each(fin_iter, eof, [&](const int i) | ||
{ | ||
//! if odd , output into the odd file and incerment the iterator | ||
//! if even , output into the even file and incerment the iterator | ||
i%2 ? *fOddOutput_iter++ = i : *fEvenOutput_iter++ = i; | ||
if (argc != 4) return -1; | ||
|
||
std::ifstream ifs(argv[1]); | ||
std::ofstream ofs_odd(argv[2]), ofs_even(argv[3]); | ||
|
||
std::istream_iterator<int> in(ifs), in_eof; | ||
std::ostream_iterator<int> out_odd(ofs_odd, " "), out_even(ofs_even, "\n"); | ||
|
||
std::for_each(in, in_eof, [&out_odd, &out_even](const int i){ | ||
*(i&0x1 ? out_odd : out_even)++ = i; | ||
}); | ||
} | ||
|
||
return 0; | ||
} |
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 @@ | ||
1 2 3 4 5 6 7 8 9 |