GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — CppPrimer/ch10 at master · AnddyWang/CppPrimer

Expand file tree

/

ch10

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

ch10

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
ex10_01_02.cpp
ex10_01_02.cpp
 
 
ex10_03_04.cpp
ex10_03_04.cpp
 
 
ex10_05.cpp
ex10_05.cpp
 
 
ex10_06.cpp
ex10_06.cpp
 
 
ex10_07.cpp
ex10_07.cpp
 
 
ex10_09.cpp
ex10_09.cpp
 
 
ex10_11.cpp
ex10_11.cpp
 
 
ex10_12.cpp
ex10_12.cpp
 
 
ex10_13.cpp
ex10_13.cpp
 
 
ex10_16.cpp
ex10_16.cpp
 
 
ex10_17.cpp
ex10_17.cpp
 
 
ex10_18_19.cpp
ex10_18_19.cpp
 
 
ex10_20_21.cpp
ex10_20_21.cpp
 
 
ex10_22.cpp
ex10_22.cpp
 
 
ex10_24.cpp
ex10_24.cpp
 
 
ex10_25.cpp
ex10_25.cpp
 
 
ex10_27.cpp
ex10_27.cpp
 
 
ex10_28.cpp
ex10_28.cpp
 
 
ex10_29.cpp
ex10_29.cpp
 
 
ex10_30.cpp
ex10_30.cpp
 
 
ex10_31.cpp
ex10_31.cpp
 
 
ex10_32.cpp
ex10_32.cpp
 
 
ex10_33.cpp
ex10_33.cpp
 
 
ex10_34_35_36_37.cpp
ex10_34_35_36_37.cpp
 
 
ex10_42.cpp
ex10_42.cpp
 
 

README.md

Outline

Chapter 10. Generic Algorithms

##Exercise 10.1 and 10.2 ##Exercise 10.3 and 10.4 ##Exercise 10.5 ##Exercise 10.6 ##Exercise 10.7

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

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.

##Exercise 10.11 ##Exercise 10.12 ##Exercise 10.13

Exercise 10.14:

Write a lambda that takes two ints and returns their sum.

auto add = [](int lhs, int rhs){return lhs + rhs;};

Exercise 10.15:

Write a lambda that captures an int from its enclosing function and takes an int parameter. The lambda should return the sum of the captured int and the int parameter.

int i = 42;
auto add = [i](int num){return i + num;};

##Exercise 10.16 ##Exercise 10.17 ##Exercise 10.18 and 10.19 ##Exercise 10.20 and 10.21 ##Exercise 10.22

Exercise 10.23:

How many arguments does bind take?

Assuming the function to be bound have n parameters, bind take n + 1 parameters. The additional one is for the function to be bound itself.

##Exercise 10.24 ##Exercise 10.25

Exercise 10.26:

Explain the differences among the three kinds of insert iterators.

  • back_inserter uses push_back.
  • front_inserter uses push_front.
  • insert uses insert

This function takes a second argument, which must be an iterator into the given container. Elements are inserted ahead of the element denoted by the given iterator.

Exercise 10.27

Exercise 10.28

Exercise 10.29

Exercise 10.30

Exercise 10.31

Exercise 10.32

Exercise 10.33

Exercise 10.34 ~ 10.37

Exercise 10.38:

List the five iterator categories and the operations that each supports.

  • Input iterators : ==, !=, ++, *, ->
  • Output iterators : ++, *
  • Forward iterators : ==, !=, ++, *, ->
  • Bidirectional iterators : ==, !=, ++, --, *, ->
  • Random-access iterators : ==, !=, <, <=, >, >=, ++, --, +, +=, -, -=, -(two iterators), *, ->, iter[n] == * (iter + n)

Exercise 10.39:

What kind of iterator does a list have? What about a vector?

list have the Bidirectional iterators. vector have the Random-access iterators.

Exercise 10.40:

What kinds of iterators do you think copy requires? What about reverse or unique?

  • copy : first and second are Input iterators, last is Output iterators.
  • reverse : Bidirectional iterators.
  • unique : Forward iterators.

Exercise 10.41:

Based only on the algorithm and argument names, describe the operation that the each of the following library algorithms performs:

replace(beg, end, old_val, new_val); // replace the old_elements in the input range as new_elements.
replace_if(beg, end, pred, new_val); // replace the elements in the input range which pred is true as new_elements.
replace_copy(beg, end, dest, old_val, new_val); // copy the new_elements which is old_elements in the input range into dest.
replace_copy_if(beg, end, dest, pred, new_val); // copy the new_elements which pred is true in the input range into dest.

Exercise 10.42

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.