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