Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Vectors, Iterators and Binary Searches - I'm a bit confused!

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 22nd Jun 2011 19:28
I've been trying to get my head round C++ vectors (not the one for movement in 2D/3D space!) as a way of managing the data structures for a GUI system I'm working on, but I could use some advice on whether I stick with the methods I've been using or advance to pointers and iterators - I've done a lot of research online, but I'm not really any the wiser on how these things actually work.

In my project, I create each form with a unique ID (verified in a separate method), and always add it to the vector "Form" such that the vector is always sorted by the "iFormID" parameter. I frequently have to find the vector index of a particular FormID, so I've implemented my own binary search through the array.

Header file:


Main cpp file:


Firstly, I'm concerned about the line "int high = Form.size();", because Form.size() returns a value of type t_Size and I'm not sure if it's valid to assign it to an int (although the code compiles fine). I was hoping to use iterators, but then I'm not sure if this is valid:



The line "Mid = (High+Low)/2;" triggers a compiler error, as you can't add two iterators - so how do I find the Mid-point of the vector to continue the search? Also, is the comparison "while(High>=Low)" a valid statement? Is it even worthwhile advancing to iterators even if I can get this working? (I'm not sure quite what advantages they would bring)

I have found some references to the library <algorithms>, which provides a binary search method - it reqires iterators for the start and end of the search range in the vector, but it only returns true or false to state whether the specified ID exists. Is there any way to access the position (either an iterator or array index) of the entry? And if the requested form does not exist, can you get the index of where you should insert the form to keep all the iFormID variables running in numerical order (that is, the vector is always sorted)?


Sorry, for all of the questions, but I'm really out of my depth here - any help would be gratefully appreciated!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 23rd Jun 2011 01:06
Forget form IDs. Let the vector store a list of pointers to forms, as such:



The form ID is essentially it's position in the vector in this case. Having access to the objects directly is better than integer IDs.

Now, to answer some of your questions: 'size_t' is just a typedef for 'unsigned int', which are integers that can't be negative. Iterators are best used for traversing and iterating, and aren't really that good for random access. For a binary search, you're best using indexes.



"everyone forgets a semi-colon sometimes." - Phaelax
Mistrel
Retired Moderator
18
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 23rd Jun 2011 06:09 Edited at: 23rd Jun 2011 06:11
Quote: "Now, to answer some of your questions: 'size_t' is just a typedef for 'unsigned int', which are integers that can't be negative."


The actual size of size_t is not defined by the C++ standard and can vary between compilers and platforms. Be sure to study the specification when using built in typedefs to avoid falling into these traps:

Quote: "The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors,[3][4] when moving from 32 to 64-bit architecture, for example.

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits [5]."


http://en.wikipedia.org/wiki/Size_t

All you have to worry about is that it is unsigned and is used to define the size of an object. And don't try to convert it to another data type for storage; keep it as size_t.

Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 23rd Jun 2011 22:22
Thanks for all the advice on size_t, it's been really helpful.

I don't really want to let go of the FormID, as I'm trying to put together a function library that runs like DB objects or sprites - the user specifies the unique ID number to identify the form. To change parameters later, the user gives this ID to specify which form is to be modified. I can't just assign my own ID based on pointers as then the Form's will be unsorted as regards the FormID parameter of the user - hence the binary search is useless and the code slows down.

That said, I think a vector of pointers may be just the thing for my internal vector of visible forms - I'll have to play about with it some more.

Thanks once again!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.

Login to post a reply

Server time is: 2024-05-18 09:27:07
Your offset time is: 2024-05-18 09:27:07