Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 17, 2016
0 parents
commit 6467b76
Showing
1,493 changed files
with
648,823 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2013 Ray Lischner | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
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,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*Exploring C++ 11*](http://www.apress.com/9781430261933) by Ray Lischner (Apress, 2013). | ||
|
||
![Cover image](9781430261933.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
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,14 @@ | ||
# Contributing to Apress Source Code | ||
|
||
Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. | ||
|
||
## How to Contribute | ||
|
||
1. Make sure you have a GitHub account. | ||
2. Fork the repository for the relevant book. | ||
3. Create a new branch on which to make your change, e.g. | ||
`git checkout -b my_code_contribution` | ||
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. | ||
5. Submit a pull request. | ||
|
||
Thank you for your contribution! |
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,97 @@ | ||
// Listing 1-2. Testing Your Compiler | ||
/// Sort the standard input alphabetically. | ||
/// Read lines of text, sort them, and print the results to the standard output. | ||
/// If the command line names a file, read from that file. Otherwise, read from | ||
/// the standard input. The entire input is stored in memory, so don’t try | ||
/// this with input files that exceed available RAM. | ||
/// | ||
/// Comparison uses a locale named on the command line, or the default, unnamed | ||
/// locale if no locale is named on the command line. | ||
|
||
#include <algorithm> | ||
#include <cstdlib> | ||
#include <fstream> | ||
#include <initializer_list> | ||
#include <iostream> | ||
#include <locale> | ||
#include <string> | ||
#include <vector> | ||
|
||
template<class C> | ||
struct text : std::basic_string<C> | ||
{ | ||
text() : text{""} {} | ||
text(char const* s) : std::basic_string<C>(s) {} | ||
text(text&&) = default; | ||
text(text const&) = default; | ||
text& operator=(text const&) = default; | ||
text& operator=(text&&) = default; | ||
}; | ||
|
||
/// Read lines of text from @p in to @p iter. Lines are appended to @p iter. | ||
/// @param in the input stream | ||
/// @param iter an output iterator | ||
template<class Ch> | ||
auto read(std::basic_istream<Ch>& in) -> std::vector<text<Ch>> | ||
{ | ||
std::vector<text<Ch>> result; | ||
text<Ch> line; | ||
|
||
while (std::getline(in, line)) | ||
result.push_back(line); | ||
|
||
return result; | ||
} | ||
|
||
/// Main program. | ||
int main(int argc, char* argv[]) | ||
try | ||
{ | ||
// Throw an exception if an unrecoverable input error occurs, e.g., | ||
// disk failure. | ||
std::cin.exceptions(std::ios_base::badbit); | ||
|
||
// Part 1. Read the entire input into text. If the command line names a file, | ||
// read that file. Otherwise, read the standard input. | ||
std::vector<text<char>> text; ///< Store the lines of text here | ||
if (argc < 2) | ||
text = read(std::cin); | ||
else | ||
{ | ||
std::ifstream in(argv[1]); | ||
if (not in) | ||
{ | ||
std::perror(argv[1]); | ||
return EXIT_FAILURE; | ||
} | ||
text = read(in); | ||
} | ||
|
||
// Part 2. Sort the text. The second command line argument, if present, | ||
// names a locale, to control the sort order. Without a command line | ||
// argument, use the default locale (which is obtained from the OS). | ||
std::locale const& loc{ std::locale(argc >= 3 ? argv[2] : "") }; | ||
std::collate<char> const& collate( std::use_facet<std::collate<char>>(loc) ); | ||
std::sort(text.begin(), text.end(), | ||
[&collate](std::string const& a, std::string const& b) | ||
{ | ||
return collate.compare(a.data(), a.data()+a.size(), | ||
b.data(), b.data()+b.size()) < 0; | ||
} | ||
); | ||
|
||
// Part 3. Print the sorted text. | ||
for (auto const& line : text) | ||
std::cout << line << '\n'; | ||
} | ||
catch (std::exception& ex) | ||
{ | ||
std::cerr << "Caught exception: " << ex.what() << '\n'; | ||
std::cerr << "Terminating program.\n"; | ||
std::exit(EXIT_FAILURE); | ||
} | ||
catch (...) | ||
{ | ||
std::cerr << "Caught unknown exception type.\nTerminating program.\n"; | ||
std::exit(EXIT_FAILURE); | ||
} |
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,24 @@ | ||
// Listing 2-1. Reading Test | ||
/// Read the program and determine what the program does. | ||
|
||
#include <iostream> | ||
#include <limits> | ||
|
||
int main() | ||
{ | ||
int min{std::numeric_limits<int>::max()}; | ||
int max{std::numeric_limits<int>::min()}; | ||
bool any{false}; | ||
int x; | ||
while (std::cin >> x) | ||
{ | ||
any = true; | ||
if (x < min) | ||
min = x; | ||
if (x > max) | ||
max = x; | ||
} | ||
|
||
if (any) | ||
std::cout << "min = " << min << "\nmax = " << max << '\n'; | ||
} |
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,9 @@ | ||
// Listing 2-3. Determining the Number of Bits in a bool | ||
#include <iostream> | ||
#include <limits> | ||
|
||
int main() | ||
{ | ||
// Note that "digits" means binary digits, i.e., bits. | ||
std::cout << "bits per bool: " << std::numeric_limits<bool>::digits << '\n'; | ||
} |
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,18 @@ | ||
// Listing 3-1. Integer Arithmetic | ||
/// Read the program and determine what the program does. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int sum{0}; | ||
int count{}; | ||
int x; | ||
while (std::cin >> x) | ||
{ | ||
sum = sum + x; | ||
count = count + 1; | ||
} | ||
|
||
std::cout << "average = " << sum / count << '\n'; | ||
} |
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,20 @@ | ||
// Listing 3-2. Print Average, Testing for a Zero Count | ||
/// Read integers and print their average. | ||
/// Print nothing if the input is empty. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int sum{0}; | ||
int count{}; | ||
int x; | ||
while (std::cin >> x) | ||
{ | ||
sum = sum + x; | ||
count = count + 1; | ||
} | ||
|
||
if (count != 0) | ||
std::cout << "average = " << sum / count << '\n'; | ||
} |
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,15 @@ | ||
// Listing 3-3. Testing for Even or Odd Integers | ||
/// Read integers and print a message that tells the user | ||
/// whether the number is even or odd. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int x; | ||
while (std::cin >> x) | ||
if ( ) // Fill in the condition. | ||
std::cout << x << " is odd.\n"; | ||
else | ||
std::cout << x << " is even.\n"; | ||
} |
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,15 @@ | ||
// Listing 3-4. Testing for Even or Odd Integers | ||
/// Read integers and print a message that tells the user | ||
/// whether the number is even or odd. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int x; | ||
while (std::cin >> x) | ||
if ( ) // Fill in the condition. | ||
std::cout << x << " is even.\n"; | ||
else | ||
std::cout << x << " is odd.\n"; | ||
} |
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,22 @@ | ||
// Listing 3-5. Print Average, Testing for a Zero Count | ||
/// Read integers and print their average. | ||
/// Print nothing if the input is empty. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
int sum{0}; | ||
int count{}; | ||
int x; | ||
while (std::cin >> x) | ||
{ | ||
sum = sum + x; | ||
count = count + 1; | ||
} | ||
|
||
if (count == 0) | ||
std::cout << "No data.\n"; | ||
else | ||
std::cout << "average = " << sum / count << '\n'; | ||
} |
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,9 @@ | ||
// Listing 4-1. Different Styles of String Output | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Shape\tSides\n" << "-----\t-----\n"; | ||
std::cout << "Square\t" << 4 << '\n' << | ||
"Circle\t?\n"; | ||
} |
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,7 @@ | ||
// Listing 4-2. Printing a Double-Quote Character | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "\"\n"; | ||
} |
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,11 @@ | ||
// Listing 4-3. Adding a Triangle and Keeping the Columns Aligned | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Shape\t\tSides\n" << | ||
"-----\t\t-----\n"; | ||
std::cout << "Square\t\t" << 4 << '\n' << | ||
"Circle\t\t?\n" | ||
"Triangle\t" << 3 << '\n'; | ||
} |
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,15 @@ | ||
// Listing 4-4. Printing Information That Is Stored in Variables | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
std::string shape{"Triangle"}; | ||
int sides{3}; | ||
|
||
std::cout << "Shape\t\tSides\n" << | ||
"-----\t\t-----\n"; | ||
std::cout << "Square\t\t" << 4 << '\n' << | ||
"Circle\t\t?\n"; | ||
std::cout << shape << '\t' << sides << '\n'; | ||
} |
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,9 @@ | ||
// Listing 4-5. Defining and Printing an Empty String | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
std::string empty; | ||
std::cout << "|" << empty << "|\n"; | ||
} |
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,15 @@ | ||
// Listing 4-6. Demonstrating Uninitialized Variables | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
std::string shape; | ||
int sides; | ||
|
||
std::cout << "Shape\t\tSides\n" << | ||
"-----\t\t-----\n"; | ||
std::cout << "Square\t\t" << 4 << '\n' << | ||
"Circle\t\t?\n"; | ||
std::cout << shape << '\t' << sides << '\n'; | ||
} |
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,15 @@ | ||
// Listing 5-1. Demonstrating Input and Output | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Enter a number: "; | ||
int x; | ||
std::cin >> x; | ||
std::cout << "Enter another number: "; | ||
int y; | ||
std::cin >> y; | ||
|
||
int z{x + y}; | ||
std::cout << "The sum of " << x << " and " << y << " is " << z << "\n"; | ||
} |
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,14 @@ | ||
// Listing 5-2. Reading Strings | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() | ||
{ | ||
std::cout << "What is your name? "; | ||
std::string name{}; | ||
std::cin >> name; | ||
std::cout << "Hello, " << name << ", how are you? "; | ||
std::string response{}; | ||
std::cin >> response; | ||
std::cout << "Good-bye, " << name << ". I'm glad you feel " << response << "\n"; | ||
} |
Oops, something went wrong.