hdoj1075 What Are You Talking About(STL map处理字符串)

本文介绍了一种利用C++标准库中的map数据结构来实现字符串查找与替换的方法。通过具体的代码示例,展示了如何构建键值对并将这些键值对用于文本处理任务中。文章详细解释了每个步骤,并附带了完整的源代码。

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

来源:http://acm.hdu.edu.cn/showproblem.php?pid=1075

运用map处理问题

具体见代码注释

#include<stdio.h>
#include<string> 
#include<iostream>
#include<map>
#include <ctype.h>
using namespace std;
map<string,string> m;
string s;
int search(string key) //用来判断map里是否存在key值 
{
  map<string,string>::iterator it=m.find(key);
  if( it != m.end() )
    return 1;
    return 0;
}
int main()
{
	string start;
	string s1,s2,temp,t;//temp用来储存book部分 
	int i,j,len,k;
	cin>>start;
	while(cin>>s1>>s2)
	{
		if(s1=="END")break;
		m.insert(make_pair(s2,s1)); 	
	}
    i=0; temp="";
    getchar();
	while(getline(cin,s))//边循环边把内容加入temp ,用 gets读char*效率更好 
	{
		if(s=="END")break;
        temp+=s;temp+='\n';
	}
	for( j=0;j<temp.length();j++)//输出 
	{
		if(!isalpha(temp[j]))
		{
		  //t+='\0';
		  if(search(t))	 printf("%s%c",m[t].c_str(),temp[j]);
		  else printf("%s%c",t.c_str(),temp[j]);
		  t=""; continue;
		}
		t+=temp[j]; 		
	}
	//cout<<endl;	
return 0;	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值