Re: Which is more efficient:STL:S et or array?
In message <_qGdnawZo-FTgxDfRVn-hA@rcn.net>, Pete Becker
<petebecker@acm .org> writes[color=blue]
>CodeCracker wrote:[color=green]
>> I think I wasn't clear. Let me rehearse what I was telling.
>> I have some items( either simple values as int, float etc or objects (
>> some other classes of mine say FileClass).
>> Now every time somebody calls my function DoSomething() I do a search
>> inside my array to find that item and if it is found I call DoThis()
>> and if not found I call DoThat().
>> But the problem here is that if I use an array implementation I have to
>> write a whole lot of If statement or write a loop where it goes in
>> search of the current item in the array, for which it will make a whole
>> lot of comparison. Instead of doing all this if I use the STL:set
>> containers and insert my items once there and then search for my
>> current item every time in my set will it not be more efficient than
>> my array implementation.
>> Remember I am not creating my container from scratch and using the
>> existing containers only.
>>[/color]
>
>If you're going to populate your array once and then not change it
>you're probably better off with a vector, because it uses less memory.
>
>vector<whateve r> vec(first, last);
>sort(vec.begin (), vec.end());[/color]
And maybe vec.erase(uniqu e(vec.begin(), vec.end()), vec.end()) to remove
duplicates?[color=blue]
>
>when you need to find something, use
>
>find(vec.begin (), vec.end(), X)[/color]
Since it's sorted, binary_search (if you just want to know if X is
there) or lower_bound (if you need an iterator to it) might be better.[color=blue]
>
>Even if you're going to add or remove things occasionally, this is
>probably better. set wins when you add and remove things fairly often.
>[/color]
--
Richard Herring
In message <_qGdnawZo-FTgxDfRVn-hA@rcn.net>, Pete Becker
<petebecker@acm .org> writes[color=blue]
>CodeCracker wrote:[color=green]
>> I think I wasn't clear. Let me rehearse what I was telling.
>> I have some items( either simple values as int, float etc or objects (
>> some other classes of mine say FileClass).
>> Now every time somebody calls my function DoSomething() I do a search
>> inside my array to find that item and if it is found I call DoThis()
>> and if not found I call DoThat().
>> But the problem here is that if I use an array implementation I have to
>> write a whole lot of If statement or write a loop where it goes in
>> search of the current item in the array, for which it will make a whole
>> lot of comparison. Instead of doing all this if I use the STL:set
>> containers and insert my items once there and then search for my
>> current item every time in my set will it not be more efficient than
>> my array implementation.
>> Remember I am not creating my container from scratch and using the
>> existing containers only.
>>[/color]
>
>If you're going to populate your array once and then not change it
>you're probably better off with a vector, because it uses less memory.
>
>vector<whateve r> vec(first, last);
>sort(vec.begin (), vec.end());[/color]
And maybe vec.erase(uniqu e(vec.begin(), vec.end()), vec.end()) to remove
duplicates?[color=blue]
>
>when you need to find something, use
>
>find(vec.begin (), vec.end(), X)[/color]
Since it's sorted, binary_search (if you just want to know if X is
there) or lower_bound (if you need an iterator to it) might be better.[color=blue]
>
>Even if you're going to add or remove things occasionally, this is
>probably better. set wins when you add and remove things fairly often.
>[/color]
--
Richard Herring
Comment