Data structure with auto-increment field

Discussion in 'C++' started by Michele 'xjp', Jul 9, 2007.

  1. Hi,
    I need a data structure (better if in STL) what give me:
    - an auto generated auto-increment integer field ('id')
    - various string fields

    This structure data must be searchable: I must be able to search any of
    the fields using a string (or a int if I search the 'id').
    This is equal to a record in a database, with the searchable property.

    To implement this, I was thinking to use STL's map<int, container>
    The container is a structure data (defined by me) that contains the
    various string fields. The key of the STL's map will be the number of
    the times that I call map.insert(). The container itself contains also
    the 'id'.

    What do you think about this?
    I am not sure that the container in the map will be searchable by his
    fields.

    Is there any way to do this in a better way?

    Thanks
     
    Michele 'xjp', Jul 9, 2007
    #1
    1. Advertisements

  2. On 2007-07-09 10:59, Michele 'xjp' wrote:
    > Hi,
    > I need a data structure (better if in STL) what give me:
    > - an auto generated auto-increment integer field ('id')
    > - various string fields
    >
    > This structure data must be searchable: I must be able to search any of
    > the fields using a string (or a int if I search the 'id').
    > This is equal to a record in a database, with the searchable property.
    >
    > To implement this, I was thinking to use STL's map<int, container>
    > The container is a structure data (defined by me) that contains the
    > various string fields. The key of the STL's map will be the number of
    > the times that I call map.insert(). The container itself contains also
    > the 'id'.


    I'd probably create a struct that contains the data you want and then
    push_back them into a std::vector, then the index will be the ID you are
    looking for and finding a record by id is O(1), to search by a string
    you have to create your own comparator (or loop over all elements), this
    will take O(n) unless they are inserted in some sorted order in which
    case it'll be O(log n).

    --
    Erik Wikström
     
    =?ISO-8859-15?Q?Erik_Wikstr=F6m?=, Jul 9, 2007
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    4
    Views:
    4,517
  2. Replies:
    104
    Views:
    12,068
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,994
    eliza81
    Jun 11, 2010
  4. Karthik Gurusamy

    auto-increment operator - why no syntax error?

    Karthik Gurusamy, Dec 9, 2007, in forum: Python
    Replies:
    2
    Views:
    773
    Marc 'BlackJack' Rintsch
    Dec 9, 2007
  5. Vincent De Groote

    structure address = structure first field address ?

    Vincent De Groote, Apr 29, 2009, in forum: C Programming
    Replies:
    31
    Views:
    2,374
  6. Chris Rebert

    Re: auto increment

    Chris Rebert, Mar 4, 2011, in forum: Python
    Replies:
    1
    Views:
    396
    BartC
    Mar 4, 2011
  7. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    838
    Peter J. Holzer
    Nov 23, 2008
  8. Replies:
    2
    Views:
    473
    Rui Maciel
    Dec 12, 2012
Loading...