2단계 리스트를 STL로 바꾸기

girneter의 이미지

예를들어,
여러명의 선수가 팀을 짜서 팀끼리 리그를 합니다.
팀에 index 가 있고, 한 팀내에 선수에게 index 가 있어서
3-27
이라고 하면 3 팀의 27번 선수를 가리킵니다.

이 index 는 둘다 int 범위여야하고, 0 부터 시작하는게 아닙니다
그리고 리그에 팀이 새로 들어오거나 팀째로 빠지는 경우도 많고
한 팀 내에서도 선수의 영입이나 퇴출이 잦습니다.

이걸 2단계 리스트로 구현했습니다.
별건 아니고,

struct Player {
    int data;

    int team_num;
    int player_num;

    struct Player* next_team;
    struct Player* next_player;
}

struct Player* Head;

이렇게 해서는 3-27 번 선수를 찾고 싶으면
Head 부터 next_team 을 돌면서 team_num == 3 을 찾고
거기서부터 next_player 를 돌면서 player_num == 27 을 찾습니다.
별 문제없고 잘 돕니다만,
이걸 STL 을 써서 바꿔보고 싶다는 생각이 들었습니다.

어떻게 하면 좋을까요?
쉽게 생각하기에는

map<int, Player> PlayerMap;
map<int, map<int, Player> > TeamMap;

Player a();
PlayerMap[27] = a;
TeamMap[3] = PlayerMap;

(TeamMap[3])[27].data = 4;

이렇게 해도 되긴 될꺼 같은데 괜찮은건지.
추천해 주실만한 더 좋은 방법 있습니까?

doldori의 이미지

적당한 선택 같군요. 그런데...
Player a();
이것은 인자가 없고 Player를 반환하는 함수 a를 선언한 것입니다. 가끔씩 접하는
실수죠. 그냥 Player a; 로 선언하면 됩니다.

happyjun의 이미지

얼마나 효율적인지는 모르지만 boost::multi_index_container<> 도 사용할 수 있습니다.

Player를 RDB의 table이라고 생각하면, play_num이라는 primary key를 가지고 있고 team_num이라는 foreign key를 가지고 있다고 생각할 수 있습니다.

이런 table은 play_num, team_num의 Index를 가지고 있으면 search를 효율적으로 할 수 있겠죠.

그런 아이디어에서 출발한 것이 boost::multi_index_container<>입니다.

key라는 하나의 index만을 가지는 map과는 달리 n개의 index를 가질 수 있습니다.

http://boost.org/libs/multi_index/doc/index.html

----------------------------------------
http://moim.at
http://mkhq.co.kr

익명 사용자의 이미지

map<pair<int,int>, Player> 이런것도 될껄요.
저라면 Team이라는 클래스를 새로 정의해서 쓰겠습니다.

only2sea의 이미지

happyjun wrote:
얼마나 효율적인지는 모르지만 boost::multi_index_container<> 도 사용할 수 있습니다.

Player를 RDB의 table이라고 생각하면, play_num이라는 primary key를 가지고 있고 team_num이라는 foreign key를 가지고 있다고 생각할 수 있습니다.

이런 table은 play_num, team_num의 Index를 가지고 있으면 search를 효율적으로 할 수 있겠죠.

그런 아이디어에서 출발한 것이 boost::multi_index_container<>입니다.

key라는 하나의 index만을 가지는 map과는 달리 n개의 index를 가질 수 있습니다.

http://boost.org/libs/multi_index/doc/index.html

이걸 쓰면 결국 싱글 레벨이 되는 것이죠?
아마 여러명의 선수로 구성된 팀이 들어오거나 빠져나갈 때는 아주 조금 더 느려지겠죠. 그래도 꽤 좋은 것 같은데요. 목적에도 딱 맞고.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.