httpresponse结果返回map_C++与STL入门(5):映射map

本文介绍了C++中的STL关联容器map,包括其作为键值对的映射特性,重载的[]运算符,以及基本操作如插入、查找、删除等。通过一个反片语的例题,展示了如何利用map来标准化和检查单词的重复性。

map就是从键(key)到值(value)的映射。

它重载了[]运算符,使[]表示了不同的功能。

因此,map像是数组的“高级版”

例如,使用

map month_name

表示月份名字-->月份编号的映射,然后用

month_name["July"]=7

这样的方式进行赋值,表示

July --> 7

这样的映射。

下面,让我们继续了解一下map的用法:

回复map,获取CSDN论坛关于map的用法连接。

map是STL的一个关联容器,它提供一对一的hash。

  • 第一个可以称为关键字(key),每个关键字只能在map中出现一次;

  • 第二个可能称为该关键字的值(value);

就像函数,定义域必须能找到至少一个映射,但每个元素所能对应的是确定的

8f88da511ba5fe2cc06bee122b7dbf29.png

使用map得包含map类所在的头文件

#include   //注意,STL头文件没有扩展名.h

map对象是模板类,需要关键字和存储对象两个模板参数:

std:map<int, string> personnel;

这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

map的基本操作函数:

     C++ maps是一种关联式容器,包含“关键字/值”对

     begin()         返回指向map头部的迭代器

     clear()        删除所有元素

     count()         返回指定元素出现的次数

     empty()         如果map为空则返回true

     end()           返回指向map末尾的迭代器

     equal_range()   返回特殊条目的迭代器对

     erase()         删除一个元素

     find()          查找一个元素

     get_allocator() 返回map的配置器

     insert()        插入元素

     key_comp()      返回比较元素key的函数

     lower_bound()   返回键值>=给定元素的第一个位置

     max_size()      返回可以容纳的最大元素个数

     rbegin()        返回一个指向map尾部的逆向迭代器

     rend()          返回一个指向map头部的逆向迭代器

     size()          返回map中元素的个数

     swap()           交换两个map

     upper_bound()    返回键值>给定元素的第一个位置

     value_comp()     返回比较元素value的函数


例题:反片语

输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排得到输入文本中的另一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序排列。(大写在小写前)样例输入:ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries #(输入以#结束)

样例输出:

DiskNotEderaildrIedeyeladdersoon

#include 

#include 

#include 

#include 

#include 

#include 

using namespace std;

//思路:将单词标准化排序并用map以确定是否重复

map<string, int> cnt; //用于检验标准化单词

vector<string> words; //用于存储原始单词

//标准化单词

string stdw(const string s)

{

    string ans = s;

    for (int i = 0; i < ans.length(); i++)

        ans[i] = tolower(ans[i]);

    sort(ans.begin(), ans.end());

    return ans;

}

int main()

{

    string s;

    while (cin >> s)

    {

        if (s[0] == '#')

            break;

        words.push_back(s);

        string ans;

        ans = stdw(s);

        if (!cnt.count(ans))

            cnt[ans] = 0; //count 子函数 用于检查数目 重载[]表示ans-->0映射

        cnt[ans]++;

        //非常重要,如果没有找到映射关系,会返回0;因此后面使用!会出错

    }

    vector<string> answer;

    for (int i = 0; i < words.size(); i++)

        if (cnt[stdw(words[i])] == 1)

            //if (!cnt[stdw(words[i])])取消上面的cnt[ans]++;改用此句会出错,因为没有找到映射关系会返回 0

            answer.push_back(words[i]);

    sort(answer.begin(), answer.end());

    for (int i = 0; i < answer.size(); i++)

        cout << answer[i] << endl;

    return 0;

}

#include #include #include #include #include #include using namespace std;//思路:将单词标准化排序并用map以确定是否重复map<string, int> cnt; //用于检验标准化单词vector<string> words; //用于存储原始单词//标准化单词string stdw(const string s){    string ans = s;    for (int i = 0; i < ans.length(); i++)        ans[i] = tolower(ans[i]);    sort(ans.begin(), ans.end());    return ans;}int main(){    string s;    while (cin >> s)    {        if (s[0] == '#')            break;        words.push_back(s);        string ans;        ans = stdw(s);        if (!cnt.count(ans))//使用到计数            cnt[ans] = 0; //count 子函数 用于检查数目 重载[]表示ans-->0映射        cnt[ans]++;        //非常重要,如果没有找到映射关系,会返回0;因此后面使用!会出错    }    vector<string> answer;    for (int i = 0; i < words.size(); i++)        if (cnt[stdw(words[i])] == 1)            //if (!cnt[stdw(words[i])])取消上面的cnt[ans]++;改用此句会出错,因为没有找到映射关系会返回 0            answer.push_back(words[i]);    sort(answer.begin(), answer.end());    for (int i = 0; i < answer.size(); i++)        cout << answer[i] << endl;    return 0;}
结果:

fa2ba107aafb79e9132273a26b5c1a57.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值