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. Advertising

  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. Advertising

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. Replies:
    104
    Views:
    10,931
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,477
    eliza81
    Jun 11, 2010
  3. linkswanted
    Replies:
    1
    Views:
    879
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    879
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    506
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page