U E D R , A S I H C RSS

Map연습문제/임영동

~cpp 
//벡터, 맵 사용 예제
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;

int main()
{
 //맵 객체들의 벡터인 decoder를 선언
 vector< map<char, char> > decoder;
 
 //각 디코딩 규칙인 rule들을 선언
 map<char, char> rule1;
 map<char, char> rule2;
 map<char, char> rule3;
 
 //각 디코딩 규칙의 상세한 내용들을 정의해줌
 rule1['b']='1';  rule2['1']='z';  rule3['z']='a';
 rule1[' ']='2';  rule2['2']='x';  rule3['x']='b';
 rule1['z']='3';  rule2['3']=' ';  rule3[' ']='c';
 rule1['a']='4';  rule2['4']='y';  rule3['y']='d';
 rule1['y']='5';  rule2['5']='a';  rule3['a']='e';
 rule1['x']='6';  rule2['6']='w';  rule3['w']='f';
 rule1['i']='7';  rule2['7']='v';  rule3['v']='g';
 rule1['h']='8';  rule2['8']='u';  rule3['u']='h';
 rule1['g']='9';  rule2['9']='b';  rule3['b']='i';
 rule1['\n']='0'; rule2['0']='t';  rule3['t']='j';
 rule1['c']='!';  rule2['!']='s';  rule3['s']='k';
 rule1['f']='@';  rule2['@']='c';  rule3['c']='l';
 rule1['e']='#';  rule2['#']='r';  rule3['r']='m';
 rule1['d']='$';  rule2['$']='o';  rule3['o']='n';
 rule1['w']='%';  rule2['%']='d';  rule3['d']='o';
 rule1['u']='^';  rule2['^']='p';  rule3['p']='p';
 rule1['v']='&';  rule2['&']='e';  rule3['e']='q';
 rule1['j']='*';  rule2['*']='q';  rule3['q']='r';
 rule1['k']='(';  rule2['(']='l';  rule3['l']='s';
 rule1['l']=')';  rule2[')']='m';  rule3['m']='t';
 rule1['m']='<';  rule2['<']='f';  rule3['f']='u';
 rule1['n']='>';  rule2['>']='g';  rule3['g']='v';
 rule1['o']='?';  rule2['?']='\n'; rule3['\n']='w';
 rule1['p']='{';  rule2['{']='h';  rule3['h']='x';
 rule1['t']='}';  rule2['}']='i';  rule3['i']='y';
 rule1['s']='=';  rule2['=']='j';  rule3['j']='z';
 rule1['q']='+';  rule2['+']='k';  rule3['k']='2';
 rule1['r']='-';  rule2['-']='n';  rule3['n']='\n';
 
 //디코딩 규칙을 디코더 벡터에 추가
 decoder.push_back(rule1);
 decoder.push_back(rule2);
 decoder.push_back(rule3);
 
 //해석할 문장을 선언
 string input="wjgydlrtyffworxjbdzyrsybfwlrobffylryjbkyjrtbdcyyrvmbjlsrkugjglrmdcgdarjbjyftr";

 //반복자 선언
 vector< map<char, char> >::iterator it;
 
 //디코딩용 루프
 for(it=decoder.begin();it!=decoder.end();++it)
 {
 /*for(int i=0;i!=input.size();++i)
 {//이런 식으로도 된다. 하지만 반복자 쓰는 연습을 하기 위해 아래의 식으로.
 input[i]=(*it)[input[i]];
 }*/
  for(string::iterator i=input.begin();i!=input.end();i++)
  {
   (*i)=(*it)[(*i)];
   //cout<<*i;
  }
 }
 cout<<input;
 return 0;
}



STL실습, 블스캠프2004/목요일
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0892 sec