Skip to content

Commit

Permalink
added 10.6 to 10.10
Browse files Browse the repository at this point in the history
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
pezy committed Dec 9, 2014
1 parent c04e9fc commit 23b6114
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 17 deletions.
20 changes: 20 additions & 0 deletions ch10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,23 @@
##[Exercise 10.1 and 10.2](ex10_01_02.cpp)
##[Exercise 10.3 and 10.4](ex10_03_04.cpp)
##[Exercise 10.5](ex10_05.cpp)
##[Exercise 10.6](ex10_06.cpp)
##[Exercise 10.7](ex10_07.cpp)

## Exercise 10.8:
>We said that algorithms do not change the size of the containers over which they operate. Why doesn’t the use of back_inserter invalidate this claim?
Cause the `back_inserter` is a **insert iterator**, what iterator adaptor that generates an iterator that **uses a container operation** to add elements to a given container.

the algorithms don't change the size, but the iterator can change it by using the container operation.

##[Exercise 10.9](ex10_09.cpp)

## Exercise 10.10:
>Why do you think the algorithms don’t change the size of containers?
@Mooophy:
The aim of this design is to separate the algorithms and the operation provided by member function.

@pezy:
Cause the library algorithms operate on **iterators**, **not containers**. Thus, an algorithm **cannot (directly)** add or remove elements.
27 changes: 27 additions & 0 deletions ch10/ex10_06.cpp
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
49 changes: 49 additions & 0 deletions ch10/ex10_07.cpp
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);
}
18 changes: 1 addition & 17 deletions ch10/ex10_6_9_10.cpp → ch10/ex10_09.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@
//! @author @Yue Wang @shbling @pezy @zzzkl
//! @date 01.12.2014
//!
//! Exercise 10.6:
//! Using fill_n, write a program to set a sequence of int values to 0.
//!
//! Exercise 10.9:
//! Implement your own version of elimDups. Test your program by printing
//! the vector after you read the input, after the call to unique, and after
//! the call to erase.
//!
//! Exercise 10.10:
//! Why do you think the algorithms don’t change the size of containers?
// The aim of this design is to seperate the algorithms and the operation
// provided by memeber function.
//!

#include <iostream>
#include <string>
Expand Down Expand Up @@ -55,23 +47,15 @@ void elimdups(std::vector<std::string> &vs)

int main()
{
//! ex 10.6
std::vector<int> v{1,2,3,4,5,6,7,8,9};
std::fill_n(v.begin(),v.size(),0);
std::cout << "ex10.6:\n";
println(v);

//! ex 10.9
std::vector<std::string> vs{"a","v","a","s","a","a","a"};
std::cout << "ex 10.9:\n";
elimdups(vs);
return 0;
}
//! output:
//ex10.6:
//0 0 0 0 0 0 0 0 0
//ex 10.9:
//a v a s a a a
//a a a a a s v
//a s v a a a a
//a s v a a a a // VS2013 or clang will have some differences. (a s v a a)
//a s v

0 comments on commit 23b6114

Please sign in to comment.