영넌 개발로그

[C++ STL] container map 본문

코딩/C++

[C++ STL] container map

영넌 2020. 12. 7. 21:20

container map 은 hash table과 같은 일을 한다.  hash table은 key와 value값을 가지고 있는 일방향 암호화 방식이다. 검색할 때 사용하는 것을 key라고 하고 진짜 값은 value에 저장되어 있는 자료구조이다. 맵에서는 순서가 중요하지 않다. 검색해서 key값을 이용해 바로 찾아갈 수 있기 때문이다.

 

맵에 값을 넣는 method는 insert를 사용한다. 반환되는 형식은 container set과 같이 pair로 받는다. <iterator, bool> 쌍으로 받으며 삽입에 성공하면 true 실패하면 false를 반환한다.

맵에서 값을 찾는 method는 find를 사용한다. 찾는 타겟의 위치를 iterator로 반환한다.

 

 

map for <key, value>

#include <iostream> 
#include <map>
#include <string>

using namespace std;


int main() {

	map<string, int> mymap;

	mymap.insert(make_pair("mike1", 24));
	mymap.insert(make_pair("mike2", 21));
	mymap.insert(make_pair("mike3", 29));
	mymap.insert(make_pair("mike4", 20));
	mymap.insert(make_pair("mike5", 19));

	
	//e는 pair 여서 first, second를 가짐
	for (auto& e : mymap) {
		cout << e.first << ", " << e.second << endl;
	}

	auto pos = mymap.find("mike3");
	//pos = mymap.find("mike103");

	//값이 나옴
	cout << mymap["mike1"] << endl;
	//없는건 0가 출력됨
	cout << mymap["mike99"] << endl;



	if (pos == mymap.end()) {
		cout << "Failed to find" << endl;
	}
	else {
		cout << "Find: " << (*pos).first << ", " << (*pos).second << endl;
	}


	//pair<map<string,int>::iterator, bool> res = mymap.insert(make_pair("mike10", 99));
	pair<map<string, int>::iterator, bool> res = mymap.insert(make_pair("mike3", 99));
	if (res.second == true) {
		cout << "Insertion success" << endl;
	}
	else {
		cout << "Insertion failed:" << (*(res.first)).first << ", " << (*(res.first)).second << endl;
	}

	mymap.erase("mike4");

	//e는 pair 여서 first, second를 가짐
	for (auto& e : mymap) {
		cout << e.first << ", " << e.second << endl;
	}

	


	return 0;

}

 

 

 

map for <object(key), value>

#include <iostream> 
#include <map>
#include <string>

using namespace std;


class Person {
public:
	string name;
	int age;
	Person(string _name, int _age) : name(_name), age(_age) {};

	bool operator<(const Person& p)const {
		if (name < p.name) {
			return true;
		}
		else if (name > p.name) {
			return false;
		}
		else {
			//이름 먼저 sort 이후에 나이 sort
			if (age < p.age) return true;
			else return false;
		}
	}
};

int main() {

	map<Person, int> mymap;

	mymap.insert(make_pair(Person("mike1", 24),24));
	mymap.insert(make_pair(Person("mike2", 21), 21));
	mymap.insert(make_pair(Person("mike3", 19), 19));
	mymap.insert(make_pair(Person("mike4", 17), 17));
	mymap.insert(make_pair(Person("mike5", 30), 30));

	

	//fine 대용
	cout << mymap[Person("mike1",24)] << endl;

	auto pos = mymap.find(Person("mike1", 24));

	//pos.first == key / pos.second == value
	if (pos == mymap.end()) {
		cout << "Failed to find" << endl;
	}
	else {
		cout << "Find: " << (*pos).first.name << ", " << (*pos).second << endl;
	}
	
	pair<map<Person, int>::iterator, bool> res = mymap.insert(make_pair(Person("mike10", 99),99));
	if (res.second == true) {
		cout << "Insertion success" << endl;
	}
	else {
		cout << "Insertion failed:" << (*(res.first)).first.name << ", " << (*(res.first)).second << endl;
	}

	mymap.erase(Person("mike2", 21));
	
	for (auto& e : mymap) {
		cout << e.first.name << ", " << e.first.age << ", " << e.second << endl;
	}

	return 0;

}

Comments