U E D R , A S I H C RSS

STL/map

map

  • dictionary ꡬ쑰λ₯Ό κ΅¬ν˜„ν•˜μ˜€λ‹€. DataStructure μ—μ„œλŠ” symbol table 이라고 λ§ν•œλ‹€.
  • dictionary κ΅¬μ‘°λž€ key 와 valueκ°€ μ΄μž¬ν•˜λ©°, keyλ₯Ό μ΄μš©ν•˜μ—¬ valueλ₯Ό μ°ΎλŠ” μžλ£Œκ΅¬μ‘°μ΄λ‹€.
  • 이 κ΅¬μ‘°λŠ” 각 μ–Έμ–΄λ§ˆλ‹€ λ‹€μ–‘ν•œ 이름을 κ°€μ§„λ‹€.
    Perl, PHP Associated Array
    Python dictionary
    Java ~cpp HashMap, Hashtable
    STL(C++) map
  • include : map
    ~cpp 
    #include <map> 
    

μ„ μ–Έ


~cpp 
// map<key_type, value_type>
map<string, long> m;

key λ„£κΈ°


~cpp 
m["홍길동"] = 20;

순회

  • STL의 container 듀은 λͺ¨λ‘ λΉ„μŠ·ν•œ λͺ¨μ–‘μ˜ 순회λ₯Ό ν•œλ‹€.
  • map 은 내뢀에 STL의 pair λ₯Ό μ΄μš©ν•˜μ—¬ κ΅¬ν˜„ν•œλ‹€. κ·Έλž˜μ„œ, iterator κ°€ κ°€λ¦¬ν‚€λŠ” 것은 pair<key_type, value_type> ν˜•μ΄λ‹€.
  • λ‚΄λΆ€ μΈμžλ“€μ€ μ •μ—΄λ˜μ–΄ μžˆλ‹€.
    ~cpp 
    // for μ—μ„œ 반볡자 이용 순회
    for(map<int, int>::iterator i; i = m.begin() ; i != m.end() ; ++i) { 
    	cout << "key: " << (*i).first 
    		<< "value: " << (*i).second << endl;
    } 
    
  • ν”„λ‘œκ·Έλž¨μ˜ 예


    ~cpp 
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    
    int main()
    {
    	map<string, long> directory;
    	directory["홍길동"] = 1234567l;
    	directory["κΉ€μ² μˆ˜"] = 9876543l;
    	directory["김봉남"] = 3459876l;
    
    	
    	cout << "μ „ν™” λ²ˆν˜ΈλΆ€μ˜ λ‚΄μš©μ€ " <<endl;
    	map<string, long>::iterator i;
    	i = directory.begin();
    	for ( ; i != directory.end();i++)
    		cout << "이름:" << (*i).first 
    		<< " μ „ν™”λ²ˆν˜Έ: " << (*i).second << endl;
    	cout << "μž…λ‹ˆλ‹€. "<<endl;
    	
    	string name;
    	
    	while( cin >> name ){
    		if ( name.compare("exit") ==0)break;
    
    		cout << "이름을 μž…λ ₯ν•΄ μ£Όμ„Έμš”.(μ’…λ£Œ:exit):";
    		
    		if (directory.find(name) != directory.end())
    			cout << name << "의 μ „ν™”λ²ˆν˜ΈλŠ”"
    				<< directory[name] << "μž…λ‹ˆλ‹€.n";
    		else
    			cout << "μ£„μ†‘ν•©λ‹ˆλ‹€. κ·Έ 이름이 μ „ν™” λ²ˆν˜ΈλΆ€μ— μ—†μŠ΅λ‹ˆλ‹€." << name << "n";
    	}
    
    	return 0;
    }
    

    Thread

    μ•„μ‰¬μš΄μ  : VC++ 6.0 μ—μ„œ map ν•œλ²ˆ μ“°λ©΄ warning 이 72κ°œκ°€ λœ¬λ‹€; STLPort λ₯Ό 써야 ν• κΉŒ..
    warning 의 μ΄μœ λŠ” STLμ—μ„œ λ‚˜μ˜€λŠ” λ””λ²„κ·Έμ˜ 정보가 VC++ 디버그 정보λ₯Ό μœ„ν•΄ ν• λ‹Ήν•˜λŠ” 곡간(255byte)보닀 많기 λ•Œλ¬Έμž…λ‹ˆλ‹€. 보톡 디버그 λͺ¨λ“œλ‘œ 디버깅을 ν•˜μ§€ μ•ŠμœΌλ©΄, Project settingμ—μ„œ C/C++ ν…μ—μ„œ Debug info λ₯Ό μ΅œμ†Œν•œ line number only 둜 ν•΄λ†“μœΌλ©΄ warning λŠ” μ—†μ–΄ μ§‘λ‹ˆλ‹€. κ·Έλž˜λ„ warning κ°€ λ‚œλ‹€λ©΄ C/C++ ν…μ—μ„œ Generate browse info λ₯Ό λΉ„ν™œμ„±(κΈ°λ³Έκ°’)ν™” μ‹œν‚€μ„Έμš”.
    # pragma warning( disable : 4786 ) ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.
    ----
    STL
    Valid XHTML 1.0! Valid CSS! powered by MoniWiki
    last modified 2021-02-07 05:27:58
    Processing time 0.0115 sec