key, value 를 알맞은(?) 자료 구조에 넣기?

초보입니다.의 이미지

예를 들어 아래와 같은 key , value 값들이 있다고 할때

==name== ==value==
"Seoul" 500
"NewYork" 100
"Pusan" 400
: :

제 생각엔 2차원 배열로.. 관리가 가능 할 줄 알았습니다.
헌데.. 아래와 같이 사용했을때.. 2차원 배열은 알맞지 않단걸
알았습니다.

#include<stdio.h>
main()
{
int i;
char a[][8] = {"Seoul","NewYork","Pusan", ....};
for(i=0 ; i<5 ; i++)
printf("%s\n",a[i]);
}

위와 같이.. name 값(스트링) 만을 넣을 수가 있었습니다.

그렇담 key, value 를 넣기 알맞은 자료 구조는 무엇인가요???

위 자료를 넣고.. 검색에 사용할 생각입니다.

조언 부탁드리겠습니다.

dolsemix의 이미지

배열 말고도 구조체라는 것이 있지요...
키워드가 struct 였던가.... 기억이 가물가물 하네요 ^^;;
C 문법책 좀 찾아봐야겠습니다..

담배 고만 펴야겠다...

bugiii의 이미지

C++경우에 map< string, int > 정도면 손쉽게 해결할 수 있는 자료형태 같습니다.

C로 하시려면 자료 구조에 대한 좋은 텍스트를 구하시고, 해쉬나 맵 같은 자료 구조를 찾으시면 자세히 나와 있습니다.

litdream의 이미지

stdmap<class KEY, class VALUE>

삽질의 대마왕...

jenix의 이미지

strcut test
{
      char *name;
      int value;
}
.
.
.
void main(void)
{
    int i;
    struct test mycity[8];

    mycity[0].name = "Seoul\0";
    mycity[0].value = 15;
    .
    .
    .
    mycity[7].name = "NewYork\0";
    mycity[7].value = 33;

   for( i = 0; i < 8 ; i++ ) printf("%s \n",mycity[i].name);
}

---------------------------------------------------------------------------
http://jinhyung.org -- 방문해 보세요!! Jenix 의 블로그입니다! :D

yielding의 이미지

associative memory 형태의 자료구조를 쓰시려 하시군요
c++라면 위의 여러 분들이 말씀하신 map이 ok

c로 하시면 hash table을 쓰시던지 아니면
AVL tree등의 balance 트리를 inner representaion으로 쓰고 밖에 이 트리를 wrapping하는 function을 만들어서 쓰면 되겠네요.

Life rushes on, we are distracted

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.