#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    [BCB6] table for a quick and regular access to data?


    Hello everybody,

    I'm developping an application for a population dynamics simulation of birds inhabiting forest fragments. To store my individuals with their characteristics (x,y coordinates, habitat, age, sex...), I'm currently using a list of structures (StructName VarName[10000]) that I access very often (they move, die, breed...), and when needed, I call each individual by the line number of the structure. Is it a good idea? Or is there any better way (not too complicated) for a quick and regular access to my individuals? If so, how to implement it?
    I'm using a TListView to display the structure content, but when my 10,000 lines are full, the application struggles a lot. I'd like to improve that too.

    Thank you very much in advance.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    If you're accessing individuals by their array index, that is probably the fastest way to go. However, if you're searching for an item (not necessarily by the array index) you can perhaps use an STL map or hashmap if the order is not important.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0
    Originally Posted by Scorpions4ever
    if you're searching for an item (not necessarily by the array index) you can perhaps use an STL map or hashmap if the order is not important.
    Thanks, I'm getting into STL stuff and I managed to create a vector and a list from my structure. However, how can I search for a particular individual according to one of its characteristics?

    Is that the way?

    vector<StructName, allocator<StructName> > Vect;
    vector<StructName, allocator<StructName> >::iterator Iter;
    StructName TempStruct;
    (...)
    // loop with Iter
    {TempStruct= *Iter;
    if (TempStruct.Charact=="WhatIWant") Do something;
    }

    I tried that 'cause apparently I can't use the iterator directly, is there a way to get something like Iter.Charact?

    For my individuals, order is not important, I can add the new individuals only to the end, but I need to access them directly to delete them or to change their characteristics individually. Should I use a map? (Borland c++ Builder doesn't seen to support hash_map).
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    You can use a vector and an iterator something like this:
    Code:
    vector <StructName> Vect;
    Vect listofitems;
    
    // Add items to the list
    StructName somevar;
    somevar.Charac = "WhatIWant";
    listofitems.push_back(somevar);
    
    // Now look for an item
    StructName tempstruct;
    Vect::iterator Iter;
    // Note the != instead of < in the for statement below
    for (Iter = listofitems.begin(); Iter != listofitems.end(); Iter++) {
        tempstruct = *Iter;
        // Can also use (*Iter).Charact in the line below
        if (tempstruct.Charact == "WhatIWant") {
            // Do something
            break;
        }
    }
    However, this means you're iterating through a set of rows looking for something. If you want to jump to something directly, you may be better off using map. I will post a map example later, if you want it. hash_map is actually a non-standard extension to the STL, first added by SGI. It happens that a lot of vendors provide it, so I was hoping Borland did as well. In fact, there is a rumour that it will be included in the next set of standards.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0
    Thank you very much, it's getting much clearer now. But you're right, I should probably use a map.

    Originally Posted by Scorpions4ever
    If you want to jump to something directly, you may be better off using map. I will post a map example later, if you want it.
    That would be excellent! thx!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    almost there...


    OK, I managed to have a list, a vector and a map to work. There's no hash_map implemented in BCB6, there is a multi map but a single map will do.
    However it's pretty hard to decide what to choose. I often need to access directly the entries corresponding to some criterias but I regularly need to go through the complete list as well. If there is a big difference, what should I choose?
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Map or multimap might be the way to go here then. You can easily jump to a specific item using them. You can also go through all the items in a map or multimap similar to the same way you do it for a vector. For example:
    Code:
    #include <map>
    #include <iostream>
    #include <string>
    using namespace std;
    
    typedef map<string, int> Months;
    
    int main(void) {
      Months monthdays;
      monthdays["jan"] = 31; 
      monthdays["feb"] = 28; 
      monthdays["mar"] = 31; 
      monthdays["apr"] = 30;
    
      // Jump to a specific item in the list.
      cout << "March has " << monthdays["mar"] << " days" << endl;
    
      // Go through all the items in the list
      Months::iterator mon;
      for (mon = monthdays.begin(); mon != monthdays.end(); mon++)
        cout << (*mon).first << " has " << (*mon).second << endl;
    
      return 0;
    }
    Basically (*mon).first refers to the key value and (*mon).second refers to the item value.

    Also, if you want to use hash_map, you may be able to use other STL library implementations, besides the one that came with your compiler. http://www.stlport.com is one very well known, free and reliable STL library which also supports hash_map.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0
    Thank you for your code!
    However, it doesn't seem to be so easy with C++Builder... When I copied the code from my test-program to my real application, everything seems very complicated suddenly:
    First, with BCB, apparently, I need to declare e.g. a list like "list <int, allocator<int> > ListName". I did it in my small test-program and it works.
    But in my main application, if I use:
    typedef map<int, individuals, allocator< individuals> > MapName;
    the compiler expects a ',' after 'map<' .... weird.

    I checked all the project options and nothing is different between my test-program and my real application... I don't get it.

    I tried using std::map instead of map, and it says that map is not a member of std (?!?), independently of using "using namespace std" or not...
    I tried everything and nothing works... I decided to stick to a list, I made all the modifications in my application, and now, I face the problem with passing the iterator to some functions, that doesn't look simple again with BCB...

    Would you guys have any example under BCB6 of how to use STL??? I'm getting really sick of it, I don't know any more what I'm doing...
    By the way, Scorp, I noticed that STLport is implemented with BCB6, what you can't see in the main help file... I'm not even sure how to tell my program to use it and not the old stl. Any idea?
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    The following works just fine for me with C++ Builder 6. Bold letters indicate the lines that I added.
    Code:
    #include <vcl.h>
    #include <map>
    using namespace std;
    #pragma hdrstop
    
    typedef map<AnsiString, int> Months;
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    	Months monthdays;
    	monthdays["jan"] = 31;
    	monthdays["feb"] = 28;
    	monthdays["mar"] = 31;
    	monthdays["apr"] = 30;
    
    	// Go through all the items in the list
    	Months::iterator mon;
    	AnsiString s;
    	for (mon = monthdays.begin(); mon != monthdays.end(); mon++) {
    		s = (*mon).first + " has " + IntToStr((*mon).second);
    		ListBox1->Items->Add(s);
    	}
    
    }
    
    //---------------------------------------------------------------------------
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo