C++/summary

STL - Map || Stack

gandus 2010. 10. 25. 10:53

 ·Map은 사전과 같은 자료 구조
·(key)가 제시되면 Map은 값(value)을 반환한다


map ( string(키 타입), string(데이타 타입) )

map<sting, string> dic;

dic["boy"] = "소년";


문장을 받아서 단어의 갯수를 파악한다
map을 이용.

#include<iostream>

#include<string>

#include<map>

using namespace std;

 

void main(){

 

        map<string, int> table;

        string s;

 

        cout<<"문장을입력하시오. : ";

        while(1){

               cin>> s;

               table[s]++;           

 

               if(cin.eof())  break;

               table[s]--;

        }

 

        map<string, int> ::iterator it;

        for(it= table.begin(); it != table.end(); it++) {

 

               cout<<it->first <<": " << it->second<<endl;

        }

}




 

·스택은 늦게 들어온 데이터들이 먼저 나가는 자료 구조







#include<iostream>

#include<string>

#include<stack>

using namespace std;

 

void main(){

 

        stack<char> st;

        string input;

        int i;

 

        cout<<"괄호를입력하시오.: ";

        cin>> input;

       

        for(i=0; i<input.length(); i++){

 

               if(input[i] == '[' || input[i] == '{' || input[i] == '(' )

               {

                       st.push(input[i]);

               }

 

               else{

                       if(st.empty())  return;               // 닫는괄호제거.

 

                       else if(input[i] ==']' && st.top() == '[')

                              st.pop();

                       else if(input[i] =='}' && st.top() == '{')

                              st.pop();

                       else if(input[i] ==')' && st.top() == '(')

                              st.pop();

 

                       else{

                              cout<<"잘못됨"<<endl;

                              break;

                       }

               }

        }

 

        if( st.empty() ){

               cout<<"올바름"<<endl;

        }

        else

               cout<<"틀림" <<endl;

              

 

}