Skip to content

Commit

Permalink
Merge pull request pezy#99 from pezy/master
Browse files Browse the repository at this point in the history
Finished Chapter 11.
  • Loading branch information
Mooophy committed Dec 18, 2014
2 parents e6d7f80 + deeb8e3 commit 4bea2f1
Show file tree
Hide file tree
Showing 22 changed files with 370 additions and 776 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
- [Chapter 7. Classes](ch07/README.md)
- Part II: The C++ Library
- [Chapter 8. The IO Library](ch08/README.md)
- [Chapter 9. Sequential Containers](ch09)
- [Chapter 10. Generic Algorithms](ch10)
- [Chapter 11. Associative Containers](ch11)
- [Chapter 9. Sequential Containers](ch09/README.md)
- [Chapter 10. Generic Algorithms](ch10/README.md)
- [Chapter 11. Associative Containers](ch11/README.md)
- [Chapter 12. Dynamic Memory](ch12)
- Part III: Tools for Class Authors
- [Chapter 13. Copy Control](ch13)
Expand Down
35 changes: 0 additions & 35 deletions ch11/11_17.cpp

This file was deleted.

133 changes: 133 additions & 0 deletions ch11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Chapter 11. Associative Containers

## Exercise 11.1:
>Describe the differences between a map and a vector.
A `map` is a collection of key-value pairs. we can get a value **lookup by key** efficiently.

A `vector` is a collection of objects, and every object has an **associated index**, which gives access to that object.

## Exercise 11.2:
>Give an example of when each of list, vector, deque, map, and set might be most useful.
- list : a to-do list. always need insert or delete the elements anywhere.
- vector : save some important associated data, always need query elements by index.
- deque : message handle. FIFO.
- map : dictionary.
- set : bad_checks.

## [Exercise 11.3 and 11.4](ex11_3_4.cpp)

## Exercise 11.5:
>Explain the difference between a map and a set. When might you use one or the other?
- `set` : the element type is the **key type**.
- `map` : we should use a key-value pair, such as `{key, value}` to indicate that the items together from one element in the map.

I use `set` when i just need to store the `key`, In other hand, I would like use `map` when i need to store a key-value pair.

## Exercise 11.6:
>Explain the difference between a set and a list. When might you use one or the other?
`set` is unique and order, but `list` is neither. using which one depend on whether the elements are unique and order to store.

## [Exercise 11.7](ex11_7.cpp)
## [Exercise 11.8](ex11_8.cpp)
## [Exercise 11.9 and 11.10](ex11_9_10.cpp)
## [Exercise 11.11](ex11_11.cpp)
## [Exercise 11.12 and 11.13](ex11_12_13.cpp)
## [Exercise 11.14](ex11_14.cpp)

## Exercise 11.15:
>What are the mapped_type, key_type, and value_type of a map from int to vector<int>?
- mapped_type : vector<int>
- key_type : int
- value_type : std::pair<int, vector<int>>

## Exercise 11.16:
>Using a map iterator write an expression that assigns a value to an element.
```cpp
std::map<int, std::string> map;
map[25] = "Alan";
std::map<int, std::string>::iterator it = map.begin();
it->second = "Wang";
```

## Exercise 11.17:
>Assuming c is a multiset of strings and v is a vector
of strings, explain the following calls. Indicate whether each call is legal:

```cpp
copy(v.begin(), v.end(), inserter(c, c.end())); // legal
copy(v.begin(), v.end(), back_inserter(c)); // illegal, no `push_back` in `set`.
copy(c.begin(), c.end(), inserter(v, v.end())); // legal.
copy(c.begin(), c.end(), back_inserter(v)); // legal.
```
## [Exercise 11.18](ex11_18.cpp)
## Exercise 11.19:
>Define a variable that you initialize by calling begin() on the multiset named bookstore from 11.2.2 (p. 425).
Write the variable’s type without using auto or decltype.
```cpp
using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs);
std::multiset<Sales_data, compareType> bookstore(compareIsbn);
std::multiset<Sales_data, compareType>::iterator c_it = bookstore.begin();
```
## [Exercise 11.20](ex11_20.cpp)
## Exercise 11.21:
>Assuming word_count is a map from string to size_t and word is a string, explain the following loop:
```cpp
while (cin >> word)
++word_count.insert({word, 0}).first->second;
```
```cpp
++ (word_count.insert({word, 0}).first->second)
```
## Exercise 11.22:
>Given a map<string, vector<int>>, write the types used as an argument and as the return value for the version of insert that inserts one element.
```cpp
std::pair<std::string, std::vector<int>> // argument
std::pair<std::map<std::string, std::vector<int>>::iterator, bool> // return
```

## [Exercise 11.23](ex11_23.cpp)
## [Exercise 11.24 ~ 11.26](ex11_24_25_26.cpp)
## [Exercise 11.27 ~ 11.30](ex11_27_28_29_30.cpp)
## [Exercise 11.31](ex11_31.cpp)
## [Exercise 11.32](ex11_32.cpp)
## [Exercise 11.33](ex11_33.cpp)

