Skip to content

Commit

Permalink
fix ex11.34
Browse files Browse the repository at this point in the history
  • Loading branch information
shidenggui committed Dec 26, 2014
1 parent 6dfd42b commit 1bfe1ab
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ch11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ std::pair<std::map<std::string, std::vector<int>>::iterator, bool> // return
## 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.

In gcc 4.8.3, will report error:
```cpp
error: passing ‘const std::map<std::basic_string<char>, std::basic_string<char> >’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::basic_string<char>; _Compare = std::less<std::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::basic_string<char>]’ discards qualifiers [-fpermissive]
auto key = m[s];
^
```
Because std::map's operator is not declared as **const**,but m is declared as a reference to std::map with **const**.If insert new pair,it will cause error.
## 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)})`?
Expand Down

0 comments on commit 1bfe1ab

Please sign in to comment.