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.0155 sec