본문 바로가기
프로그래머의 길/C & C++

Class Map 사용예제

by 제이콥케이 2009. 11. 30.
반응형

#include <iostream>
#include <conio.h>
#include <map> // <= map 을 사용하기 위한 전처리선언문
#include <string>

// map 은 key 와 value 를 동시에 저장할수 있는 형태

using namespace std;

int main()
{
 map <int, int> m1; // key 는 앞에것임, 뒤에것이 value
 map <int, int> :: iterator iter;
 
 m1.insert(pair<int,int> (1,10)); // pair를 사용한 insert
 m1.insert(pair<int,int> (2,20));
 m1.insert(pair<int,int> (3,30));
 
 m1[4] = 40 ; // <- 배열처럼도 사용이 가능하다. 이것은 m1.insert(pair<int,int> (4,40)) 의미임.
 iter = m1.find(2);

 cout <<"key 값이 2인 데이터 : " << iter->second << endl;
 iter = m1.find(5);
 if(iter==m1.end())
 {
  cout << "key 값이 5인 데이터는 웂슴다" << endl;
 }
 iter = m1.end();
 iter--;
 cout << iter->first << "번의 데이터는 " << iter->second << endl;
 getch();
 return 0;
}


반응형

'프로그래머의 길 > C & C++' 카테고리의 다른 글

CStringT::Tokenize.  (0) 2009.12.02
complex 예제 (복소수 연산) & conj (켤레 복소수)  (0) 2009.12.01
string <-> wstring  (0) 2009.07.22
비트맵 파일 RGB 조절  (0) 2008.05.19
자료형  (0) 2008.05.19