## Exercise 11.34:
>What would happen if we used the subscript operator instead of find in the transform function?
If the subscript operator was used instead, it would add the given key into the map when no element with matching key was found.

## Exercise 11.35:
>In buildMap, what effect, if any, would there be from rewriting `trans_map[key] = value.substr(1);` as `trans_map.insert({key, value.substr(1)})`?
- use subscript operator: if a word does appear multiple times, our loops will put the **last** corresponding phrase into trans_map
- use `insert`: if a word does appear multiple times, our loops will put the **first** corresponding phrase into trans_map

## Exercise 11.36:
>Our program does no checking on the validity of either input file. In particular, it assumes that the rules in the transformation file are all sensible.
What would happen if a line in that file has a key, one space, and then the end of the line? Predict the behavior and then check it against your version of the program.

we added a file that name "word_transformation_bad.txt" to folder `data`. the file only has a key, one space.

the program of 11.33 don't influenced by that.

## Exercise 11.37:
>What are the advantages of an unordered container as compared to the ordered version of that container? What are the advantages of the ordered version?
- the advantages of an unordered container:
- useful when we have a key type for which there is no obvious ordering relationship among the elements
- useful for applications in which the cost of maintaining the elements in order is prohibitive
- the advantages of the ordered version:
- Iterators for the ordered containers access elements in order by key
- we can directly define an ordered container that uses a our own class types for its key type.

## [Exercise 11.38](ex11_38.cpp)
48 changes: 17 additions & 31 deletions ch11/ex11_11.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
//! @Alan
//! Exercise 11.11:
//! Redefine bookstore without using decltype.
// discussion on stack overflow:
// http://stackoverflow.com/questions/20608365/is-it-possible-to-code-this-waywhilelambda
//!
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <list>
//
// ex11_11.cpp
// Exercise 11.11
//
// Created by pezy on 12/15/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Redefine bookstore without using decltype.

#include "../ch07/ex7_26.h"
#include <set>



class A
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
int lenth;
public:
int getLenth() const {return lenth;}
};

bool compareA(const A &a1, const A &a2)
{
return a1.getLenth() < a2.getLenth();
return lhs.isbn() < rhs.isbn();
}

int main()
{
//! more approaches can be found on the post of SO.
bool (*fp) (const A &a1, const A &a2) = compareA;

std::multiset<A, bool (*) (const A &, const A &)> m1(fp);

return 0;
}


using compareType = bool (*)(const Sales_data &lhs, const Sales_data &rhs);
//typedef bool(*compareType)(const Sales_data &lhs, const Sales_data &rhs);
std::multiset<Sales_data, compareType> bookstore(compareIsbn);
}
80 changes: 28 additions & 52 deletions ch11/ex11_12_13.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,33 @@
//! @Alan
//! Exercise 11.12:
//! Write a program to read a sequence of strings and ints, storing
//! each into a pair. Store the pairs in a vector.
//!
//! Exercise 11.13:
//! There are at least three ways to create the pairs in the program
//! for the previous exercise. Write three versions of that program,
//! creating the pairs in each way. Explain which form you think is
//! easiest to write and understand, and why.
//!
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <list>
#include <set>
//
// ex11_12_13.cpp
// Exercise 11.12 11.13
//
// Created by pezy on 12/15/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Write a program to read a sequence of strings and ints,
// storing each into a pair. Store the pairs in a vector.
//
// There are at least three ways to create the pairs in the program for the previous exercise.
// Write three versions of that program, creating the pairs in each way.
// Explain which form you think is easiest to write and understand, and why.

#include <vector>
#include <utility>

#include <string>
#include <iostream>

int main()
{
std::vector<std::pair<std::string, int>> vec;
std::string str;
int i;
std::string word;

std::pair<std::string, int> pair;
std::vector<std::pair<std::string, int>> v;

while([&]()
{
std::cout << "enter a word:\n";
std::cin >> word;
std::cout << "enter an int:\n";
std::cin >> i;

return word != "@q";
}())
{
//! way 1:
//pair = {word, i};
//!^^^^^^^^^^^^^^^^^^^
//! way one is the easiest to write and understand.
//! way 2:
//pair = std::make_pair(word,i);
//! way 3:
pair = std::pair<std::string, int>(word,i);

v.push_back(pair);
for(auto e : v)
std::cout << e.first << " "
<< e.second << " ";
std::cout << "\n";
}
return 0;
}


while (std::cin >> str >> i)
vec.push_back(std::pair<std::string, int>(str, i));
//vec.push_back(std::make_pair(str, i));
//vec.push_back({str, i});
//vec.emplace_back(str, i); //!!! easiest way.

for (const auto &p : vec)
std::cout << p.first << ":" << p.second << std::endl;
}
35 changes: 0 additions & 35 deletions ch11/ex11_15_16.cpp

This file was deleted.

5 changes: 0 additions & 5 deletions ch11/ex11_18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <iterator>


int main()
{
Expand Down
Loading

0 comments on commit 4bea2f1

Please sign in to comment.