E D R S I H C RSS
ID
Password
Join
청년에게 권하고 싶은 것은 다음 세 마디뿐이다. 즉 일하라. 더욱 더 일하라. 끝까지 일하라. -비스마르크



Contents

1 list의 내용들을 담고 map을 인덱스처럼 사용하려고 합니다. 여기서 map의 두번째 요소(second)에 list에 iterator를 넣어 관리할 수 있을까요? 된다면 상당히 구조적인 관리가 가능할 것 같은데...

1 list의 내용들을 담고 map을 인덱스처럼 사용하려고 합니다. 여기서 map의 두번째 요소(second)에 list에 iterator를 넣어 관리할 수 있을까요? 된다면 상당히 구조적인 관리가 가능할 것 같은데... #

가능합니다. 다음은 그에 대한 테스트 소스입니다. 보시면 testmap의 second에 list의 iterator를 잡고 있는 것을 볼 수 있습니다. list 콘테이너의 추가함수중에서 iterator를 반환하는 것은 insert밖에 없어서 insert로 작성해보았습니다.
#include <stdio.h>

#include <map>
#include <list>
#include <string>
#include <iostream>
#include <utility>

using namespace std;

int main(int argc, char* argv[])
{
	typedef list<long> testlisttype;
	typedef map<string, testlisttype::iterator> testmaptype;
	testmaptype testmap;
	testlisttype testlist;

	// list와 map의 추가작업
	for (int i = 0; i < 10000; i++)
	{
		// list에 추가할때마다 각각의 iterator를 백업
		testlisttype::iterator _iter = testlist.insert(testlist.end(), i);
		// 키 문자열 생성
		char tmp[255]; sprintf(tmp, "key%d", i);
		// map에 키 문자열과 그에 대응하는 list상의 iterator를 대입
		testmap.insert(make_pair(string(tmp), _iter));
	}
	
	// map의 list iterator를 사용해서 대응하는 데이터를 찾는 작업
	for (int j = 0; j < 1000; j++)
	{
		char tmp[255];
		sprintf(tmp, "key%d", j);
		printf("%s -> %d\n", tmp, *(testmap[tmp]));
	}

	return 0;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-10-28 12:42:54
Processing time 0.0500 sec