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. added 10.7 and 10.8 2. splited 10.6 from ex10_6_9_10.cpp 3. splited 10.10 from ex10_6_9_10.cpp 4. added clang different result noting in comments.
- Loading branch information
Showing
4 changed files
with
97 additions
and
17 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,27 @@ | ||
// | ||
// ex10_06.cpp | ||
// Exercise 10.6 | ||
// | ||
// Created by pezy on 12/9/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Using fill_n, write a program to set a sequence of int values to 0. | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using std::vector; using std::cout; using std::endl; using std::fill_n; | ||
|
||
int main() | ||
{ | ||
vector<int> vec{0,1,2,3,4,5,6,7,8,9}; | ||
fill_n(vec.begin(), vec.size(), 0); | ||
|
||
for (auto i : vec) | ||
cout << i << " "; | ||
cout << endl; | ||
} | ||
|
||
// @Output | ||
// 0 0 0 0 0 0 0 0 0 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,49 @@ | ||
// | ||
// ex10_07.cpp | ||
// Exercise 10.7 | ||
// | ||
// Created by pezy on 12/9/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Determine if there are any errors in the following programs and, if so, correct the error(s) | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <list> | ||
#include <algorithm> | ||
|
||
using std::vector; using std::cout; using std::endl; using std::list; using std::cin; using std::fill_n; | ||
|
||
template<typename Sequence> | ||
void print(Sequence const& seq) | ||
{ | ||
for (const auto& i : seq) | ||
cout << i << " "; | ||
cout << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
// (a) | ||
vector<int> vec; | ||
list<int> lst; | ||
int i; | ||
while (cin >> i) | ||
lst.push_back(i); | ||
vec.resize(lst.size()); | ||
// ^ Fixed: added this statement | ||
// Cause Algorithms that write to a destination iterator assume | ||
// the destination is large enough to hold the number of elements being written. | ||
copy(lst.cbegin(), lst.cend(), vec.begin()); | ||
|
||
// (b) | ||
vector<int> v; | ||
v.reserve(10); | ||
fill_n(v.begin(), 10, 0); | ||
// ^ (b)No error, but not any sense. v.size() still equal zero. | ||
// Fixed: 1. use `v.resize(10);` | ||
// or 2. use `fill_n(std::back_inserter(v), 10, 0)` | ||
|
||
print(v); | ||
print(vec); | ||
} |
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