map的例子

简介: 11.4 编写单词计数程序,忽略大小写和标点。例如,“example.”,“example,"和”Example“应该递增相同的计算器。 #include #include #include #include using namespace std; int main() { ...

11.4 编写单词计数程序,忽略大小写和标点。例如,“example.”,“example,"和”Example“应该递增相同的计算器。

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    map<string,size_t> word_count;
    string word;
    while(cin>>word)
    {
        word[0]=tolower(word[0]);
        auto f=find(word.begin(),word.end(),',');
        if(f!=word.end())
            word.erase(f);
        auto ff=find(word.begin(),word.end(),'.');
        if(ff!=word.end())
            word.erase(ff);
        ++word_count[word];
    }
    for(auto w:word_count)
        cout<<w.first<<" occurs "<<w.second<<endl;
    return 0;
}

 

11.7定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子们的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。

#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<utility>
using namespace std;

int main()
{
    vector<string> student;
    map<string,vector<string>> family;
    string firstname;
    string lastname;
    /*while(cin>>lastname&&lastname!="0")
    {
        family.insert(make_pair(lastname,student));
    }*/
    while(cin>>lastname)
    {
        while(cin>>firstname&&firstname!="\n)
        family[lastname].push_back(firstname);
    }

    for(auto s:family)
    {
        cout<<s.first<<" firstname ";
        for(auto r:s.second)
            cout<<r<<" ";
        cout<<endl;
    }
     return 0;
}

 

相关文章
|
存储 前端开发 对象存储
一文搞懂Map与Set的用法和区别!
前言 作为前端开发人员,我们最常用的一些数据结构就是 Object、Array 之类的,毕竟它们使用起来非常的方便。往往有些刚入门的同学都会忽视 Set 和 Map 这两种数据结构的存在,因为能用 set 和 map 实现的,基本上也可以使用对象或数组实现,而且还更简单。 但是,存在必然合理,当你真正了解 Map 和 Set 之后,你就会发现它们原来时如此美好!
1525 0
一文搞懂Map与Set的用法和区别!
|
3月前
|
前端开发 程序员
【面试题】在循环 for、for-in、forEach、for-of 、map中改变item的值,会发生什么?
【面试题】在循环 for、for-in、forEach、for-of 、map中改变item的值,会发生什么?
|
3月前
|
前端开发 Java 测试技术
把List<对象>转变<String,List<对象>>形式,k为判断的值,v为k重复的数据。
把List<对象>转变<String,List<对象>>形式,k为判断的值,v为k重复的数据。
21 0
|
10月前
|
算法 C++ Python
C++中map的用法
⭐一、map的简介 map是C++STL中的一个关联式容器,它提供一对一的hash,它类似于Python中的字典,也有着键值对(Key-Value)这一说。我们可以通过键(Key)来找到值(Value),但需要注意的是,每个键(Key)只能在map中出现一次哦! map可以储存多种类型的数据,它主要用于一对一映射的情况,map内部的实现是通过自建一颗红黑树,这颗树可以对数据进行自动排序。所以在map内部所有的数据都是有序的,这个功能以后可以方便我们解决很多问题。
266 0
|
10月前
|
JSON 数据格式
for_forEach_map有什么区别?区别大了
for、forEach、map日常都在用,但是你知道他们有什么区别吗?为什么要有这么多功能相似的东西?性能怎么样?看这里,我告诉你
58 0
Python: list of list, 将内部 list 的 index 作为该内部 list 中每个元素的分类标签
Python: list of list, 将内部 list 的 index 作为该内部 list 中每个元素的分类标签
|
索引
forEach用法与map用法区别
forEach用法与map用法区别
148 0
|
算法 安全 Java
Array,List,Set及Map遍历内容的方法探究
Array,List,Set及Map遍历内容的方法探究
|
C++ 容器
map以及使用举例--C++基础
map以及使用举例--C++基础
114 0
map以及使用举例--C++基础
【Groovy】map 集合 ( map 集合定义 | 通过 getClass 函数获取 map 集合的类型 | 代码示例 )
【Groovy】map 集合 ( map 集合定义 | 通过 getClass 函数获取 map 集合的类型 | 代码示例 )
223 0
【Groovy】map 集合 ( map 集合定义 | 通过 getClass 函数获取 map 集合的类型 | 代码示例 )