Message Flood

本文介绍了一种使用C++中的std::string进行大小写转换的方法,并通过两个实例展示了如何利用multimap和map容器解决特定问题。第一个实例使用multimap结合std::transform函数处理字符串,第二个实例则直接采用map容器进行更新操作。文章提供了完整的代码示例,适用于需要掌握C++字符串操作和数据结构应用的读者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

~题目链接~

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1500&cid=1183

std::string字符串大小写转换~http://www.haogongju.net/art/2158882

ai枫 的博客  http://www.cnblogs.com/luyingfeng/p/3161649.html

输入

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

结果
3

(1).multimap多重映照容器 + std::transform函数

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>//包括各种数据结构的具体元素检索、替换、逆序等等通用的算法

using namespace std;

int main()
{
    int n,m;
    string str;
    while(~scanf("%d",&n)&&n!=0)
    {
        map<string,int> Q;
        scanf("%d",&m);
        int k=0;
        for(int i=0; i<n; i++)
        {
            cin>>str;
            transform(str.begin(),str.end(),str.begin(),::tolower);
            /*transform(str.begin(), str.end(), str.begin(), ::tolower); //将大写的都转换成小写
            transform(str.begin(), str.end(), str.begin(), ::toupper); //将小写的都转换成大写
            transform(str.begin(), str.end(), str.begin(), exchange);  //大小写切换*/
            Q.insert(pair<string,int>(str,1));//插入元素
        }
        for(int i=0; i<m; i++)
        {
            cin>>str;
            transform(str.begin(),str.end(),str.begin(),::tolower);
            if(Q.find(str)!=Q.end())//查找键值
            {
                if(Q[str])
                {
                    Q[str] = 0;
                    k++;
                }
            }

        }
        printf("%d\n",n-k);
    }
}

  

(2).map容器

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>

using namespace std;

void updata(char *str )
{
    int i,len=strlen(str);
    for (i=0; i<len; i++)
    {
        if( str[i]<='z' && str[i] >='a')
            str[i]=str[i]-'a'+'A';
    }
}

int main()
{
    int n,m;

    while(~scanf("%d",&n)&&n!=0)
    {
        map<string,int>Q;
        char str[12];
        scanf("%d",&m);
        int k=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",str);
            updata(str);
            Q[str]++;
        }
        for(int i=0; i<m; i++)
        {
            scanf("%s",str);
            updata(str);
            if(Q[str])
            {
                Q[str]=0;
                k++;
            }
        }
        printf("%d\n",n-k);
    }
    return 0;
}

  

  

转载于:https://www.cnblogs.com/guoyongzhi/p/3233037.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值