Skip to content

Commit

Permalink
ch11 ex11.8
Browse files Browse the repository at this point in the history
  • Loading branch information
huangmingchuan committed Jan 11, 2016
1 parent 334c704 commit 37902b9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 17 deletions.
6 changes: 1 addition & 5 deletions Cpp_Primer_Answer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ch10\exercise7_26.h" />
<ClInclude Include="data\Sales_item.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ch10\exercise10_42.cpp" />
<ClCompile Include="ch11\exercise11_4.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{EAEE1AD0-0D58-421C-BAF5-604CEA494D9C}</ProjectGuid>
Expand Down
10 changes: 1 addition & 9 deletions Cpp_Primer_Answer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ch10\exercise7_26.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="data\Sales_item.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ch10\exercise10_42.cpp">
<ClCompile Include="ch11\exercise11_4.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
Expand Down
27 changes: 24 additions & 3 deletions ch11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,55 @@

> 描述map 和 vector 的不同。
`map` 是关联容器, `vector` 是顺序容器。

## 练习11.2

> 分别给出最适合使用 list、vector、deque、map以及set的例子。
## 练习11.3
* `list`:双向链表,适合频繁插入删除元素的场景。
* `vector`:适合频繁访问元素的场景。
* `deque`:双端队列,适合频繁在头尾插入删除元素的场景。
* `map`:字典。
* `set`:适合有序不重复的元素的场景。

## [练习11.3](exercise11_3.cpp)

> 编写你自己的单词计数程序。
## 练习11.4
## [练习11.4](exercise11_4.cpp)

> 扩展你的程序,忽略大小写和标点。例如,"example."、"example,"和"Example"应该递增相同的计数器。
## 练习11.5

> 解释map和set的区别。你如何选择使用哪个?
map 是键值对,而 set 只有键没有值。当我需要存储键值对的时候使用 map,而只需要键的时候使用 set。

## 练习11.6

> 解释set和list 的区别。你如何选择使用哪个?
set 是有序不重复集合,底层实现是红黑树,而 list 是无序可重复集合,底层实现是链表。

## 练习11.7

> 定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
## 练习11.8
```cpp
map<string, vector<string>> m;
for (string ln; cout << "Last name:\n", cin >> ln && ln != "@q";)
for (string cn; cout << "|-Children's names:\n", cin >> cn && cn != "@q";)
m[ln].push_back(cn);
```

## [练习11.8](exercise11_8.cpp)

> 编写一个程序,在一个vector而不是一个set中保存不重复的单词。使用set的优点是什么?
set 的优点是集合本身的元素就是不重复。

## 练习11.9

> 定义一个map,将单词与一个行号的list关联,list中保存的是单词所出现的行号。
Expand Down
17 changes: 17 additions & 0 deletions ch11/exercise11_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
std::map<std::string, std::size_t> word_count;
std::string word;
while (std::cin >> word)
++word_count[word];

for (const auto& elem : word_count)
std::cout << elem.first << " : " << elem.second << "\n";
return 0;
}
28 changes: 28 additions & 0 deletions ch11/exercise11_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cctype>

void word_count_pro(std::map<std::string, int>& m)
{
std::string word;
while (std::cin >> word)
{
for (auto& ch : word)
ch = tolower(ch);

word.erase(std::remove_if(word.begin(), word.end(), ispunct),
word.end());
++m[word];
}
for (const auto& e : m) std::cout << e.first << " : " << e.second << "\n";
}

int main()
{
std::map<std::string, int> m;
word_count_pro(m);

return 0;
}
17 changes: 17 additions & 0 deletions ch11/exercise11_8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::vector<std::string> exclude = { "aa", "bb", "cc", "dd", "ee", "ff" };
for (std::string word; std::cout << "Enter plz:\n", std::cin >> word;)
{
auto is_excluded = std::binary_search(exclude.cbegin(), exclude.cend(), word);
auto reply = is_excluded ? "excluded" : "not excluded";
std::cout << reply << std::endl;
}

return 0;
}

0 comments on commit 37902b9

Please sign in to comment.