Home » Community » Newbie corner » Algorithms and Vectors
Algorithms and Vectors [message #28130] |
Tue, 17 August 2010 22:58 |
281264
Messages: 270 Registered: June 2010 Location: Spain
|
Experienced Member |
|
|
Hi,
I have a query regarding the usage of Vectors and U++ algorithms. The case is as follows: I am using Vector to manage a list of objects; the structure of the objects in question is rather simple: 3 double variables, one unsigned integer and a bool variable. Is it possible to use Find or FindBinary (or any other algorithm) to make a search within the Vector of structs?
Cheers,
Javier
|
|
|
|
|
Re: Algorithms and Vectors [message #28138 is a reply to message #28136] |
Wed, 18 August 2010 16:06 |
|
Hi Javier,
The predicate is basicaly a less than function, not equality. So the code should beclass Predicate{
public:
bool operator()(const A& lhs, const A& rhs){
return lhs.a<rhs.a;
}
};
Honza
|
|
|
|
Re: Algorithms and Vectors [message #28145 is a reply to message #28142] |
Wed, 18 August 2010 18:52 |
|
Oh, ok, I see... Well, I don't have right now time to test compiling the code, but I can tell you right away, that you actually don't need this. Find(), FindBinary(), FindUpperBound() etc are doing something little else. They return index where the element should be inserted to keep the array sorted. Check the manual for details.
The simple way to find something, is to just go through the array in for cycle and look for the element you need. For simple struct like yours this should be pretty fast, especially if you check only value of one member.
If you need somewhat complex checking or exact equality of members, I would recommend you using ArrayIndex instead of Array. The only requirement is that you have to implement GetHashValue() function. If I remember correctly that is described quite good in Core Values Tutorial. Then you can search just using myindex.Find(myobject).
Sorry for not giving deeper explanation, I'm in hurry now
Honza
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Mar 29 10:49:13 CET 2024
Total time taken to generate the page: 0.01608 seconds
|