Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

STL/map

From ZeroWiki
Revision as of 05:27, 7 February 2021 by imported>Unknown
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

map

  • dictionary 구조를 구현하였다. DataStructure 에서는 symbol table 이라고 말한다.
  • dictionary 구조란 keyvalue가 존재하며, key를 이용하여 value를 찾는 자료구조이다.
  • 이 구조는 각 언어마다 다양한 이름을 가진다.
Perl, PHP Associated Array
Python dictionary
Java {{{~cpp HashMap, Hashtable}}}
STL(C++) map
  • include : map
#include <map> 
=== 선언 ===
// map<key_type, value_type>
map<string, long> m;
=== key  넣기 ===
m["홍길동"] = 20;
=== 순회 ===
  • STL의 container 들은 모두 비슷한 모양의 순회를 한다.
  • map 은 내부에 STL의 pair 를 이용하여 구현한다. 그래서, iterator 가 가리키는 것은 pair<key_type, value_type> 형이다.
  • 내부 인자들은 정열되어 있다.
// for 에서 반복자 이용 순회
for(map<int, int>::iterator i; i = m.begin() ; i != m.end() ; ++i) { 
	cout << "key: " << (*i).first 
		<< "value: " << (*i).second << endl;
} 

프로그램의 예

#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 를 비활성(기본값)화 시키세요. 


  1. pragma warning( disable : 4786 ) 하시면 됩니다.

STL