练习11.1:描述map 和 vector 的不同。
map是关联容器,vector是顺序容器,关联容器与值无关,vector则与值密切相关
练习11.2:分别给出最适合使用 list、vector、deque、map以及set的例子。
list链表
vector动态数组
deque队列
map映射
set集合
练习11.3:编写你自己的单词计数程序。
#include <iostream>
#include <map>
void words_count()
{
std::map<std::string, std::size_t> m;
std::string word;
while (std::cin >> word)
{
++m[word];
}
}
int main()
{
words_count();
return 0;
}
练习11.4:扩展你的程序,忽略大小写和标点。例如,"example."、"example,"和"Example"应该递增相同的计数器。
#include <iostream>
#include <map>
void words_count()
{
std::map<std::string, std::size_t> m;
std::string word;
while (std::cin >> word)
{
for (auto& ch : word)
{
ch = tolower(ch);
}
if (ispunct(*--word.end()))
{
word = word.substr(0, word.size() - 1);
}
++m[word];
for (const auto &c : m)
{
std::cout << c.first << " occurs " << c.second << " times." << std::endl;
}
}
}
int main()
{
words_count();
return 0;
}
练习11.5:解释map和set的区别。你如何选择使用哪个?
map是键-值对的银蛇集合,而set是单纯的键集合。根据实际情况选择~~~
练习11.6:解释set和list的区别。你如何选择使用哪个?
set是单纯的键集合,list是链式值集合。根据实际情况选择~~~
练习11.7:定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
#include <iostream>
#include <vector>
#include <map>
void fun()
{
std::map<std::string, std::vector<std::string>> m;
std::string family, child;
std::cout << "input family name:";
while (std::cin >> family)
{
std::cout << "input child name:";
while (std::cin >> child)
{
m[family].push_back(child);
std::cout << "continue child?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
std::cout << "continue family?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
for (const auto &c : m)
{
std::cout << "family: " << c.first << std::endl;
std::cout << "child: ";
for (auto i : c.second)
{
std::cout << i << '\t';
}
std::cout << std::endl;
}
}
int main()
{
fun();
return 0;
}
练习11.8:编写一个程序,在一个vector而不是一个set中保存不重复的单词。使用set的优点是什么?
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
void fun()
{
std::vector<std::string> v;
std::string word;
while (std::cin >> word)
{
if (std::find(v.begin(), v.end(), word) == v.end())
{
v.push_back(word);
}
}
for (auto c : v)
{
std::cout << c << '\t';
}
}
int main()
{
fun();
std::cout << std::endl;
return 0;
}
优点是无需考虑键值重复问题。
练习11.9:定义一个map,将单词与一个行号的list关联,list中保存的是单词所出现的行号。
std::map<std::string, std::list<unsigned>> map;
练习11.10:可以定义一个vector<int>::iterator 到 int 的map吗?list<int>::iterator 到 int 的map呢?对于两种情况,如果不能,解释为什么。
可以定义 vector<int>::iterator 到 int 的map,不能定义 list<int>::iterator 到 int 的map。因为map的关键字类型要求 < 操作,list 的迭代器不支持比较运算。
练习11.11:不使用decltype 重新定义 bookstore。
multiset<Sales_data,bool (*)compareIsbn(const Sales_data &,const Sales_data &)> bookstore(compareIsbn);
练习11.12:编写程序,读入string和int的序列,将每个string和int存入一个pair中,pair保存在一个vector中。
#include <iostream>
#include <utility>
#include <vector>
int main()
{
std::string word;
int i;
std::vector<std::pair<std::string, int>> v;
std::cout << "input string:" << std::flush;
while (std::cin >> word)
{
std::cout << "input number:" << std::flush;
std::cin >> i;
v.push_back(make_pair(word, i));
std::cout << "continue(Y/N):" << std::flush;
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
std::cout << "input string:" << std::flush;
}
for (const auto &i : v)
{
std::cout << i.first << '\t' << i.second << std::endl;
}
return 0;
}
练习11.13:在上一题的程序中,至少有三种创建pair的方法。编写此程序的三个版本,分别采用不同的方法创建pair。解释你认为哪种形式最易于编写和理解,为什么?
// #1
v.push_back({word, i});
// #2
v.push_back(make_pair(word, i));
// #3
v.push_back(std::pair<std::string, int>(word, i));
第二种易于理解,第一种简洁,第三种则易读
练习11.14:扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair的vector,保存孩子的名和生日。
#include <iostream>
#include <vector>
#include <map>
void fun()
{
std::map<std::string, std::vector<std::string>> m;
std::vector<std::pair<std::string, std::string>> v;
std::string family, child, birth;
std::cout << "input family name:";
while (std::cin >> family)
{
std::cout << "input child name:";
while (std::cin >> child)
{
m[family].push_back(child);
std::cout << "input child birthday:";
std::cin >> birth;
v.push_back({child, birth});
std::cout << "continue child?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
std::cout << "continue family?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
for (const auto &c : m)
{
std::cout << "family: " << c.first << std::endl;
std::cout << "child: ";
for (auto i : c.second)
{
std::cout << i << '\t';
}
std::cout << std::endl;
}
}
int main()
{
fun();
return 0;
}
练习11.15:对一个int到vector<int>的map,其mapped_type、key_type和value_type分别是什么?
mapped_type是vector<int>,key_type是int,value_type是pair<int,vector<int>>
练习11.16:使用一个map迭代器编写一个表达式,将一个值赋予一个元素。
std::map<int, char> m; std::map<int, char>::iterator m_it = m.begin(); m_it->second = 'c';
练习11.17:假定 c 是一个string的multiset,v是一个string的vector,解释下面的调用。指出每个调用是否合法:
copy(v.begin(), v.end(), inserter(c, c.end())); copy(v.begin(), v.end(), back_inserter(c)); copy(c.begin(), c.end(), inserter(v, v.end())); copy(c.begin(), c.end(), back_inserter(v));
第二个错误,mutiset没有push_back操作
练习11.18:写出第382页循环中map_it的类型,不要使用auto或decltype。
std::map<std::string, size_t>::iterator
练习11.19:定义一个变量,通过对11.2.2节(第378页)中的名为 bookstore 的multiset调用begin()来初始化这个变量。写出变量的类型,不要使用auto或decltype。
multiset<Sales_data>::iterator mset=bookstore.begin();
练习11.20:重写11.1节练习(第376页)的单词计数程序,使用insert代替下标操作。你认为哪个程序更容易编写和阅读?解释原因。
#include <iostream>
#include <map>
void words_count()
{
std::map<std::string, std::size_t> m;
std::string word;
while (std::cin >> word)
{
auto ret = m.insert({word, 1});
if (!ret.second)
{
++ret.first->second;
}
}
}
int main()
{
words_count();
return 0;
}
第一种更容易阅读和书写。
练习11.21:假定word_count是一个string到size_t的map,word是一个string,解释下面循环的作用:
while (cin >> word)
++word_count.insert({word, 0}).first->second;
该语句是一个链式表达式的语句,首先插入一个pair,然后返回一个pair,返回的pair,第一个是指向map的元素的迭代器,然后对该迭代器解引用,获得所指元素关联的值,最后递增该值。
练习11.22:给定一个map<string,vector<int>>,对此容器的插入一个元素的insert版本,写出其参数类型和返回类型。
参数:pair<string, vector<int>>
返回类型:pair<map<string, vector<int>>::iterator,bool>
练习11.23:11.2.1节练习(第378页)中的map以孩子的姓为关键字,保存他们的名的vector,用multimap重写此map。
#include <iostream>
#include <vector>
#include <map>
void fun()
{
std::multimap<std::string, std::vector<std::string>> m;
std::string family, child;
std::cout << "input family name:";
while (std::cin >> family)
{
std::cout << "input child name:";
while (std::cin >> child)
{
std::vector<std::string> v;
v.push_back(child);
m.insert({family, v});
std::cout << "continue child?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
std::cout << "continue family?(Y/N):";
char c = 'Y';
std::cin >> c;
if (c == 'N' || c == 'n')
{
break;
}
}
for (const auto &c : m)
{
std::cout << "family: " << c.first << std::endl;
std::cout << "child: ";
for (auto i : c.second)
{
std::cout << i << '\t';
}
std::cout << std::endl;
}
}
int main()
{
fun();
return 0;
}
练习11.24:下面的程序完成什么功能?
map<int, int> m; m[0] = 1;
添加一个关键词到map中,如果该关键词已存在,则重新赋值。
练习11.25:对比下面的程序与上一题程序
vector<int> v; v[0] = 1;
错误,不能对空vector直接赋值。
练习11.26:可以用什么类型来对一个map进行下标操作?下标运算符返回的类型是什么?请给出一个具体例子———即,定义一个map,然后写出一个可以用来对map进行下标操作的类型以及下标运算符将会返会的类型。
std::map<int, char> m = { 1, '1' };
int index; //索引
char returnType; //返回类型
练习11.27:对于什么问题你会使用count来解决?什么时候你又会选择find呢?
需要知道数量时使用count,需要知道第一个元素位置时使用find。
练习11.28:对一个string到int的vector的map,定义并初始化一个变量来保存在其上调用find所返回的结果。
map<string,int>::iterator ret=map.end();
练习11.29:如果给定的关键字不在容器中,upper_bound、lower_bound 和 equal_range 分别会返回什么?
如果给定的关键字不在容器中,则 lower_bound和 upper_bound 会返回相等的迭代器,指向一个不影响排序的关键字插入位置。而equal_range 会返回一个 pair,pair 中的两个迭代器都指向关键字可以插入的位置。
练习11.30:对于本节最后一个程序中的输出表达式,解释运算对象pos.first->second的含义。
pos是指向map中的元素的迭代器pair,首先获取第first元素迭代器,然后解引用得到pair元素,再对pair元素解引用获取元素相关联的值。
练习11.31:编写程序,定义一个作者及其作品的multimap。使用find在multimap中查找一个元素并用erase删除它。确保你的程序在元素不在map中时也能正常运行。
#include <iostream>
#include <vector>
#include <map>
void fun()
{
std::multimap<std::string, std::string> m;
std::string author, writing;
std::cout << "input author name:";
while (std::cin >> author)
{
if (author == "N" || author == "n")
{
break;
}
std::cout << "input writing name:";
std::cin >> writing;
m.insert({author, writing});
std::cout << "input author name(N for break):";
}
std::cout << std::endl << std::endl;
for (const auto &c : m)
{
std::cout << "author: " << c.first << std::flush;
std::cout << " writing: " << c.second << std::endl;;
}
std::cout << "input author name that you want to delete: ";
std::string del;
std::cin >> del;
if (m.find(del) != m.end())
{
m.erase(m.find(del));
}
std::cout << std::endl << std::endl;
for (const auto &c : m)
{
std::cout << "author: " << c.first << std::endl;
std::cout << "writing: " << c.second << std::endl;;
}
std::cout << std::endl;
}
int main()
{
fun();
return 0;
}
练习11.32:使用上一题定义的multimap编写一个程序,按字典序打印作者列表和他们的作品。
上一题已按字典序打印,参见上一题。
练习11.33:实现你自己版本的单词转换程序。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
using namespace std;
map<string, string> buildMap(ifstream &map_file)
{
map<string, string> trans_map;
string key;
string value;
while (map_file >> key && getline(map_file, value))
if (value.size() > 1)
trans_map[key] = value.substr(1);
else
throw runtime_error("no rule for " + key);
return trans_map;
}
const string & transform(const string &s, const map<string, string> &m)
{
auto map_it = m.find(s);
if (map_it != m.cend())
return map_it->second;
else
return s;
}
void word_transform(ifstream &map_file, ifstream &input)
{
auto trans_map = buildMap(map_file);
string text;
while (getline(input, text))
{
istringstream stream(text);
string word;
bool firstword = true;
while (stream >> word)
{
if (firstword)
firstword = false;
else
cout << " ";
cout << transform(word, trans_map);
}
cout << endl;
}
}
int main()
{
return 0;
}
练习11.34:如果你将transform函数中的find替换为下标运算符,会发生什么情况?
如果使用下标运算符,当关键字未在容器中时,会导致容器中新添加一个元素。
练习11.35:在buildMap中,如果进行如下改写,会有什么效果?
trans_map[key] = value.substr(1);
改为trans_map.insert({key, value.substr(1)});
因为下标运算符没有就插入,有就是赋值,而insert没有时才插入,有时不做任何操作,因此当一个转换规则即key多次出现的时候,下标运算符会用最后一次添加的值来赋值覆盖,而用insert则是第一次添加的值。
练习11.36:我们的程序并没检查输入文件的合法性。特别是,它假定转换规则文件中的规则都是有意义的。如果文件中的某一行包含一个关键字、一个空格,然后就结束了,会发生什么?预测程序的行为并进行验证,再与你的程序进行比较。
terminate called after throwing an instance of 'std::runtime_error'
what(): no rule for k
已放弃 (核心已转储)
从程序运行结果来看,会抛出异常。
练习11.37:一个无序容器与其有序版本相比有何优势?有序版本有何优势?
无序性能更高,有序则可以保持顺序。
练习11.38:用 unordered_map 重写单词计数程序(参见11.1节,第375页)和单词转换程序(参见11.3.6节,第391页)。
略
来源:https://www.cnblogs.com/pluse/p/5779093.html